edited by
322 views
4 votes
4 votes

A process executes the following segment of code :

int foo(int n)
{
    int i;
    for(i=0; i <n; i++)
    {
        for(j = 0; j < n; j++)
        {
            if(j < i)
            {
                fork();
            }
        }
    ​}
    return 0;
}


The total number of child processes created if foo is called with parameter $n = 5$ is _______

edited by

1 Answer

Best answer
4 votes
4 votes
fork(), copies the current process image, and creates a child process which starts executing from the next instruction onward. fork() returns the child process id to the parent and $0$ to the child process.
 
 In the given code, $i$ and $j$ loop execute n times. But the if condition will be true for in total $0+1+2+\ldots n-1 = n(n-1)/2$ times. For each of these, the fork() ensures the number of processes doubles.
 
 For $n=5$ we'll have $5*4/2 = 10$ times the if condition gets TRUE and so, the number of processes created will be $2^{10} = 1024.$ But out of this $1$ is the parent process and total number of child processes $=1023.$
selected by
Answer:

Related questions

2 votes
2 votes
1 answer
2
gatecse asked Nov 15, 2020
104 views
Processes in an operating system can be in any of the following states. Choose the proper matching for the given table.$\begin{array}{|l|l|l|}\hline& \textbf{Process Stat...