Semaphores

A semaphore is a counter of tokens. sem_wait takes a token, and sleeps if none is left; sem_post puts one back, or hands it straight to a sleeper. Start it at 1 and it is a lock, at 0 and it orders two threads, at N and it counts N free slots. The tabs follow OSTEP Chapter 31 from that one idea to the dining philosophers.

Keyboard: ← → to step, Space to play or pause.

Examples follow OSTEP, Chapter 31, Semaphores. The restroom and waiting-room pictures come from Chapters 28 and 30.