Consider the following program include include include int m

Consider the following program: #include #include #include int main () {pid_t pidl=0, pid2=0; int i=3; pid1=fork(); if(pidl != 0) {printf(\"%d\ \", ++i); pid2 = fork(); if(pid2 != 0) {waitpid(pid1, NULL, 0); printf(%d\ \", ++i); waitpid(pid2,NULL, 0); printf(\"%d\ \", ++i); exit(0);} printf(\"%d\ \",i);} printf(\"%d\ \", ++i);} How many processes are created during the execution of this program? Explain. List all the possible outputs of the program

Solution

a) When the first fork() is executed , 2 processes are created. For the second pid2 again 2 more will be created.

b)The outcome for the following program would be:

4

5

4

5

6

The reason is as follows:

1.the first printf statement has ++i. since the initial value of i is 3 its first incremented and then printed.

2.the second printf statement prints 5 as the value gets incremented as before.

3.Here value is 4 because new process created has value of i as 3 which is then incremented.

4.The value of i is 5 which is then printed at the end of the statement.It is again incremented due to ++i thus printing the value 6.

 Consider the following program: #include #include #include int main () {pid_t pidl=0, pid2=0; int i=3; pid1=fork(); if(pidl != 0) {printf(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site