Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the following code fragment:if (fork... Start Learning for Free
 
Consider the following code fragment:
if (fork() == 0)
{ a = a + 5; printf("%d,%d", a, &a); }
else { a = a –5; printf("%d, %d", a, &a); }
Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?
  • a)
    u = x + 10 and v = y
  • b)
    u = x + 10 and v != y
  • c)
    u + 10 = x and v = y
  • d)
    u + 10 = x and v != y
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
Consider the following code fragment:if (fork() == 0){ a = a + 5; prin...
When a fork() system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process. But this is not needed in certain cases. When the child is needed just to execute a command for the parent process, there is no need for copying the parent process’ pages, since exec replaces the address space of the process which invoked it with the command to be executed. In such cases, a technique called copy-on-write (COW) is used. With this technique, when a fork occurs, the parent process’s pages are not copied for the child process. Instead, the pages are shared between the child and the parent process. Whenever a process (parent or child) modifies a page, a separate copy of that particular page alone is made for that process (parent or child) which performed the modification. This process will then use the newly copied page rather than the shared one in all future references.

fork() returns 0 in child process and process ID of child process in parent process.
In Child (x), a = a + 5
In Parent (u), a = a – 5;

Child process will execute the if part and parent process will execute the else part. Assume that the initial value of a = 6. Then the value of a printed by the child process will be 11, and the value of a printed by the parent process in 1. Therefore u+10=x Now the second part. The answer is v = y.



We know that, the fork operation creates a separate address space for the child. But the child process has an exact copy of all the memory segments of the parent process. Hence the virtual addresses and the mapping (initially) will be the same for both parent process as well as child process.
PS: the virtual address is same but virtual addresses exist in different processes’ virtual address space and when we print &a, it’s actually printing the virtual address. Hence the answer is v = y.
View all questions of this test
Most Upvoted Answer
Consider the following code fragment:if (fork() == 0){ a = a + 5; prin...
"The value of a in the child process is: %d\n", a); }
else
{ a = a + 5; printf("The value of a in the parent process is: %d\n", a); }

Assuming the initial value of "a" is 10, what will be the output of this code?

The output will be:
- "The value of a in the child process is: 15"
- "The value of a in the parent process is: 15"

This is because when the fork() system call is called, it creates a new process (the child process) that is an exact copy of the parent process. In this case, both processes will have their own copy of variable "a", with an initial value of 10.

When the child process executes, it will add 5 to its own copy of "a", resulting in a value of 15. The parent process will also add 5 to its own copy of "a", also resulting in a value of 15. Therefore, both processes will print the same value for "a" in their respective printf statements.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer?
Question Description
Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer?.
Solutions for Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer?, a detailed solution for Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? has been provided alongside types of Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following code fragment:if (fork() == 0){ a = a + 5; printf("%d,%d", a, &a); }else { a = a –5; printf("%d, %d", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?a)u = x + 10 and v = yb)u = x + 10 and v != yc)u + 10 = x and v = yd)u + 10 = x and v != yCorrect answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev