in Operating System retagged ago by
4,725 views
11 votes
11 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.$
in Operating System retagged ago by
by
4.7k views

4 Comments

Hmm , you’re correct . During exam I was thinking that global variables are shared across C files, but then one has to use extern for that.
0
0
I have marked A and C?? Is this crrct???
0
0
Let’s wait for someone to answer, i wasted some time on this , and it costed me.
0
0

3 Answers

21 votes
21 votes
Best answer

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
by

4 Comments

@samarpita 

When P1 enters into main section, it create two child thread T1 and T2 - that means total 3.

Given that waiting is removed... So the parent process can immediately executes print(x) - at that time it print zero

0
0
edited by

@Shaik Masthan sir got my mistake...actually, I was thinking that the main thread will also execute foo() ..so x will be anyways 1 or 2..but only thread1 and thread2 are executing the foo()...So, sir basically main(actual) thread will not wait for the complete execution of two threads and print x=0.

1
1
Yes.
0
0
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]

3 Comments

option d should be correct

As both threads will have their own variable y

variable x & L1 will be shared by both threads as they’re global

1
1
yes, I missed that out thanks for correcting !
0
0
Each thread has it’s own stack, program counter the resources they share are heaps, code segment, access to files, devices...
1
1
0 votes
0 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