GATE CS 2021 (Afternoon) — Question 52

Operating Systems (OS)Concurrency & Synchronization (Semaphores, Mutex, Monitors, Locks)MSQ2 marksMedium

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\):
c
1int x = 0;    // global
2Lock L1;      // global
3main( ) {
4    create a thread to execute foo( ); // Thread T1
5    create a thread to execute foo( ); // Thread T2
6    wait for the two threads to finish execution;
7    print (x);
8}
9foo( ) {
10    int y = 0;
11    Acquire L1;
12    x = x + 1;
13    y = y + 1;
14    Release L1;
15    print (y);
16}
17
Which of the following statement(s) is/are correct?
  1. A
    Both \(P_1\) and \(P_2\) will print the value of \(x\) as 2.
  2. B
    At least one of \(P_1\) and \(P_2\) will print the value of \(x\) as 4.
  3. C
    At least one of the threads will print the value of \(y\) as 2.
  4. D
    Both \(T_1\) and \(T_2\), in both the processes, will print the value of \(y\) as 1.
Practise the full 2021-AN paper
Challenge a friend: WhatsApp Telegram
Recommended book · affiliate linkOperating System Concepts (Galvin)Scheduling, synchronisation, deadlocks, memory and file systems.
Practise more on Concurrency & Synchronization (Semaphores, Mutex, Monitors, Locks)Timed, in the real exam interface · free to start

A GATE question every morning

One real past-paper question every morning for your branch, answer hidden until you tap. Free on Telegram.

Join
Stuck? Ask in the student group