GATE CS 2021 Set 2 | Question 52

Operating Systems › Process Synchronization · MSQ · 2 marks · Answer: A, D

  1. Question 52Operating Systemsmsq2 marks
    Consider the following multi-threaded code segment (in a mix of C and pseudo-code), invoked by two processes P1 and P2, and each of the processes spawns two threads T1 and T2:
    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?
    • A.
      Both P1 and P2 will print the value of x as 2.
      ✓
    • B.
      At least one of P1 and P2 will print the value of x as 4.
    • C.
      At least one of the threads will print the value of y as 2.
    • D.
      Both T1 and T2, in both the processes, will print the value of y as 1.
      ✓

    Answer: A, D

    Solution

    Processes have separate address spaces, so each process's global x is incremented by its own two threads (under the lock) to 2 — (A) true, and (B) false (x is never shared to reach 4). y is a local variable, always ending at 1 — (D) true, (C) false.

More GATE PYQ on Process Synchronization

All GATE CS Operating Systems PYQ →

Attempt GATE CS 2021 Set 2 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →