Unsorted

TODO What came before?

Semaphore Implementation with no Busy Waiting

TODO

Typedef struct {
    int value;
    struct process *list;
    } semaphore;

wait(semaphore *S) {
    S -> value--;
    if (S -> value < 0) {
        // add this process to S -> list
        sleep();
    }
}

signal(semaphore *S) {
    if (S -> value <= 0) {
        // remove process from S -> list
        wakeup(P);
    }
}

Basic system calls used:

Issues with Semaphores

Incorrect use of sempahore operations can cause deadlock and starvation.

Example: Misusing Semaphores

Interchanging Order

TODO

Replaces signal with wait

TODO

omits signal, wait, or both

TODO

Monitors

High-level ADT that encapsulates data along with functions though which the data may be accessed and manipulated

Purpose: Guarantee mutual exclusion.

monitor monitor_name
{
    // shared vars
    procedure P1 (...) {...}
    ...
    procedure Pn (...) {...}
        initialization code (...) {...}
}

Deadlock & Starvation

Deadlock: Two or more processes wait indefinitely for an event that can only be caused by only one of the waiting processes.

Starvation: Indefinite blocking

Priority Inversion: Scheduling problem when lower-priority process holds a lock needed by a higher-priority process.

Indivisibility: When we say a critical section must be indivisibly/atomically, we are talking about the code which holds and releases the lock.

Priority Inversion Protocol: We temporarily assign the highest priority to the process currently executing in its critical section to prevent it getting starved.

Classical Problems of Synchronization

Bounded Buffer Problem

Suppose you have n buffers, each can hold one item.

TODO Code

do {
    ...
    // produce iteme
    ...

}

Producer:

Consumer:

TODO Increment and decrement rules?

Readers and Writers

Dining Philosophers