diff --git a/course/03-Semaphores.md b/course/03-Semaphores.md new file mode 100644 index 0000000000000000000000000000000000000000..ac9c59f8a448b2c5b769fb60320fc290c069d57b --- /dev/null +++ b/course/03-Semaphores.md @@ -0,0 +1,133 @@ +--- +author: Florent Gluck - Florent.Gluck@hesge.ch + +title: Semaphores + +date: \vspace{.5cm} \footnotesize \today + +pandoc-latex-fontsize: + - classes: [tiny] + size: tiny + - classes: [verysmall] + size: scriptsize + - classes: [small] + size: footnotesize + - classes: [huge, important] + size: huge +--- + +[//]: # ---------------------------------------------------------------- +## What is a semaphore? + +- A semaphore is a **synchronization mechanism** between threads (or processes) +- Used to synchronize the execution of threads (or processes) +- Waiting on a semaphore is a **passive** operation +- Invented by E.W. Dijkstra in 1965 + +[//]: # ---------------------------------------------------------------- +## Anatomy of a semaphore + +A semaphore can be seen as an integer variable associated with two operations: + + - incrementation + - decrement + +These two operations are **atomic** + +[//]: # ---------------------------------------------------------------- +## Using a semaphore + +- A semaphore is **shared** by several threads/processes +- A semaphore is **always** initialized to an integer value $\geq$ 0 +- The only possible operations are **incrementation** and **decrementation** +- In general, the value of a semaphore **cannot** be determined + +[//]: # ---------------------------------------------------------------- +## Behavior of a semaphore + +- When a thread **decrements** a semaphore, if the result is **< 0**, the thread is **suspended** +- When a thread **increments** a semaphore, if one or more threads are suspended on the semaphore, then **one thread is awakened** and continues its execution + - it is **impossible to determine** in advance which thread will be woken up + +[//]: # ---------------------------------------------------------------- +## The meaning of a semaphore's value + +- Value > 0: + - represents the number of threads that can decrement the semaphore without being suspended +- Value < 0: + - represents the number of threads suspended on this semaphore +- Value == 0: + - means that no thread is suspended, but that if a thread decrements the semaphore, then the thread will be suspended + +[//]: # ---------------------------------------------------------------- +## Properties of semaphore + +- Before decrementing a semaphore, there is **no way of knowing** whether the thread will be suspended or not +- After incrementing a semaphore, another thread is awakened and the two threads **run concurrently** + - **no way of knowing** which thread will immediately continue +- When a semaphore is incremented, there's **no way of knowing** whether a thread is already suspended + +[//]: # ---------------------------------------------------------------- +## Semaphore terminology + +Other terminologies exist besides increment/decrement: + +- P/V +- signal/wait +- post/wait + +[//]: # ---------------------------------------------------------------- +## Semaphores with POSIX.1-2001 + +- Header file: `semaphore.h` +- Type: `sem_t` +- Fonctions: + - `sem_init` + - `sem_destroy` + - `sem_wait` + - `sem_post` + +[//]: # ---------------------------------------------------------------- +## Usage example + +```{.tiny .c} +// Errors are purposedly ignored +#include <pthread.h> +#include <semaphore.h> + +#define SEM_THREAD 0 // use 1 for inter-process sempahores + +static sem_t sem; + +static void *f1(void *data) { + sem_wait(&sem); + puts("f1: I'm always last :-("); +} + +static void *f2(void *data) { + puts("f2: I'm always first!"); + sem_post(&sem); +} + +int main() { + sem_init(&sem, SEM_THREAD, 0); + pthread_t t1, t2; + pthread_create(&t1, NULL, f1, NULL); + pthread_create(&t2, NULL, f2, NULL); + pthread_join(t1, NULL); + pthread_join(t2, NULL); + sem_destroy(&sem); + return 0; +} +``` + +[//]: # ---------------------------------------------------------------- +## Resources + +\small +- Man pages\ +\footnotesize `man sem_overview` + +\small +- Operating Systems: Three Easy Pieces, Remzi H. and Andrea C. Arpaci-Dusseau. Arpaci-Dusseau Books\ +\footnotesize [\textcolor{myblue}{http://pages.cs.wisc.edu/~remzi/OSTEP/}](http://pages.cs.wisc.edu/~remzi/OSTEP/)