Question & Answer: Including the initial parent process, how many processes are created by the following program? Explain……

Including the initial parent process, how many processes are created by the following program? Explain.

#include <stdio.h>

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Including the initial parent process, how many processes are created by the following program? Explain……
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

#include <unistd.h>

int main(void)

{

// Fork a child process

fork();

// fork another child process

fork();

// and another process

fork();

return 0;

}

Expert Answer

 On execution of the given code 8 processes are created.

Each fork function call creates child process to parent we becalculate number of process as order 2n where n refersto number of fork functions.

Explanation :-

  1. Initial parent ->total processes =1.
  2. Loop starts in parent, i == 0
  3. Parent fork()s, creating child 1.
  4. Now total Processes = 2 ( 1 parent 1 child)
  5. Loop restarts in both processes, now i == 1.
  6. Parent and child (1) fork(), creating children 2 and 3.
  7. Now Total Processes = 4 (1 parent 3 childs )
  8. Loop restarts in all four processes, now i == 2.
  9. Parent and children (1 ,2, 3 )all fork(), creating children 4 to 7.
  10. Now Total processes = 8 (1 parent 7 Childs )
  11. Loop restarts in all eight processes, now i == 3.
  12. i < 3 the condition false then come out from the loop.
  13. return 0;
  14. All processes are terminated then prrogram terminated.

Still stressed from student homework?
Get quality assistance from academic writers!