Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 437 Bytes

File metadata and controls

41 lines (31 loc) · 437 Bytes

Semaphore

Binary Semaphores

int S = 1;

void wait() {
    while (S <= 0);
    S--;
}

void signal() {
    S++;
}

Counting Semaphores

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

void wait(semaphore *s) {
    s->value--;
    if (s->value < 0) {
        block(s->list);
    }
}

void signal(semaphore *s) {
    s->value++;
    if (s->value <= 0) {
        wakeup(s->list);
    }
}