1 votes 1 votes The value of z after the execution of the following program is void f(int x) { staticint z; z=z+x; } int main() { int y=10; fork(); fork(); f(y); return 0; } 1.30 2.20 3.40 4. 10 Programming in C zeal programming programming-in-c zeal2019 + – Prince Sindhiya asked Dec 16, 2018 • edited Mar 7, 2019 by ajaysoni1924 Prince Sindhiya 796 views answer comment Share Follow See all 17 Comments See all 17 17 Comments reply Shobhit Joshi commented Dec 16, 2018 reply Follow Share 40 ? 0 votes 0 votes anjali007 commented Dec 16, 2018 reply Follow Share 30? 0 votes 0 votes Shobhit Joshi commented Dec 16, 2018 reply Follow Share why 30 ? 0 votes 0 votes Prince Sindhiya commented Dec 16, 2018 reply Follow Share 10 is answer given 0 votes 0 votes anjali007 commented Dec 16, 2018 reply Follow Share @Shobhit Joshi AS the function will be called 3 times so it will be 10+10+10=30. No of function calls=2^n-1 0 votes 0 votes anjali007 commented Dec 16, 2018 reply Follow Share @Prince Sindhiya if it would just have been int x then it could be 10.. what were u getting? 0 votes 0 votes Shobhit Joshi commented Dec 16, 2018 reply Follow Share @anjali007 the number of child process created are 2^n - 1.There is also 1 parent process. 0 votes 0 votes anjali007 commented Dec 16, 2018 reply Follow Share @Shobhit Joshi I think it will be called 3 times only.. can u pls elaborate a bit 0 votes 0 votes Utkarsh Joshi commented Dec 16, 2018 reply Follow Share there will be separate address space for all the children as they are modifying a variable. 0 votes 0 votes Subham Nagar commented Dec 16, 2018 i edited by Subham Nagar Dec 16, 2018 reply Follow Share @anjali007 why are we not counting 4 function calls as 1 parent process is also there? 0 votes 0 votes Shobhit Joshi commented Dec 16, 2018 reply Follow Share @anjali007 The function call does not depends on fork, fork is just creating a new child process, can you show me how you think the program flow will work. 0 votes 0 votes Shobhit Joshi commented Dec 16, 2018 reply Follow Share @Subham Nagar I am counting 4 function calls that's why i said answer will be 40. 0 votes 0 votes anjali007 commented Dec 16, 2018 reply Follow Share @Shobhit Joshi got it. thanks!! I got confused btw no of children and func calls.. 0 votes 0 votes Prince Sindhiya commented Dec 16, 2018 reply Follow Share @utkarsh bro can u explain 0 votes 0 votes Prince Sindhiya commented Dec 16, 2018 reply Follow Share @UTKARSH I GOT 40 AND THIS IS EXPLANATION GIVEN This is because z exists in separate processes, separate virtual address spaces. Changing one process won’t affect the other process. 0 votes 0 votes Utkarsh Joshi commented Dec 16, 2018 reply Follow Share Prince Sindhiya 10 should be the correct answer. Fork is implemented using copy on write mechanism. each child will have its own address space. So updation by one process won't affect other. please read this copy on write method from galvin. :) 0 votes 0 votes Prince Sindhiya commented Dec 16, 2018 reply Follow Share Ok Utkarsh I will see it in Galvin I am facing problem in this particular topic, and i wanted to ask this question that from where to refer it 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes As the function $f(10)$ is called $4$ times and the variable $z$ is static. Initially, $z=0$ On first function call, $z=0+10=10$ On second call, $z=10+10=20$ On third call, $z=20+10=30$ On fourth call, $z=30+10=40$ So, answer should be $40$ Shobhit Joshi answered Dec 16, 2018 Shobhit Joshi comment Share Follow See 1 comment See all 1 1 comment reply Rohit9846 commented Sep 26, 2019 reply Follow Share No, The correct answer is 10. When we call the fork() the executing program gets replicated which means every child and parent process (program under execution) has a data segment. So, the value of z=0;initially and when parent process terminates the value becomes 10 irrespective of what the child process holds in their own data segment. 0 votes 0 votes Please log in or register to add a comment.