retagged by
10,119 views
25 votes
25 votes

​​​​​​Consider the following multi-threaded code segment (in a mix of C and pseudo-code), invoked by two processes $P_1$ and $P_2$, and each of the processes spawns two threads $T_1$ and $T_2$:

int x = 0;  // global
Lock L1;    // global
main () { 
    create a thread to execute foo(); // Thread T1
    create a thread to execute foo(); // Thread T2
    wait for the two threads to finish execution;
    print(x);}
    
    
foo() {
    int y = 0;
    Acquire L1;
    x = x + 1;
    y = y + 1;
    Release L1;
    print (y);}

Which of the following statement(s) is/are correct?

  1. Both $P_1$ and $P_2$ will print the value of $x$ as $2.$
  2. At least of $P_1$ and $P_2$ will print the value of $x$ as $4.$
  3. At least one of the threads will print the value of $y$ as $2.$
  4. Both $T_1$ and $T_2$, in both the processes, will print the value of $y$ as $1.$
retagged by

4 Answers

Best answer
31 votes
31 votes

Each process has its own address space.

  1. $P_1:$

    Two threads $T_{11}, T_{12}$ are created in main.

    Both execute foo function and threads don’t wait for each other. Due to explicit locking mechanism here mutual exclusion is there and hence no race condition inside foo().

    $y$ being thread local, both the threads will print the value of $y$ as $1.$

    Due to the wait in main, the print(x) will happen only after both the threads finish. So, $x$ will have become $2.$ 

    PS: Even if $x$ was not assigned $0$ explicitly in C all global and static variables are initialized to $0$ value.

  2. $P_2:$ 

    Same thing happens here as $P_1$ as this is a different process. For sharing data among different processes mechanisms like shared memory, files, sockets etc must be used. 

So, the correct answer here is A and D.


  • Suppose wait is removed from the main(). Then the possible $x$ values can be $0,1,2$ as the main thread as well as the two created threads can execute in any order.
  • Suppose locking mechanism is removed from foo() and assignments are not atomic. (If increment is atomic here, then locking is not required). Then race condition can happen and so one of the increments can overwrite the other. So, in main, $x$ value printed can be either $1$ or $2$. 
  • Now suppose we had just one process which does a fork() inside main before creating the threads. How the answer should change?
selected by
8 votes
8 votes
Let’s eliminate and reach to the answer-
A. True, Both P1 and P2 would print x as 2, this is necessarily true as both the processes have their own copy of variables.
B. False, for the same reason mentioned above
C. False, Since y is an auto variable hence both the threads would have there own copy of variable “y” which they would increment to 1 hence both the threads would print y as 1 and not 2.
D. True, For the reason mentioned above (C)
Hence answer happens to be only A,D [MSQ]
1 votes
1 votes
Statement A now if we look at the code and read properly question is saying both process execute this code so once when it is executed by process 1 x will be initialized to 0 then both threads of process 1 will make x as 2. now again process 2 will initialize x as 0 don’t confuse yourself by thinking that x is global and just initialized to 0 only once. No x will be initialized to 0 2 times once by P1 and one by P2 so yes both of them will print x as 2 when both threads execute in each process. So A is right now coming to option D y is local variable so every time whenever any thread executes it is initialized to 0. So total 4 times y is initialized to 0. and every time each thread make value of y as 1 and then new thread again initialize it to 0 and again makes 1 so yes both threads in both process makes y as 1. So A and D is correct.
Answer:

Related questions