Operating Systems: Dining Philosophers


Allowing at most N-1 philosophers to be sitting

semaphore Chopstick[N] = 1,
    Sitting = N-1;

Philosopher(Index):

do {
    wait(Sitting)
    wait(Chopstick[Index])
    wait(Chopstick[(Index+1) % N)
    Eat
    signal(Chopstick[(Index+1) % N)
    signal(Chopstick[Index])
    signal(Sitting)
    Think
    } forever