Skip to content
Snippets Groups Projects
Commit 9389f0a5 authored by Florent Gluck's avatar Florent Gluck
Browse files

Added 03-Semaphores.md

parent e7266852
Branches master
No related tags found
No related merge requests found
---
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/)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment