The following C program is executed on a Unix/Linux system :
#include<unistd.h>
int main()
{
int i;
for(i=0; i<10; i++)
if(i%2 == 0)
fork();
return 0;
}
The total number of child processes created is ________________ .
View worked solution
Answer: 31
To find the total number of child processes created, let's analyze the execution of the loop:
The loop variable ranges from to . The fork() system call is executed only when the condition i % 2 == 0 is true. This condition is satisfied for the following values of :
This means there are exactly iterations where a fork() is executed.
Since both the parent and the newly created child processes continue executing the loop from the next iteration, every process existing at the start of an even iteration will execute the fork() call. This doubling effect is identical to executing consecutive unconditional fork() statements:
- Initially, there is parent process.
- After the fork (at ): processes.
- After the fork (at ): processes.
- After the fork (at ): processes.
- After the fork (at ): processes.
- After the fork (at ): processes.
At the end of the program, there are total processes.
The number of child processes created is:
Free practice · Your answers on this page are not saved.
