In: Computer Science
How many new processes will be created by each of the following program segment:
a. int i;
for (i = 0; i < 1000; i++)
if (fork()==0) break;
b. int i;
for (i = 0; i < 1000; i++)
if (fork()!=0) break;
c. int i, pid;
for (i = 0; i < 1000; i++)
pid = fork();
a.
The fork() system call is used to create a child process for the parent process. Using fork() system call n number of child processes can be generated from the same parent process.
Consider the given code.
The “for” loop iterates for 1000 times.
If the fork() system call is 0, it means the child process is created.
In the first iteration, the total number of processes = 2
In the second iteration, the total number of processes = 4
In the third iteration, the total number of processes = 8
The number of new processes = 2n -1
Here n =1000.
Then the number of new processes created = (21000 -1) processes.
b.
Consider the given code.
The “for” loop iterates for 1000 times.
If the fork() system call is not 0, it means the parent process is created.
In the first iteration, the total number of processes = 2
In the second iteration, the total number of processes = 4
In the third iteration, the total number of processes = 8
The number of new processes = 2n -1
Here n =1000.
Then the number of new processes created = (21000 -1) processes.
c.
Consider the code.
The “for” loop iterates for 1000 times.
In the first iteration, the total number of processes = 2
In the second iteration, the total number of processes = 4
In the third iteration, the total number of processes = 8
The number of new processes = 2n -1
Here n =1000.
Then the number of new processes created = (21000 -1) processes.