Dispatcher events

The kernel moves a process between states in response to events: the dispatcher starts it, the timer stops it, an I/O request blocks it, and an I/O completion wakes it. Not every event makes sense at every moment.

What to build

Apply a list of events to n processes on one CPU, and print the final state of each process.

Requirements

  • All n processes start in the ready state, and the CPU is idle.
  • run p: allowed only when the CPU is idle and process p is ready. It becomes running. timeout: the running process becomes ready. block: the running process becomes waiting. exit: the running process becomes terminated. wake p: process p, if it is waiting, becomes ready.
  • An event that is not allowed prints event K: not allowed (K counts events from 1) and changes nothing.
  • After all events print Pi: state for each process, then context switches: C, the number of run events that were allowed.

Input

The first line has n (1 ≤ n ≤ 8) and e (1 ≤ e ≤ 40). Then e events, one per line: run p, timeout, block, exit or wake p (processes are numbered from 1).

Output

First the not allowed lines in order, then n lines Pi: ready, Pi: running, Pi: waiting or Pi: terminated, then the context switches: line.

Constraints

A process number is always between 1 and n.

Example

Input:

2 6
run 1
block
run 2
timeout
wake 1
run 1

Output:

P1: running
P2: ready
context switches: 3

Why it works

Each rule is a row of the state diagram from the lesson: only the running process can time out, block or exit, and only a waiting process can be woken. Counting the allowed run events counts the context switches, which is the overhead the kernel wants to keep small.

Examples

Example 1 · Input
2 6
run 1
block
run 2
timeout
wake 1
run 1
Expected output
P1: running
P2: ready
context switches: 3
Example 2 · Input
3 3
timeout
run 3
run 1
Expected output
event 1: not allowed
event 3: not allowed
P1: ready
P2: ready
P3: running
context switches: 1

Hints

Hint 1

Keep an array state[] with a number for each process, and a variable running that holds the running process or -1 when the CPU is idle.

Hint 2

For each event decide first whether it is allowed. If not, print the error line and change nothing.

Hint 3

Read the event word with scanf, then read the process number only for run and wake.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>

int main(void) {
    int n, e, p, state[9];   /* 0 ready, 1 running, 2 waiting, 3 terminated */
    int running = -1, switches = 0;
    char word[16];
    const char *names[4] = {"ready", "running", "waiting", "terminated"};
    scanf("%d %d", &n, &e);
    for (int i = 1; i <= n; i++) state[i] = 0;
    for (int k = 1; k <= e; k++) {
        scanf("%15s", word);
        int ok = 0;
        if (strcmp(word, "run") == 0) {
            scanf("%d", &p);
            if (running < 0 && state[p] == 0) {
                state[p] = 1;
                running = p;
                switches++;
                ok = 1;
            }
        } else if (strcmp(word, "wake") == 0) {
            scanf("%d", &p);
            if (state[p] == 2) {
                state[p] = 0;
                ok = 1;
            }
        } else if (running >= 0) {
            ok = 1;
            if (strcmp(word, "timeout") == 0) state[running] = 0;
            else if (strcmp(word, "block") == 0) state[running] = 2;
            else state[running] = 3;
            running = -1;
        }
        if (!ok) printf("event %d: not allowed\n", k);
    }
    for (int i = 1; i <= n; i++) printf("P%d: %s\n", i, names[state[i]]);
    printf("context switches: %d\n", switches);
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Processes →