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 Operating System fork-system-call operating-system test-series + – sumedha bhatia asked Sep 5, 2018 • retagged Jul 8, 2019 by Cristine sumedha bhatia 1.5k views answer comment Share Follow See all 3 Comments See all 3 3 Comments reply SHUBHAM SHASTRI commented Sep 5, 2018 reply Follow Share is it 40 ?? 0 votes 0 votes sumedha bhatia commented Sep 5, 2018 reply Follow Share The given answer is 10. 0 votes 0 votes awaneesh commented Sep 5, 2018 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.
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 sakharam answered Sep 5, 2018 • selected Sep 5, 2018 by sumedha bhatia sakharam comment Share Follow See all 14 Comments See all 14 14 Comments reply Shaik Masthan commented Sep 5, 2018 reply Follow Share is it global variables also separate for child process and parent process? 1 votes 1 votes Magma commented Sep 5, 2018 reply Follow Share sakharam But here Z is declare as a static variable 0 votes 0 votes Shaik Masthan commented Sep 5, 2018 reply Follow Share @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 votes 0 votes Magma commented Sep 5, 2018 reply Follow Share No NO Shaik Masthan you're totally right 0 votes 0 votes Mayankprakash commented Sep 5, 2018 reply Follow Share @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. 0 votes 0 votes sakharam commented Sep 5, 2018 reply Follow Share 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 2 votes 2 votes sakharam commented Sep 5, 2018 reply Follow Share @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 votes 0 votes Magma commented Sep 5, 2018 reply Follow Share 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 votes 0 votes Mayankprakash commented Sep 5, 2018 reply Follow Share 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 0 votes 0 votes Shaik Masthan commented Sep 5, 2018 reply Follow Share yes, even global variables are also separate https://www.geeksforgeeks.org/fork-memory-shared-bw-processes-created-using/ 1 votes 1 votes Magma commented Sep 5, 2018 reply Follow Share then , finally what is the correct answer ?? 40 0r 10 ?? 0 votes 0 votes sakharam commented Sep 5, 2018 i edited by sakharam Sep 5, 2018 reply Follow Share @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 votes 3 votes Mayankprakash commented Sep 5, 2018 reply Follow Share Thanks!! 0 votes 0 votes Magma commented Sep 5, 2018 reply Follow Share wow nice explanation sakharam Thanks 0 votes 0 votes Please log in or register to add a comment.