OS_CH7
OS_CH7
Examples
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Recap
• In the previous chapter
• discussed the critical-section problem and focused on how race conditions can occur when multiple
concurrent processes share data.
• examined several tools that address the critical-section problem by preventing race conditions from
occurring.
• These tools ranged from pure software-based solution written by programmers to low-level hardware
solutions (such as memory barriers and the compare-and-swap and the test_and_set operation) to higher-
level tools (from mutex locks to semaphores).
•
Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Producer-Consumer problem
• The producer produces items one by one and places them in the buffer, this
buffer has a limited number of items that it can hold
• Consumer consumes items one by one from the buffer
Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Producer finds Buffer is full
Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Consumer finds empty buffer
Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Solution to Bounded-Buffer Problem
Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
}
Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
• 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 */
...
}
Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Producer-consumer
If the buffer is empty, then the consumer waits until a new item is
placed in the buffer. When the producer produces an item it does the
following: wait (empty) will decrement empty variable and increment
variable full by calling signal (full) this will awake the consumer
waiting for full to become positive wait(full)
Operating System Concepts – 10th Edition 10 Silberschatz, Galvin and Gagne ©2018
Producer
Wait(empty) Decrements
empty variable
Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Producer
Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Producer
Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Consumer
Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Consumer
Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Consumer
Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem
• 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
• allow multiple readers to read at the same time will not have any bad
affect on the shared dataset
• However, if a writer and another process (either reader or writer) access
the shared dataset simultaneously, inconsistent data might be the result
• Only one single writer can access the shared data at the same time
• A writer should get an exclusive access to the shared object, no other
reader or writer is allowed to get access
Operating System Concepts – 10th Edition 2 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem
• Several variations of how readers and writers are considered – all involve
some form of priorities
• The simplest one, referred to as the first readers–writers problem,
• Requires that no reader be kept waiting unless a writer has already
obtained permission to use the shared object.
• In other words, no reader should wait for other readers to finish simply
because a writer is waiting
Operating System Concepts – 10th Edition 3 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem/reader preference
• The reader-writer problem can be solved using semaphores
• Shared Data
• Data set/ what is to be shared
• Semaphore rw_mutex initialized to 1
• compete between readers and writers
• Semaphore mutex initialized to 1
• To lock the read count
• Integer read_count initialized to 0
• keeps track of how many processes are currently reading the object
Operating System Concepts – 10th Edition 4 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
Operating System Concepts – 10th Edition 5 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true){ • This is necessary to lock the
wait(mutex);
read_count++; read count, if many readers
if (read_count == 1) are trying to read at the same
wait(rw_mutex); time, then we need to
signal(mutex); synchronize how they
... increment the read_count
/* reading is performed */ variable
...
wait(mutex); • Other reader can start after
read count--;
if (read_count == 0) the one locking the mutex
signal(rw_mutex); release it using signal(mutex)
signal(mutex);
}
Operating System Concepts – 10th Edition 6 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true){ • When the first reader comes, it
wait(mutex);
read_count++; updates the read_count, so it is
if (read_count == 1) equal to 1. then it will call
wait(rw_mutex); wait(rw_mutex) to block the
signal(mutex); writer. Othor readers don’t
... need to lock the writer, it is
/* reading is performed */
already locked by the first
...
reader
wait(mutex);
read count--;
if (read_count == 0) • The last reader release the lock
signal(rw_mutex); to enable the writer to resume
signal(mutex); (read_count ==0)
}
• First-reader competes with the
writer and last reader signal the
writer
Operating System Concepts – 10th Edition 7 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem/ analyze this code
Readers will be
blocked until all
writers finish
Operating System Concepts – 10th Edition 8 Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations
Operating System Concepts – 10th Edition 9 Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
• We have studied the Critical Section problem, which is one of the
synchronization problems
• In this chapter, we will discuss examples of concurrency control
problem
• Classical problems used to test newly-proposed synchronization
schemes
• Bounded-Buffer Problem/ producer-consumer problem
• Readers and Writers Problem
• Dining-Philosophers Problem
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Solution to Bounded-Buffer Problem
•Producer must not insert data when the buffer is full
•Consumer must not remove data when buffer is empty
•The producer and consumer must not insert and remove data
simultaneously.
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
•The structure of the producer process
while (true) {
...
/* produce an item in next_produced */
...
wait(empty); // wait until empty > 0 and then decrement
‘empty’
wait(mutex); // acquire lock
...
/* add next produced to the buffer */
...
signal(mutex); // release lock
signal(full); // increment ‘full’
}
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
•The structure of the consumer process
while (true) {
wait(full); // wait until full>0 and then decrement ‘full’
wait(mutex); // acquire lock
...
/* remove an item from buffer to next_consumed */
...
signal(mutex); // release lock
signal(empty); // increment ‘empty’
...
/* consume the item in next consumed */
...
}
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
• Readers – only read the data set; they do not perform any updates
• Writers – update data (both read and write)
• allow multiple readers to read at the same time will not have any bad
affect on the shared dataset
• However, if a writer and another process (either reader or writer) access
the shared dataset simultaneously, inconsistent data might be the result
• Only one single writer can access the shared data at the same time
• A writer should get an exclusive access to the shared object, no other
reader or writer is allowed to get access
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem/reader preference
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true){ • This is necessary to lock the
wait(mutex);
read_count++; // acquire a lock when reader want to update read count, if many readers
read_count are trying to read at the same
time, then we need to
if (read_count == 1) synchronize how they
wait(rw_mutex); // to ensure no writer can enter if there is increment the read_count
even one reader variable
signal(mutex); // other readers can enter while this current
reader is inside the CS • Other reader can start after
... the one locking the mutex
/* reading is performed */ release it using signal(mutex)
...
wait(mutex);
read count--;// a reader wants to leave
if (read_count == 0) // no reader is left in CS
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
• Five philosophers are sitting around a table. Each philosopher has a plate in front of
him/her.
• Between each two plates there is a chopstick.
• A philosopher is either “thinking” or “eating”.
• When a philosopher gets hungry he/she tries to get the 2 chopsticks on his/her left
and right.
• Having obtained both chopsticks the philosopher can eat.
• They cannot have both chopstick at the same time; pick the left then the right
• When finished eating, the philosopher puts both chopsticks down, and resumes
thinking.
Operating System Concepts – 10th Edition 11 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
Operating System Concepts – 10th Edition 12 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
Operating System Concepts – 10th Edition 13 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem Algorithm
• Semaphore Solution
• The structure of Philosopher i:
while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
}
• What is the problem with this algorithm?
Operating System Concepts – 10th Edition 14 Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
Operating System Concepts – 10th Edition 15 Silberschatz, Galvin and Gagne ©2018
Think of these cases
Operating System Concepts – 10th Edition 16 Silberschatz, Galvin and Gagne ©2018
Think of these cases
Operating System Concepts – 10th Edition 17 Silberschatz, Galvin and Gagne ©2018
End of Chapter 7
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018