Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Write separate programs, as described below, where the user enters the number of processes to be created. You must use a for loop to create

Write separate programs, as described below, where the user enters the number of processes to be created. You must use a for loop to create the processes. Before each parent terminates, it must wait until all of its children have terminated. When created, every process should print its index (0, 1, ...), its PID, and its parents PID. Each process does no work and prints a message indicating when it is done. In your report, include your program output.

  1. Create the processes as a series of parent/child pairs such that each new process is a child of the previous process. Below is an example of possible output (user input in bold):

    How many processes? 4 Created P0 (PID 207). Parent is PID 9. Created P1 (PID 208). Parent is PID 207. Created P2 (PID 209). Parent is PID 208. Created P3 (PID 210). Parent is PID 209. P3 is done. P2 is done. P1 is done. P0 is done.

  2. The first process is the parent, and all of the remaining processes are children of that first process. The only requirement for the output order is that the parent is clearly created first and terminates last. Below is an example of possible output (user input in bold). Note that child terminations do not need to be synchronized.

How many processes? 4 Created P0 (PID 207). Parent is PID 9. Created P1 (PID 208). Parent is PID 207. Created P2 (PID 209). Parent is PID 207. P2 is done. Created P3 (PID 210). Parent is PID 207. P1 is done. P3 is done. P0 is done.

Tip (where to start?): You might try just using fork() in a for loop and printing the PIDs to see what happens (and think about why). This would be similar to the lecture exercise we did in class where every process kept calling fork() again and again. Then think about what each parent or child should do inside the loop.

Tip (debugging): Since every process will run the loop separately, it may be challenging to understand what each process is doing. To help make sense of it, you might draw a table by hand with one column for each process, and track the value of all the variables in each process, including the for-loop iterator variable.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions