search
Log In
4 votes
614 views
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
614 views
0
is it 40 ??
0
The given answer is 10.
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

1 Answer

1 vote
 
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
1
is it global variables also separate for child process and parent process?
0

sakharam  But here Z is declare as a  static variable

0

@Magma

as per my knowledge,

static variables and global variables are stored as same memory segment

right?

if you are saying static variables are separate then global variable also separate

correct me if i am wrong.

0

No NO Shaik Masthan  you're totally right

0
@sakharamh @shaik

1.Here f(y) will be called 4 times and every process will have its separate variable z with value 10 ?

2.It won't be 40 because of protection concept and what happens if variable z is defined as static global variable.?

Please suggest on above points.
1
Each process has a data segment, code segment, heap and stack. When fork() is called the child process will copy everythin from parent to a new memory address. We are aware that static and global variables are in data segment so even they are copied to the new process. Fork() may follow a copy on write stratergy where in after fork's() work is done the content is copied back to the parent.

If you want to share memory addresses u can use vfork(). :-D

For more details, read 9.3: Copy on Write from Galvin book
0
@mayankprakash
1. Yes

2. If you want answer to be 40 use vfork() [I have not tried it], Even if the variable was global it will be copied for the new process.
0
see , whenver you created a child process , exactly the same memory image will be created as parent process , And  memory image contain (STACK , HEAP , DATA , CODE , FILE DESCRIPTER  )

there , The entire  address space is duplicated, including all global variables and the program text.

only register file and Program counter is different
0
Thanks sakharam

Nicely explained!!.

But I still have one doubt when you say global variable also gets copied then child process will get variable with updated value noe not 0?

Then why it is 10 only?

Thanks
1
0
then , finally what is the correct answer ?? 40 0r 10 ??
2
@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.
0
Thanks!!
0

wow nice explanation  sakharam  Thanks

Related questions

3 votes
1 answer
1
1 vote
6 answers
2
1 vote
1 answer
3
1.1k views
Consider the code fragment: count = 3; S1; Fork L1; L1: S3; S2; goto L3; S4; Fork L2; L2: S6; goto L3; S5; L3: join count S7 Which one of the following represents correct precedence graph of the above code fragment? the answer is given but I’m getting where am I wrong?
asked Jun 12, 2019 in Operating System aditi19 1.1k views
9 votes
3 answers
4
978 views
main() { if(fork()>=0) { printf("*"); if(fork()==0) { printf("*"); } else{ //do nothing } printf("*"); } How many number of times “*” will be printed?
asked Feb 27, 2019 in Operating System srestha 978 views
...