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