Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENGIN 448 Operating Systems

Homework 1

Total: 50 pts

Due: 5:00pm, Feb. 17, 2023

P1. (5 pts)

Consider a C program that calls fork(). Before calling fork(), the main (i.e., parent) process accesses an integer variable myvar and set its value to 100. The child process changes the value of myvar to 200 and displays it via printf(). After  fork(), the parent process changes myvar to 300 and displays it via printf().

What values will be displayed on screen? Why?

P2. (20 pts total: 10 pts for the C code, and 10 pts for your answers to the two questions.)

Write a C program hw1_p2.c that opens a file (with the open() system call) and then calls fork() to create a new process.

1)    Can both the child and parent processes access the file descriptor returned by open()?

2)   What happens when they are writing to the file concurrently, i.e., at the same time?

Your answers to the two questions should be supported by the outputs of your program, otherwise no credits will be given.

P3. (20 pts)

Write a C program, hw1_p3.c, in which a parent process creates two child processes via fork(), and the program connects the standard output of the first child process to the standard input of the second child process by using the pipe() system    call.

The first child process should use the following statement to send a string to the second child process via standard output file descriptor 1 through the pipe.

write(1, "sent from the first child\n", 26);

The second child process should get the string sent by the first child process (via the pipe) by reading from standard input (i.e., file descriptor 0), e.g.,

int n = read(0, buf, sizeof(buf));

Then, the second child process should display on screen its received string (from first child process via the pipe), e.g., printf("the second child process with PID %d, receive: %s", (int)getpid(), buf);

------------------------------

You should write a Makefile (5 pts) that compiles your code hw1_p2.c and hw1_p3.c to generate executables: hw1_p2 and hw1_p3, if we run make on your Makefile.

------------------------------

Your code should use standard C libraries, not include any other customized header files.

------------------------------

2. Submission of your project

Zip the folder ofyour files thatinclude:

•    Answer to P1 (in .txt format, generated by a text editor)

•    hw1_p2.c, and answers to P2 (in .txt format, generated by a text editor).

•    hw1_p3.c,

•    Makefile,

If we cannot compile (by running make on your Makefile), your code on a Linux or Mac machine, 50% will first be deducted automatically, and then all your submitted materials will be further graded.