Table of Contents

Semaphores execution order

Return to Operating Systems Course

Which are the possible execution orders of these three threads executed concurrently?

sem_t s1, s2, s3;
sem_init(&s1, 0, 2);	sem_init(&s2, 0, 0);	sem_init(&s3, 0, 0);
 
P1				P2			P3
sem_wait(&s1);			sem_wait(&s2);		sem_wait(&s3);
A;				C;			P;
sem_wait(&s1);			sem_post(&s3);		sem_post(&s1);
B;				sem_wait(&s3);
sem_post(&s2);			E;
sem_wait(&s1);			sem_post(&s1);
F;
sem_post(&s3);
sem_wait(&s1);
G;

Explanation

Solution

ABCPFEG
ABCEFDG