05 Sync Part1
05 Sync Part1
Amir H. Payberah
[email protected]
Nov. 14, 2023
Processes Synchronization
1 / 46
Background
2 / 46
Background
https://tinyurl.com/2yjcpx75
2 / 46
Background
https://tinyurl.com/2yjcpx75
2 / 46
Background
https://tinyurl.com/2yjcpx75
2 / 46
Producer-Consumer Problem
3 / 46
Producer-Consumer Problem
3 / 46
Producer-Consumer Problem
3 / 46
Producer-Consumer Problem
3 / 46
Producer-Consumer Problem
3 / 46
Producer
I Producer
while (true) {
/* produce an item in next produced */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
4 / 46
Consumer
I Consumer
while (true) {
while (counter == 0); /* do nothing */
next_consumed = buffer[out];
counter--;
/* consume the item in next consumed */
}
5 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
Race Condition
6 / 46
What’s The Output?
int counter = 0;
return NULL;
}
int main(void) {
pthread_t t1, t2;
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
7 / 46
What’s The Output?
Job 1 started.
Job 2 started.
Job 2 finished.
Job 2 finished.
8 / 46
The Critical-Section (CS)
Problem
9 / 46
The Critical-Section Problem (1/2)
10 / 46
The Critical-Section Problem (1/2)
10 / 46
The Critical-Section Problem (1/2)
10 / 46
The Critical-Section Problem (1/2)
10 / 46
The Critical-Section Problem (2/2)
I Each process must ask permission to enter CS in entry section, may follow
CS with exit section, then remainder section.
11 / 46
The Critical-Section Problem (2/2)
I Each process must ask permission to enter CS in entry section, may follow
CS with exit section, then remainder section.
I General structure of process Pi is below:
11 / 46
CS Problem Solution Requirements (1/3)
12 / 46
CS Problem Solution Requirements (2/3)
13 / 46
CS Problem Solution Requirements (3/3)
14 / 46
CS Solutions
I Peterson’s solution
I Mutex lock
I Semaphore
15 / 46
Peterson’s Solution
16 / 46
Peterson’s Solution
I Two-process solution.
17 / 46
Peterson’s Solution
I Two-process solution.
I The two processes share two variables:
• int turn
• boolean flag[2]
17 / 46
Peterson’s Solution
I Two-process solution.
I The two processes share two variables:
• int turn
• boolean flag[2]
17 / 46
Peterson’s Solution
I Two-process solution.
I The two processes share two variables:
• int turn
• boolean flag[2]
17 / 46
Algorithm for Process Pi
18 / 46
CS Requirements
19 / 46
CS Requirements
19 / 46
CS Requirements
19 / 46
CS Requirements
19 / 46
Mutex Locks
20 / 46
Mutex Locks
21 / 46
Mutex Locks
21 / 46
Mutex Locks
21 / 46
acquire() and release()
acquire() {
while (!available); /* busy wait */
available = false;
}
release() {
available = true;
}
22 / 46
23 / 46
pthread Mutexes
24 / 46
pthread Mutexes
24 / 46
pthread Mutexes
24 / 46
What’s The Output?
int counter = 0;
pthread_mutex_t lock;
return NULL;
}
int main(void) {
pthread_t t1, t2;
pthread_mutex_init(&lock, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
25 / 46
What’s The Output?
Job 1 started.
Job 1 finished.
Job 2 started.
Job 2 finished.
26 / 46
Semaphores
27 / 46
Semaphore
28 / 46
Semaphore
28 / 46
wait() and signal()
wait(S) {
while (S <= 0); // busy wait
S--;
}
signal(S) {
S++;
}
29 / 46
Counting and Binary Semaphore
30 / 46
Counting and Binary Semaphore
30 / 46
Semaphore Usage (1/2)
31 / 46
Semaphore Usage (1/2)
31 / 46
Semaphore Usage (1/2)
31 / 46
Semaphore Usage (1/2)
I If S = 0: all resources are used, and processes that wish to use a resource
will block until the count becomes greater than 0.
31 / 46
Semaphore Usage (2/2)
32 / 46
Semaphore Usage (2/2)
// Process P1
C1;
signal(S);
// Process P2
wait(S);
C2;
32 / 46
Semaphore Usage (2/2)
// Process P1
C1;
signal(S);
// Process P2
wait(S);
C2;
32 / 46
Semaphore Implementation with no Busy Waiting (1/2)
33 / 46
Semaphore Implementation with no Busy Waiting (1/2)
33 / 46
Semaphore Implementation with no Busy Waiting (1/2)
typedef struct {
int value;
struct process *list;
} semaphore;
33 / 46
Semaphore Implementation with no Busy Waiting (2/2)
I block: place the process invoking the operation on the appropriate wait-
ing queue.
I wakeup: remove one of processes in the waiting queue and place it in
the ready queue.
34 / 46
Semaphore Implementation with no Busy Waiting (2/2)
I block: place the process invoking the operation on the appropriate wait-
ing queue.
I wakeup: remove one of processes in the waiting queue and place it in
the ready queue.
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
// add this process to S->list;
block();
}
}
34 / 46
Semaphore Implementation with no Busy Waiting (2/2)
I block: place the process invoking the operation on the appropriate wait-
ing queue.
I wakeup: remove one of processes in the waiting queue and place it in
the ready queue.
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
// add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
// remove a process P from S->list;
wakeup(P);
}
}
34 / 46
Deadlock
35 / 46
Deadlock
35 / 46
Starvation
36 / 46
Starvation
36 / 46
Starvation
36 / 46
37 / 46
POSIX Semaphore
38 / 46
POSIX Semaphore
38 / 46
POSIX Semaphore
38 / 46
Parent-Child Example
void parent() {
sem_t *sem_id = sem_open(sem_name, O_CREAT, 0600, 0);
39 / 46
Parent-Child Example
void parent() {
sem_t *sem_id = sem_open(sem_name, O_CREAT, 0600, 0);
void child() {
sem_t *sem_id = sem_open(sem_name, O_CREAT, 0600, 0);
39 / 46
Readers and Writers Problem
40 / 46
Readers and Writers Problem (1/3)
41 / 46
Readers and Writers Problem (1/3)
I Problem: allow multiple readers to read at the same time, only one single
writer can access the shared data at the same time.
41 / 46
Readers and Writers Problem (1/3)
I Problem: allow multiple readers to read at the same time, only one single
writer can access the shared data at the same time.
I Shared Data
• Semaphore rw mutex initialized to 1.
• Semaphore mutex initialized to 1.
• Integer read count initialized to 0.
41 / 46
Readers and Writers Problem (2/3)
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
42 / 46
Readers and Writers Problem (3/3)
do {
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read_count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
} while (true);
43 / 46
Summary
44 / 46
Summary
45 / 46
Summary
45 / 46
Summary
45 / 46
Summary
45 / 46
Summary
I Reader/writer problem
45 / 46
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
46 / 46