0% found this document useful (0 votes)
26 views

ch07 Synchronization Examples - Blankfill

The document discusses several classic synchronization problems: the bounded buffer problem, readers-writers problem, and dining philosophers problem. It provides code examples to illustrate solutions to these problems using semaphores and monitors. Synchronization is important within operating system kernels and in languages like Java and POSIX.

Uploaded by

tmdgn0214
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

ch07 Synchronization Examples - Blankfill

The document discusses several classic synchronization problems: the bounded buffer problem, readers-writers problem, and dining philosophers problem. It provides code examples to illustrate solutions to these problems using semaphores and monitors. Synchronization is important within operating system kernels and in languages like Java and POSIX.

Uploaded by

tmdgn0214
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

본 강의 자료는 Wiley 출판사에서 저작한 것을 강의용으로 수정 편집한 것이고, 본 영상은

명지대학교 데이터테크놀로지 전공 강의용으로 제작되었습니다. 본 강의 자료 및 영상의 무단


복제 및 배포는 저작권법에 위배되어 해당 법령에 의해 처벌될 수 있음을 유의 바랍니다.

Chapter 7
Synchronization Examples

Operating System Concepts


Tenth Edition
Silberschatz, Galvin and Gagne
Chapter 7: Synchronization Examples

◆ Classic Problems of Synchronization


◆ Synchronization within the Kernel
◆ POSIX Synchronization
◆ Synchronization in Java
◆ Alternative Approaches

Copyright © 2020 John Wiley & Sons, Inc. 2


Objectives
◆ To explain the bounded-buffer, readers-writers, and
dining-philosophers synchronization problems
◆ To describe several tools used by Linux and Windows
to solve process synchronization problems

Copyright © 2020 John Wiley & Sons, Inc. 3


Classical Problems of Synchronization
◆ Classical problems used to test newly-proposed synchronization
schemes
➢ Bounded-Buffer Problem
➢ Readers and Writers Problem
➢ Dining-Philosophers Problem

Copyright © 2020 John Wiley & Sons, Inc. 4


Bounded-Buffer Problem 1
◆ Producer and consumer processes share
➢ n buffers, each can hold one item
➢ Binary semaphore mutex initialized to the value 1
➢ Counting semaphore full initialized to the value 0
➢ Counting semaphore empty initialized to the value n

int n;
semaphore mutex = 1;
semaphore full = 0;
semaphore empty = n;

Copyright © 2020 John Wiley & Sons, Inc. 5


Bounded Buffer Problem 2 wait(semaphore *S) {
S->value--;
if (S->value < 0) {
◆ The structure of the producer process add this to S->list;
sleep();
}
while (true) { }
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next_produced to the buffer */
...
signal(mutex); signal(semaphore *S) {
signal(full); S->value++;
} if (S->value <= 0) {
remove P from S->list;
wakeup(P);
}
}
Copyright © 2020 John Wiley & Sons, Inc. 6
Bounded Buffer Problem 3
◆ The structure of the consumer process

while (true) {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next_consumed */
...
}

Copyright © 2020 John Wiley & Sons, Inc. 7


Readers-Writers Problem 1
◆ A data set is shared among a number of concurrent processes
➢ Readers – only read the data set; they do not perform any updates
➢ Writers – can both read and write
◆ Problem
➢ allow multiple readers to read at the same time
➢ Only one single writer can access the shared data at the same time
◆ Several variations, all involving priorities
➢ First variation – no reader kept waiting unless writer has permission to
use shared object
➢ Second variation – once writer is ready, it performs the write ASAP
➢ Both may have starvation leading to even more variations
➢ Problem is solved on some systems by kernel providing reader-writer
locks

Copyright © 2020 John Wiley & Sons, Inc. 8


Readers-Writers Problem 2
◆ Shared Data
➢ Binary semaphore rw_mutex initialized to 1
➢ Binary semaphore mutex initialized to 1
➢ Counting semaphore read_count initialized to 0

◆ The structure of a writer process

while (true) {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}

Copyright © 2020 John Wiley & Sons, Inc. 9


Readers-Writers Problem 3
◆ The structure of a reader process
while (true) {
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);
}

Copyright © 2020 John Wiley & Sons, Inc. 10


Dining-Philosophers Problem
◆ 5 Philosophers spend their lives alternating thinking and eating
◆ Don’t interact with their neighbors, occasionally try to pick up 2
chopsticks (one at a time) to eat from bowl
➢ Need both to eat, then release both when done

Copyright © 2020 John Wiley & Sons, Inc. 11


Semaphore Solution to Dining Philosophers
◆ Shared data ◆ The structure of Philosopher i:
➢ Semaphore
while (true) {
chopstick [5]
wait( chopstick[i] );
initialized to 1
wait( chopstick[(i+1)%5] );
...
/* eat for a while */
...
signal( chopstick[i] );
signal( chopstick[(i+1)%5] );
...
/* think for a while */
...
}

◆ What is the problem with this algorithm?

Copyright © 2020 John Wiley & Sons, Inc. 12


Monitor Solution to Dining Philosophers 1
monitor DiningPhilosophers Restriction: a philosopher pick up
{ chopsticks only if both are available
enum {THINKING,HUNGRY,EATING} state[5];
condition self[5];
void pickup(int i) {
state[i] = HUNGRY;
test(i);
if (state[i]!=EATING) self[i].wait();
}
void test(int i) {
void putdown(int i) { if ((state[(i + 4) % 5] != EATING) &&
state[i] = THINKING; (state[i] == HUNGRY) &&
// test left and right neighbors(state[(i + 1) % 5] != EATING) ) {
test((i + 4) % 5); state[i] = EATING;
test((i + 1) % 5); self[i].signal();
} }
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
} © 2020 John Wiley & Sons, Inc.
Copyright 13
Monitor Solution to Dining Philosophers 2

◆ Each philosopher i invokes the operations pickup() and


putdown() in the following sequence:

DiningPhilosophers.pickup(i);
...
eat
...
DiningPhilosophers.putdown(i);

◆ No deadlock, but starvation is possible

Copyright © 2020 John Wiley & Sons, Inc. 14


Summary

You might also like