• edited by
58,321 views
196 196 votes
Consider the following code fragment:
if (fork() == 0)
{
   a = a + 5;
   printf("%d, %p n", a, &a);
}
else
{
   a = a - 5;
   printf ("%d, %p n", 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?

  1. $u = x + 10  \text{ and } v = y$
  2. $u = x + 10 \text{ and } v != y$
  3. $u + 10 = x \text{ and } v = y$
  4. $u + 10 = x \text{ and } v != y$

14 Answers

1 1 vote

 


Understanding Copy-on-Write in the Context of fork()

When a process calls fork(), the operating system creates a new process (child) by duplicating the memory space of the parent. However, to optimize performance, modern operating systems employ a technique known as Copy-on-Write (COW).

What is Copy-on-Write?

Copy-on-Write delays the actual copying of memory pages until one of the processes (parent or child) tries to modify a shared page. Initially, both processes share the same physical memory pages marked as read-only. When a process attempts to write to a shared page, a page fault is triggered. The OS then:

  • Allocates a new page,

  • Copies the contents of the original page to the new one,

  • Updates the process’s page table to point to the new writable page.

This way, memory is conserved, and copying overhead is avoided unless absolutely necessary.


Code Under Consideration

if (fork() == 0) {
   a = a + 5;
   printf("%d, %p\n", a, &a);
} else {
   a = a - 5;
   printf("%d, %p\n", a, &a);
}

Analysis

Let’s break it down with Copy-on-Write in mind:

  1. Before the fork(), there’s a variable a (assume it’s a global or stack variable).

  2. After fork(), both parent and child share the same virtual address space, including variable a, using COW.

  3. When either process tries to write to a, the OS creates a private copy for that process (due to COW), but the virtual address &a remains the same in both processes.

  4. Therefore:

    • Both the parent and child will print different values of a (one does a+5, the other a-5),

    • But both will print the same address for &a.

So the correct option is:

(C) Both processes print different values, but the same address.


Resource

Further reading: Copy-on-Write in Operating System - Studytonight


 

0 0 votes
Child will execute else part as it will be if(0) so it will be a-5 , parent will execute if part as it will be if(>0) so it will be a+5 so u = x+10 ...by fork call parent and child will have identical address space so v =y so answer is C
• edited by
0 0 votes
  • So these virtual addresses are translations of physical addresses and doesn't represent the same physical memory space, to leave a more practical example we can do a test, if we compile and run multiple times a program that displays the direction of a static variable, such as this program.

    #include <stdio.h>
    
    int main()
    {
    static int a = 0;
    
    printf("%p\n", &a);
    
    getchar();
    
    return 0;
    }

    It would be impossible to obtain the same memory address in two different programs if we deal with the physical memory directly.

    And the results obtained from running the program several times are...

enter image description here

 

 

Source : Stackoverflow.

 

0 0 votes

✅ Clean Explanation for the Question:

u + 10 = x because the parent subtracts 5 and child adds 5 to the same initial value of a.

So if a = 10, parent prints 5 and child prints 15, satisfying u + 10 = x.

✅ Hence, the correct option is: C. u + 10 = x and v = y.

Why v = y?

Even though parent and child modify the same variable a, they operate on separate copies of it because fork() creates a new process with its own memory space.

These copies are stored in different physical memory locations, but both processes receive the same virtual address for a.

📌 This virtual address is generated by the Operating System's Memory Management Unit (MMU).

Since C programs and users can only see and access virtual addresses, and the question does not mention physical memory, we consider only virtual addresses.

✅ So, in this question, the addresses of a appear the same, but the actual memory locations are different.

Therefore, the correct comparison is based on virtual addresses, and not physical ones.
 

Answer:
Position:
Show:

Related questions

197 197 votes
9 answers 9 answers
78.0k
78.0k views
Kathleen asked Sep 22, 2014
77,998 views
A $5$ stage pipelined CPU has the following sequence of stages:IF – instruction fetch from instruction memoryRD – Instruction decode and register readEX – Execute: ALU op...
32 32 votes
3 answers 3 answers
15.4k
15.4k views
gatecse asked Sep 21, 2014
15,373 views
Let $f(x)$ be the continuous probability density function of a random variable $x$, the probability that $a < x \leq b$, is :$f(b-a)$$f(b) - f(a)$$\int\limits_a^b f(x) dx...
72 72 votes
7 answers 7 answers
26.5k
26.5k views
Kathleen asked Sep 22, 2014
26,532 views
Suppose $n$ processes, $P_1, \dots P_n$ share $m$ identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process ...
48 48 votes
4 answers 4 answers
17.3k
17.3k views
Kathleen asked Sep 22, 2014
17,333 views
Normally user programs are prevented from handling I/O directly by I/O instructions in them. For CPUs having explicit I/O instructions, such I/O protection is ensured by ...