|Iman Anwarzai 9390226b46 done||1 year ago|
|.gitignore||2 years ago|
|LICENSE||2 years ago|
|Makefile||2 years ago|
|README.md||2 years ago|
|answers.txt||1 year ago|
|childcreates.c||2 years ago|
|forkloop.c||2 years ago|
|parentcreates.c||1 year ago|
|simplefork||1 year ago|
|simplefork.c||2 years ago|
The purpose of this exercise is to play with fork, and get a feeling for how it works. It should be a fairly short lab.
For this lab you are asked to answer some questions. Place the answers to the
questions in a plain text file called
answers.txt. Please make sure the answers are clearly labeled.
Use the Makefile to build the provided programs.
simplefork.c in your favorite editor. Read it through to
figure out what it is doing. Compile and run it a few times. Recall from lecture
that it is up to the operating system to decide whether the parent or the child
runs first after the fork call, and it may change from run to run.
Question 1: Which lines of output are printed more than once?
Question 2: Write down all the different possible orders for the output Note that this includes output orders that you may not be able to reproduce.
The program in
forkloop.c takes one command-line argument which
is the number of iterations of the loop that calls fork. Try it first with 2, 3 or
4. Notice that the shell prompt sometimes appears in the middle of the output.
That happens because the parent process exits before some of the children get a
chance to print their output.
Question 3: How many processes are created including the original parent when
forkloop is called with 2, 3, and 4 as arguments?
Question 4: Use an ASCII diagram to show the processes and their
relationships when you run with 3 processes. For example, the diagram for
simplefork with two processes would be (assuming the process id of the parent is 414
and the process id of the child is 416):
414 -> 416
parentcreates.c program so that the new children do
not create additional children. Only the original parent will create children.
Both the parent and the child will print the output line. The resulting diagram
will look something like the following when
parentcreates 3 is
called. Note that the child process ids will not necessarily be in sequence.
414 -> 416 414 -> 417 414 -> 420
childcreates.c program so that the each new child
creates a new process. A parent does not create any more processes. All
processes will print the output line. The resulting diagram will look something
like the following when
childcreates 3 is called:
414 -> 416 -> 417 -> 420
The information provided by the
getppid system call may not
always give you the information you expect. If a process's parent has
terminated, then the process gets "adopted" by the init process which has a
process id of 1. So when you see the ppid of a process is 1, that means that its
parent has terminated.
A process can wait for its children to terminate. If a process wants to wait
until all its children have terminated, it needs to call wait once for each
child. Add the appropriate
wait calls to both
parentcreates to ensure that each
parent does not terminate before its children.
Change your program to delay calling
wait as long as possible. In
other words if the process has other work to do like creating more children, it
should create the children first and then call
Commit the following files: