作業系統›Ch6 行程同步(Process Synchronization)
第 17 題/共 32 題
◀ OS 17/32
17. Monitor、Dining Philosophers
#OS-06-017易MonitorDining Philosophers
題組題幹(本題:第25題,共 3 小題)點擊展開

Consider the following monitor solution to the 5 dining philosophers problem where there are 5 chopsticks between 5 philosophers who sit on a round table. A philosopher needs to get both chopsticks on the right and left sides to eat. Suppose the index ii is incremented counterclockwise. With the monitor defined below, each philosopher ii invokes the operations pickup() and putdown() in the following sequence:

DiningPhilosophers.pickup(i);
    EAT
DiningPhilosophers.putdown(i);
monitor DiningPhilosophers
{
    enum { THINKING, HUNGRY, EATING } state[5];
    condition self[5];
    void pickup(int i) {
        state[i] = HUNGRY;
        test(i);
        if (state[i] != HUNGRY) self[i].wait();
    }

    void putdown(int i) {
        state[i] = THINKING;
        test((i + 4) % 5);
        test((i + 1) % 5);
    }

    void test(int i) {
        if ((state[(i + 4) % 5] != EATING) &&
            (state[i] == HUNGRY) &&
            (state[(i + 1) % 5] != EATING)) {
                state[i] = EATING;
                self[i].signal();
        }
    }

    initialization_code() {
        for (int i = 0; i < 5; i++)
            state[i] = THINKING;
    }
}

What is the purpose of the function test(i)?

📄 交大110
跳轉到第題
▤完整推導請見《WH 資工筆記 · 作業系統》Ch6 行程同步(Process Synchronization)
本章題號 · 1–20 / 32