Lecture 23
Lecture 23
Lecture 23
Operating System Concepts – 9th Edition 5.1 Silberschatz, Galvin and Gagne
Readers-Writers Problem (Cont.)
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne
Readers-Writers Problem (Cont.)
The structure of a reader process
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);
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne
Readers-Writers Problem Variations
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
Reader-writer lock in read mode: Multiple
processes are permitted concurrently
Reader-writer lock in write mode: Only one
process may acquire the lock for writing
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne
Dining-Philosophers Problem
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne
Dining-Philosophers Problem Algorithm
The structure of Philosopher i:
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting
simultaneously at the table.
Allow a philosopher to pick up the forks
only if both are available (picking must be
done in a critical section.
Use an asymmetric solution -- an odd-
numbered philosopher picks up first the
left chopstick and then the right chopstick.
Even-numbered philosopher picks up first
the right chopstick and then the left
chopstick.
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne