A real semaphore keeps a queue of the processes that are blocked on it. A signal wakes the one at the front of the queue, so processes are served in the order they asked.
What to build
Run a list of wait (P) and signal (V) operations on several semaphores and print what each one does.
Requirements
- There are
msemaphores numbered from 0, with the given starting values. Processes have ids from 1 to 9. P id s: if processidis blocked, printid is blockedand do nothing. Otherwise, if semaphoreshas a value above 0, it goes down by 1 and you printid passes s; if the value is 0, the process joins the back of the queue ofs, becomes blocked, and you printid blocks on s.V s: if the queue ofshas a process, the one at the front leaves the queue, stops being blocked, and you printid wakes on s; the value does not change. Otherwise the value goes up by 1 and you prints becomes vwith the new valuev.- At the end print
blocked:followed by the ids of the processes still blocked in increasing order, orblocked: none.
Input
The first line has m (1 ≤ m ≤ 5). The second line has the m starting values (0 to 5). The third line has q (1 ≤ q ≤ 40), followed by q operations, one per line: P id s or V s.
Output
One line for each operation, then the blocked: line.
Constraints
A woken process does not repeat its P: the wake-up itself completes it.
Example
Input:
2
1 0
6
P 1 0
P 2 0
P 3 1
V 0
V 1
V 0
Output:
1 passes 0
2 blocks on 0
3 blocks on 1
2 wakes on 0
3 wakes on 1
0 becomes 1
blocked: none
Why it works
The queue gives fairness: blocked processes wake in the order they blocked. A blocked process cannot run, so it cannot issue another operation, which is why the program refuses them. Keeping the value unchanged on a wake-up hands the resource straight to the woken process, so no other process can slip in and take it.
Examples
2 1 0 6 P 1 0 P 2 0 P 3 1 V 0 V 1 V 0
1 passes 0 2 blocks on 0 3 blocks on 1 2 wakes on 0 3 wakes on 1 0 becomes 1 blocked: none
1 0 3 P 4 0 P 4 0 V 0
4 blocks on 0 4 is blocked 4 wakes on 0 blocked: none
Hints
Hint 1
Keep for each semaphore a value and a FIFO queue of blocked process ids (an array and a head and tail index will do).
Hint 2
Keep blocked[id] for each process. An operation by a process that is blocked is refused and changes nothing.
Hint 3
V with a non-empty queue removes the first id, clears its blocked flag and keeps the value. V with an empty queue adds 1 to the value.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int m, q, id, s, value[5], queue[5][50], head[5] = {0}, tail[5] = {0}, blocked[10] = {0};
char op;
scanf("%d", &m);
for (int i = 0; i < m; i++) scanf("%d", &value[i]);
scanf("%d", &q);
for (int k = 0; k < q; k++) {
scanf(" %c", &op);
if (op == 'P') {
scanf("%d %d", &id, &s);
if (blocked[id]) {
printf("%d is blocked\n", id);
} else if (value[s] > 0) {
value[s]--;
printf("%d passes %d\n", id, s);
} else {
queue[s][tail[s]++] = id;
blocked[id] = 1;
printf("%d blocks on %d\n", id, s);
}
} else {
scanf("%d", &s);
if (head[s] < tail[s]) {
int woken = queue[s][head[s]++];
blocked[woken] = 0;
printf("%d wakes on %d\n", woken, s);
} else {
value[s]++;
printf("%d becomes %d\n", s, value[s]);
}
}
}
int any = 0;
printf("blocked:");
for (int i = 1; i < 10; i++) {
if (blocked[i]) {
printf(" %d", i);
any = 1;
}
}
if (!any) printf(" none");
printf("\n");
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step