retagged by
1,410 views
4 votes
4 votes
The value of z after the execution of the following program is

void f(int x)

{

     static int z;

     z=z+x;

}

int main()

{

     int y=10;

     fork();

     fork();

     f(y);

     return 0;

}

 

Options are:

a. 40         b. 30       c. 20         d. 10
retagged by

1 Answer

Best answer
2 votes
2 votes

SInce fork() is called, child processes are created and each process has its own data segment(static variables are in data segment), hence the answer is 10.

Even if the process uses copy on write the answer will be 10.

https://unix.stackexchange.com/questions/87551/which-file-in-kernel-specifies-fork-vfork-to-use-sys-clone-system-call

selected by

Related questions

3 votes
3 votes
3 answers
1
2 votes
2 votes
3 answers
4
Philosophical_Virus asked Dec 10, 2023
768 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs