Process Synchronization · Operating System
41 questions · asked in 23 of 27 years (2000–2026)
MCQ 34 · MSQ 4 · NAT 31-mark 22 · 2-mark 19
2000Questions per year2026
Questions 1–5 of 41
Consider three processes , and running identical code, as shown in the pseudocode below. and are two binary semaphores initialized to and , respectively. is a shared variable initialized to . Each line in the pseudocode is executed atomically.
Pseudocode of P1, P2, and P3
Which of the following patterns is/are possible to be generated as an outcome of the execution of these three processes?
Pseudocode of P1, P2, and P3
Wait(A);
Print(*);
X = X+1;
If (X == 2)
{
Print($);
Signal(B);
}
Signal(A);
Wait(B);
Print(#);
Signal(B); Assume that any of the three processes can start to execute first and context switching can happen between these processes at any arbitrary time and in any arbitrary order. Which of the following patterns is/are possible to be generated as an outcome of the execution of these three processes?
One or more options are correct.
Answer: (A), (B), (C)
Semaphore (initial value ) works as a mutex for the critical section. The section prints "*", increments and does the check. So the three processes enter this section one at a time. Exactly one of them, the second to enter, sees . That process prints "" and then signals . The third process cannot even try this section until the second one has released . This happens only after the second one prints "". So the global order is forced to be: , that is, two stars, then the dollar, then the third star. A pattern like (three stars before the dollar) can never occur.
After the dollar, semaphore has exactly one permit. This starts a chain. Each process that takes the permit with prints "". It then gives the next permit with its own , which lets the next process go. The remaining (third) star can come in at any point of this "" chain before that third process itself joins the chain. That means before any , after exactly one , or after exactly two symbols. It can never come after all three, because the third process must finish its own star and critical section before it can join the chain at all.
Answer: **$*###, **$#*##, and **$##*# are all possible; ***$### is impossible.
After the dollar, semaphore has exactly one permit. This starts a chain. Each process that takes the permit with prints "". It then gives the next permit with its own , which lets the next process go. The remaining (third) star can come in at any point of this "" chain before that third process itself joins the chain. That means before any , after exactly one , or after exactly two symbols. It can never come after all three, because the third process must finish its own star and critical section before it can join the chain at all.
Answer: **$*###, **$#*##, and **$##*# are all possible; ***$### is impossible.
Consider the following two threads and that update two shared variables and . Assume that initially . Though context switching between threads can happen at any time, each statement of or is executed atomically without interruption.
Which one of the following options lists all the possible combinations of values of and after both and finish execution?
Which one of the following options lists all the possible combinations of values of and after both and finish execution?
Answer: (A)
The final a is set by which a-update runs first. The final b is set by which b-update runs first.
- a: if T1 updates first, else .
- b: if T1 updates first, else .
- T1 then T2 gives (4, 4). T2 then T1 gives (3, 3).
- T1: a=a+1, T2: b=2b, T2: a=2a, T1: b=b+1 (4, 3).
- (3, 4) cannot happen. It needs T2's a-update before T1's a-update, and T1's b-update before T2's b-update. But T1 does a before b, and T2 does b before a, so this makes a cycle.
Answer: (a=4, b=4); (a=3, b=3); (a=4, b=3)
- a: if T1 updates first, else .
- b: if T1 updates first, else .
- T1 then T2 gives (4, 4). T2 then T1 gives (3, 3).
- T1: a=a+1, T2: b=2b, T2: a=2a, T1: b=b+1 (4, 3).
- (3, 4) cannot happen. It needs T2's a-update before T1's a-update, and T1's b-update before T2's b-update. But T1 does a before b, and T2 does b before a, so this makes a cycle.
Answer: (a=4, b=4); (a=3, b=3); (a=4, b=3)
Consider a multi-threaded program with two threads T1 and T2. The threads share two semaphores: s1 (initialized to 1) and s2 (initialized to 0). The threads also share a global variable x (initialized to 0). The threads execute the code shown below.
// code of T1
wait(s1);
x = x+1;
print(x);
wait(s2);
signal(s1);
// code of T2
wait(s1);
x = x+1;
print(x);
signal(s2);
signal(s1); Which of the following outcomes is/are possible when threads T1 and T2 execute concurrently?One or more options are correct.
Answer: (B), (C)
s1 acts as a mutex, so the thread that gets s1 first runs its part first.
If T1 runs first: T1 does wait(s1), sets x = 1 and prints 1. Then wait(s2) blocks because s2 = 0, and T1 still holds s1. T2 now blocks on wait(s1) deadlock. Only "1" is printed. So (C) is possible and (A) is not.
If T2 runs first: T2 does wait(s1), sets x = 1, prints 1, then does signal(s2) and signal(s1). Next T1 sets x = 2, prints 2, wait(s2) goes through (s2 = 1), and T1 does signal(s1). So (B) is possible. T2 never blocks, so (D) is not possible.
Answer: (B), (C)
If T1 runs first: T1 does wait(s1), sets x = 1 and prints 1. Then wait(s2) blocks because s2 = 0, and T1 still holds s1. T2 now blocks on wait(s1) deadlock. Only "1" is printed. So (C) is possible and (A) is not.
If T2 runs first: T2 does wait(s1), sets x = 1, prints 1, then does signal(s2) and signal(s1). Next T1 sets x = 2, prints 2, wait(s2) goes through (s2 = 1), and T1 does signal(s1). So (B) is possible. T2 never blocks, so (D) is not possible.
Answer: (B), (C)
Consider the two functions incr and decr shown below.
Suppose there are two implementations of the semaphore , as follows:
I-1: s is a binary semaphore initialized to 1.
I-2: s is a counting semaphore initialized to 2.
Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively.
Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively?
incr(){
wait(s);
X = X+1;
signal(s);
}
decr(){
wait(s);
X = X-1;
signal(s);
}
There are 5 threads each invoking incr once, and 3 threads each invoking decr once, on the same shared variable X. The initial value of X is 10.Suppose there are two implementations of the semaphore , as follows:
I-1: s is a binary semaphore initialized to 1.
I-2: s is a counting semaphore initialized to 2.
Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively.
Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively?
Answer: (C)
I-1 (binary semaphore = 1): Only one thread can be inside at a time, so every update is applied. This is the only possible result: .
I-2 (counting semaphore = 2): Two threads can be inside together, so some updates can be lost.
Lower limit: every value written to X is the value that thread read, plus 1 (incr) or minus 1 (decr). That value was itself written by an earlier thread (or is the initial 10). Following this chain back to 10, each thread appears at most once, so the chain has at most 3 decrements. So X can never end below .
7 can be reached by losing all 5 increments:
- One decr thread reads X = 10 and is then preempted.
- All 5 incr threads run one after another (using the second slot). X becomes 15.
- The first decr now writes . This wipes out all 5 increments.
- The other 2 decr threads run normally. X becomes 7.
So , and the pair is 12, 7.
Answer: (C)
I-2 (counting semaphore = 2): Two threads can be inside together, so some updates can be lost.
Lower limit: every value written to X is the value that thread read, plus 1 (incr) or minus 1 (decr). That value was itself written by an earlier thread (or is the initial 10). Following this chain back to 10, each thread appears at most once, so the chain has at most 3 decrements. So X can never end below .
7 can be reached by losing all 5 increments:
- One decr thread reads X = 10 and is then preempted.
- All 5 incr threads run one after another (using the second slot). X becomes 15.
- The first decr now writes . This wipes out all 5 increments.
- The other 2 decr threads run normally. X becomes 7.
So , and the pair is 12, 7.
Answer: (C)
Consider the following threads, executing on a single processor, synchronized using three binary semaphore variables, , operated upon using standard and . The threads can be context switched in any order and at any time.
Which initialization of the semaphores would print the sequence BCABCABCA ...?
| T1 | T2 | T3 |
|---|---|---|
|
|
|
Which initialization of the semaphores would print the sequence BCABCABCA ...?
Answer: (C)
Look at the code of each thread: T2 prints B. It waits on and then signals . T1 prints C. It waits on and then signals . T3 prints A. It waits on and then signals .
The output has to begin with B, so only T2 can run first. This needs and . After that, the chain B C A B goes on forever.
(If or were 1, A or C could be printed first.)
Answer:
The output has to begin with B, so only T2 can run first. This needs and . After that, the chain B C A B goes on forever.
(If or were 1, A or C could be printed first.)
Answer:
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems