The Gateway to Computer Science Excellence
+3 votes
258 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 by (253 points)
retagged by | 258 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

by Active (3k points)
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 ??
+1
@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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,324 answers
198,405 comments
105,169 users