Process-Synchronization 2
Process-Synchronization 2
Process-Synchronization 2
OPERATING SYSTEMS
2
THREE CLASSICAL
SYNCHRONIZATION PROBLEMS
• Three classical synchronization problems will be
studied.
1. The Producer-Consumer Problem
2. The Readers–Writers Problem
3. The Dining-Philosophers Problem
3
PRODUCER-CONSUMER PROBLEM
4
PRODUCER-CONSUMER PROBLEM
(CONTINUES…)
}
consumer(){
int item; / * local variable */
while (TRUE) { / * If the Buffer is empty, wait, otherwise reduce the
wait(full); number of full spaces by 1 */
wait(mutex); / * Ask permission to enter the critical section */
remove_item(&item); / * Get data from Buffer (Critical section) */
signal(mutex); / * Indicate that you have left the Critical Section */
signal(empty); / * If there is a waiting producer, wake it up */
consume_item(item);
}
} 5
PRODUCER-CONSUMER PROBLEM
(CONTINUES…)
Producer Consumer
do { do {
... wait(full);
// produce an item in nextp wait(mutex);
... ...
wait(empty); // remove item to nextc
wait(mutex); ...
... signal(mutex);
// add nextp to buffer signal(empty);
... ...
signal(mutex); // consume item in nextc
signal(full); ...
} while (true); } while (true);
6
PRODUCER-CONSUMER PROBLEM
SOLUTION IN C
Global Tanımlar
7
PRODUCER-CONSUMER PROBLEM
SOLUTION IN C ( C O N T I N U E S … )
main()
8
PRODUCER-CONSUMER PROBLEM
SOLUTION IN C ( C O N T I N U E S … )
Producer
9
PRODUCER-CONSUMER PROBLEM
SOLUTION IN C ( C O N T I N U E S … )
Consumer
10
READERS-WRITERS PROBLEM
• Suppose a database is shared by multiple processes.
• While some processes only want to read data,
• Others will want to manipulate the data.
(Readers and Writers)
• There is no problem if two processes want to
read at the same time, but a problem may
occur if a writer accesses the data at the same time
with a reader or a writer process.
• This synchronization problem is called
Readers/Writers.
11
READERS-WRITERS PROBLEM
(CONTINUES…)
Writer Process
12
READERS-WRITERS PROBLEM
(CONTINUES…)
Reader Process
do
{
wait(mutex) :The reader waits on the mutex, only
protecting the read counter
read_count++: Number of processes wanting to read
if (read_count==1) :If first reader process
wait(rw_mutex); :1 reader is waiting
signal(mutex)
......
//do the reading
.......
wait(mutex) : n-1 reader is waiting on mutex
read_count--: 1 reader finished reading
14
READERS-WRITERS PROBLEM
SOLUTION IN C
main()
15
READERS-WRITERS PROBLEM
SOLUTION IN C
Writer Process
16
READERS-WRITERS PROBLEM
SOLUTION IN C
Reader Process
17
THE DINING-PHILOSOPHERS
PROBLEM
19
THE DINING-PHILOSOPHERS
PROBLEM ( C O N T I N U E S … )
Pseudocode….
20
THE DINING-PHILOSOPHERS
SOLUTION IN C
Global Definitions
21
THE DINING-PHILOSOPHERS
SOLUTION IN C
THE DINING-PHILOSOPHERS
SOLUTION IN C
REFERENCES
• Textbook:
• Operating System Concepts, Ninth Edition, Abraham
Silberschatz, Peter Bear Galvin, Greg Gagne
24