Question

In: Computer Science

Question: Including the initial parent process, how many processes are created by the program? And what...

Question: Including the initial parent process, how many processes are created by the program? And what is the value of i before each process terminates? Please draw the parent-child process hierarchy to justify your answer.

#include<stdio.h>

#include<unistd.h>

int main() {

          int i = 0;

          if (fork() == 0) {

                     ++i;

                     if (fork() != 0)

                             i = 3;

                     else

++i;

       fork();

          }   else   {

                      i = 5;

                      if (fork() == 0)

                                   ++i;

           }

           return 0;

}

I could really use a longer explanation of this problem. I have seen the answer but I still don't really understand how to get to the answer. Any help would be greatly appreciated.

Solutions

Expert Solution

This program creates 6 processes . Each process terminates when it encounters "return 0;" , so printing the value of "i" just before that would give me the final value of "i" for that process before the process exits, so just put a printf() before that. I executed it and i got these 6 values { 5,2,2,3,3,6 } ( The order may differ ).

Now each process has its own process space and has its own variables i.e. it's own copy of " i " . Now when fork() system call is executed it returns the process number to the parent process and " 0 " to the child process and that's how we determine the flow of execution for each process.

Let's look at the following hierarchy and its path of execution :

THe main process forks() the process -2 and process -3 .. Then process -2 spawns process -4 . Later on Process -2 & 4 does fork() to create process -5 and process -6 .

So the final value of "i" for each process would be :

Main process (Process -1 ) : 5

Process -2 (Spawned by process -1 in first if {} condition ) : 2

Process -3 (Spawned by process -1 in else{} part ) : 6

Process -4 (Spawned by process -2 ) : 3

Process - 5 ( Spawned by process -2 ) : 2

Process - 6 (Spawned by process -4) : 3


Related Solutions

What type of defects and how many are likely to be created when 1.5 moles of...
What type of defects and how many are likely to be created when 1.5 moles of NiO are added to 97.5 moles of SiO2?
How is urine created? (Describe the process as blood travels, filtrate is created, and urine is...
How is urine created? (Describe the process as blood travels, filtrate is created, and urine is excreted.)( 400 words essay )
Write a program to print 2 strings. One string must be written by parent process and...
Write a program to print 2 strings. One string must be written by parent process and another string must be written by a child process In language C
c programing language When a new program is executed, a new process is created with the...
c programing language When a new program is executed, a new process is created with the next available process ID. However, there are a few special processes that always have the same process ID, which are usually given the ID value less than 5 these are called system processes. Can you identify which of the two system processes have the process ID of 0 and 1 respectively?
processes for finding and evaluating research (dnp program) what are the processes for finding and evaluating...
processes for finding and evaluating research (dnp program) what are the processes for finding and evaluating research. the research process should intergrate sound theoretical and scientific results.
How will the Quality Payment Program affect the overall processes of a medical practice? What concerns...
How will the Quality Payment Program affect the overall processes of a medical practice? What concerns are there about the quality payment program?
How many constant volume processes are modeled when analyzing an Air Standard Diesel cycle? Question 10...
How many constant volume processes are modeled when analyzing an Air Standard Diesel cycle? Question 10 options: A) 2 B) 4 C) 1 D) 3 E) 0
Cell traction forces are vital for many biological processes, including angiogenesis, inflammation, wound healing, and metastasis....
Cell traction forces are vital for many biological processes, including angiogenesis, inflammation, wound healing, and metastasis. The study of cell traction forces enables us to better understand the mechanisms of these biological processes at the cellular and molecular levels. A two-spring model has been successfully applied to capture the impact of cell type and substrate stiffness on cell traction. The first spring constant represents extracellular elasticity as perceived by the cell through the focal adhesion site. The second spring constant...
Cell traction forces are vital for many biological processes, including angiogenesis, inflammation, wound healing, and metastasis....
Cell traction forces are vital for many biological processes, including angiogenesis, inflammation, wound healing, and metastasis. The study of cell traction forces enables us to better understand the mechanisms of these biological processes at the cellular and molecular levels. A two-spring model has been successfully applied to capture the impact of cell type and substrate stiffness on cell traction. The first spring constant represents extracellular elasticity as perceived by the cell through the focal adhesion site. The second spring constant...
How many generations of peacekeeping operations has the UN created since 1945? how many do you...
How many generations of peacekeeping operations has the UN created since 1945? how many do you consider successful and why? must be 750 words and not word to word from google
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT