Computer Science Notes

Notes From CS Undergrad Courses FSU

This project is maintained by awa03

Deadlocks

/*
    Possible Deadlock, not always
*/

// Thread A
P(x);
P(y)

// Thread B
P(y)
P(x);

Why they are important

Dining Lawyer Problem

semaphore chopstick[5] = {1, 1, 1, 1, 1};
lawyer(int j){
    while(TRUE){
        // attempt to grab sticks
        P(chopstick[j]);
        P(chopstick[(j + 1) % 5]);

        eat();

        // release chopstick
        V(chopstick[(j + 1) % 5]);
        V(chopstick[j]);
    }
}

Conditions

Deadlock Prevention