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.4k 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 Show 11 previous comments 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.