in Operating System retagged by
1,488 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
in Operating System retagged by
1.5k views

3 Comments

is it 40 ??
0
0
The given answer is 10.
0
0
Answer will be 10 first two fork call not affect value z value still take by default value 0 when fun f call den Val of z=0+10 =10
0
0

1 Answer

2 votes
2 votes
Best answer

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

4 Comments

edited by
@MayankPrakash, the parent process will update its value to its own memory space and children to their own, so the children wont change ur actual data.

Consider that you are writing on a paper, some kids come to you, you being a kind person give them papers, they scribble on them. Their scribbling will affect their paper not your.

Also note that children and parent run parallel.
3
3
Thanks!!
0
0

wow nice explanation  sakharam  Thanks

0
0

Related questions