Semaphores with wait queues

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 m semaphores numbered from 0, with the given starting values. Processes have ids from 1 to 9.
  • P id s: if process id is blocked, print id is blocked and do nothing. Otherwise, if semaphore s has a value above 0, it goes down by 1 and you print id passes s; if the value is 0, the process joins the back of the queue of s, becomes blocked, and you print id blocks on s.
  • V s: if the queue of s has a process, the one at the front leaves the queue, stops being blocked, and you print id wakes on s; the value does not change. Otherwise the value goes up by 1 and you print s becomes v with the new value v.
  • At the end print blocked: followed by the ids of the processes still blocked in increasing order, or blocked: 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

Example 1 · Input
2
1 0
6
P 1 0
P 2 0
P 3 1
V 0
V 1
V 0
Expected 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
Example 2 · Input
1
0
3
P 4 0
P 4 0
V 0
Expected output
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

Lesson: Synchronization: locks and semaphores →