Lab 7 Solution

$29.99 $18.99

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. Open simplefork.c in your favourite 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…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

Rate this product

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.

Open simplefork.c in your favourite 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 running the program first with 1, 2, or 3 iterations. 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.

Also notice that some of the parent process ids are 1. This happens when the parent process terminates before the child calls getppid . (What do we call the child and the parent process when it is in thist state?) If you want avoid this situation you can add a sleep(1); just before the return call in main. Note that this is really just a hack and if we really want to ensure that a parent does not terminate before its child, we need to use wait correctly.

Question 3: How many processes are created, including the original parent, when forkloop is called with 2, 3, and 4 as arguments? n arguments?

Question 4: If we run forkloop 1 , two processes are created, the original parent process and its child. Assuming the process id of the parent is 414 and the process id of the child is 416, we can represent the relationship between these processes using the following ASCII diagram:

414 ‐> 416

Use a similar ASCII diagram to show the processes created and their relationships when you run forkloop 3 .

2页 共2 2019/3/23 14:08