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 6–10 of 41
Consider the following pseudocode, where S is a semaphore initialized to 5 in line #2 and counter is a shared variable initialized to 0 in line #1. Assume that the increment operation in line #7 is not atomic.
1. int counter =0;
2. Semaphore S= init(5);
3. void parop(void)
4. {
5. wait(S);
6. wait(S);
7. counter++;
8. signal(S);
9. signal(S);
10. } If five threads execute the function concurrently, which of the following program behavior(s) is/are possible?One or more options are correct.
Answer: (A), (B), (D)
Every thread takes 2 of the 5 permits, so at most 2 threads can be inside at the same time. Also, counter++ is not atomic (read, add, write).
- (A) 5 is possible: the threads run one after another.
- (B) 1 is possible: thread T1 reads 0 and is preempted. The other four threads then run one at a time and counter becomes 4. After that, T1 writes .
- (C) 0 is impossible: every thread writes (value it read) + 1, which is at least 1, so counter can never be 0 at the end.
- (D) Deadlock is possible: all 5 threads do their first wait (S becomes 0), and then all of them block on the second wait.
Answer: (A), (B), (D)
- (A) 5 is possible: the threads run one after another.
- (B) 1 is possible: thread T1 reads 0 and is preempted. The other four threads then run one at a time and counter becomes 4. After that, T1 writes .
- (C) 0 is impossible: every thread writes (value it read) + 1, which is at least 1, so counter can never be 0 at the end.
- (D) Deadlock is possible: all 5 threads do their first wait (S becomes 0), and then all of them block on the second wait.
Answer: (A), (B), (D)
Consider a computer system with multiple shared resource types, with one instance per resource type. Each instance can be owned by only one process at a time. Owning and freeing of resources are done by holding a global lock (L). The following scheme is used to own a resource instance:
function OWNRESOURCE(Resource R)
Acquire lock L // a global lock
if R is available then
Acquire R
Release lock L
else
if R is owned by another process P then
Terminate P, after releasing all resources owned by P
Acquire R
Restart P
Release lock L
end if
end if
end functionWhich of the following choice(s) about the above scheme is/are correct?One or more options are correct.
Answer: (A), (B), (C)
- (A) The owner is terminated instead of making the new process wait, so no process ever waits while holding resources. A circular wait cannot form, so there is no deadlock. True.
- (B) Two processes can keep terminating and restarting each other, and neither makes progress, so livelock is possible. True.
- (C) One process can be terminated again and again, so it may never finish, so starvation is possible. True.
- (D) A resource still has only one owner at any time, because the lock L guards owning and freeing. So mutual exclusion holds. False.
Answer: (A), (B), (C)
- (B) Two processes can keep terminating and restarting each other, and neither makes progress, so livelock is possible. True.
- (C) One process can be terminated again and again, so it may never finish, so starvation is possible. True.
- (D) A resource still has only one owner at any time, because the lock L guards owning and freeing. So mutual exclusion holds. False.
Answer: (A), (B), (C)
Each of a set of n processes executes the following code using two semaphores a and b initialized to 1 and 0, respectively. Assume that count is a shared variable initialized to 0 and not used in CODE SECTION P.
What does the code achieve?
CODE SECTION P
wait(a); count=count+1;
if (count==n) signal(b);
signal(a); wait(b); signal(b);
CODE SECTION QWhat does the code achieve?
Answer: (A)
Semaphore a is used to guard count. Semaphore b starts at 0.
- Every process adds 1 to count after it finishes CODE SECTION P, and then it waits on b.
- Only the n-th (last) process does signal(b). After that, each process that gets past wait(b) does signal(b) again, so the next process can pass.
So no process goes into CODE SECTION Q until all n processes have finished P. This works as a barrier.
Answer: No process executes CODE SECTION Q before every process has finished CODE SECTION P
- Every process adds 1 to count after it finishes CODE SECTION P, and then it waits on b.
- Only the n-th (last) process does signal(b). After that, each process that gets past wait(b) does signal(b) again, so the next process can pass.
So no process goes into CODE SECTION Q until all n processes have finished P. This works as a barrier.
Answer: No process executes CODE SECTION Q before every process has finished CODE SECTION P
Consider three concurrent processes P1, P2 and P3 as shown below, which access a shared variable D that has been initialized to 100.
The process are executed on a uniprocessor system running a time-shared operating system. If the minimum and maximum possible values of D after the three processes have completed execution are X and Y respectively, then the value of Y-X is __________.
The process are executed on a uniprocessor system running a time-shared operating system. If the minimum and maximum possible values of D after the three processes have completed execution are X and Y respectively, then the value of Y-X is __________.
Answer: 80
Each update is really three steps: load D, change it, store D. So one process can overwrite the results of the others.
- Maximum: the update of P2 is lost. P1 reads 100. Then P2 runs fully (D = 50). P1 then writes 120, and P3 makes it 130. So .
- Minimum: both additions are lost. P2 reads 100. P1 and P3 then run fully (D = 130). P2 then writes 100 50 = 50. So .
Answer: 80
- Maximum: the update of P2 is lost. P1 reads 100. Then P2 runs fully (D = 50). P1 then writes 120, and P3 makes it 130. So .
- Minimum: both additions are lost. P2 reads 100. P1 and P3 then run fully (D = 130). P2 then writes 100 50 = 50. So .
Answer: 80
Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is N. Three semaphores empty, full and mutex are defined with respective initial values of 0, N and 1. Semaphore empty denotes the number of available slots in the buffer, for the consumer to read from. Semaphore full denotes the number of available slots in the buffer, for the producer to write to. The placeholder variables, denoted by P, Q, R, and S, in the code below can be assigned either empty or full. The valid semaphore operations are: wait() and signal().
Which one of the following assignments to P, Q, R and S will yield the correct solution?
Producer: Consumer:
do{ do{
wait(P); wait(R);
wait(mutex); wait(mutex);
//Add item to buffer //Consume item from buffer
signal(mutex); signal(mutex);
signal(Q); signal(S);
}while(1); }while(1); Which one of the following assignments to P, Q, R and S will yield the correct solution?
Answer: (C)
In this question, "full" is the count of free slots, and it starts at N. "empty" is the count of filled slots, and it starts at 0.
- Producer: first wait on a free slot (P = full). Then add the item. Then signal that a filled slot exists (Q = empty).
- Consumer: first wait on a filled slot (R = empty). Then remove the item. Then signal that a free slot exists (S = full).
Answer: P: full, Q: empty, R: empty, S: full
- Producer: first wait on a free slot (P = full). Then add the item. Then signal that a filled slot exists (Q = empty).
- Consumer: first wait on a filled slot (R = empty). Then remove the item. Then signal that a free slot exists (S = full).
Answer: P: full, Q: empty, R: empty, S: full
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems