No Description

Iman Anwarzai 9390226b46 done 1 year ago
.gitignore d55d0adbce Initial commit 2 years ago
LICENSE d55d0adbce Initial commit 2 years ago
Makefile d2e7fc17aa initial commit 2 years ago
README.md f047734e4c Update 'README.md' 2 years ago
answers.txt 9390226b46 done 1 year ago
childcreates.c d2e7fc17aa initial commit 2 years ago
forkloop.c d2e7fc17aa initial commit 2 years ago
parentcreates.c 9390226b46 done 1 year ago
simplefork 9390226b46 done 1 year ago
simplefork.c d2e7fc17aa initial commit 2 years ago

README.md

Fork lab

Exercise 21

Introduction

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.

1. Run the simplefork program

Open 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.

2. Fork in a loop

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

2. Make the parent create all the new processes

Modify the 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

3. Make each child create a new process

Modify the 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

4. Add wait:

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 childcreates and 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 wait.

Finally

Commit the following files:

  • answers.txt
  • childcreates.c
  • parentcreates.c