Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write separate programs, as described below, that create 7 processes according to the parent/child hierarchy shown on the right, where PO is a parent to
Write separate programs, as described below, that create 7 processes according to the parent/child hierarchy shown on the right, where PO is a parent to P1 and P2, etc. Before each parent terminates, it must wait until all of its children have terminated. When created, every process should print its index (O - 6), its PID, and its parent's PID. Each process does no work and prints a message indicating when it is done. In your report, include your program output and a brief description of how you designed theprogram. PO P1 P2 P3 P4 P5 P6 a. Use a full, nested if-else structure after every fork) instruction such that one branch handles the parent and one branch handles the child. b. Use if statements without "else" and no more than 2 if-levels deep (maximum of one if inside another if). Note that P3 - P6 can also call 'wait" even though they do not have children (has no effect) The only requirement for output ordering is that it should demonstrate the necessary creation and termination of parents relative to children. For example, P1 should clearly be created some time before P.3 and P4, and it should clearly terminate some time after they do. However, P1 output does not need a particular order relative to P2 or its children OPTIONAL: If you wish, you may create a helper function to perform tedious tasks, such as printing (or more!). Note that defining a C function after main0 requires a prototype (https://computer howstuffworks.com/c13 htm). Below are two examples of possible output. Theoretically, both are possible for both programs. You may et slightly different time un Example #1 Created PO (PID 207). Parent is PID 4 Created P1 (PID 208) Parent is PID 207. Created P1 (PID 383). Parent is PID 382 Created P2 (PID 209) Parent is PID 207. Created P3 (PID 384) . Parent is PID 383 Created P3 (PID 210) Parent is PID 208. Created P2 (PID 385). Parent is PID 382 P3 is done Created P4 (PID 211) Parent is PID 208. P3 is done P4 is done P1 is done Created P5 (PID 212). Parent is PID 209. Created P5 (PID 387). Parent is PID 385 Created P6 (PID 213) Parent is PID 209. Created P6 (PID 388). Parent is PID 385 P5 is done P6 is done P2 is done PO is done Example #2 Created PO (PID 382) Parent is PID 4 Created P4 (PID 386 Parent is PID 383 P4 is done P1 is done P5 is done P6 is done P2 is done P0 is done Tip (output order): Don't forget to use fflush(stdout) to get reasonable output ordering. This was demonstrated in sleep.c in HW #1 and discussed at the end of a lecture exercise on using fork(). Tip (selfidentity): In part (b), processes can decide what to do based on their own identities. You can use several if statements, where each if statement checks its process's identity. Rather than using the PID assigned by the OS, an easier way for a process to track its own identity can be to use a variable that stores a value from 0 to 6. For example, if process #0 makes child process #2, that child process can immediately set its identity to be 2 . Then any actions that P2 needs to perform can be done after checking for this custom identity value of 2. In this way, you don't need to wory about keeping track of the PID assigned by the OS
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started