Process-Synchronization 1
Process-Synchronization 1
OPERATİNG SYSTEMS
2
PROBLEM DEFINITION
• Concurrent (at the same time) access to shared
data (memory space, file) or a shared resource is
always prone to problems and may cause
inconsistency.
• The way to ensure consistency is
3
PRODUCER/CONSUMER PROBLEM
4
PRODUCER/CONSUMER PROBLEM
(CONTINUES…)
• Producer Process
Old code New code
item next_produced; item next_produced;
while (true) while (true) {
{ /* produce an item and */
/* produce an item in next_produced */ /* put in next_produced*/
5
PRODUCER/CONSUMER PROBLEM
(CONTINUES…)
• Consumer Process
Old code New code
item next_consumed; item next_consumed;
while (true) { while (true) {
while (counter == 0)
while (in == out) ; // do nothing buffer empty
; /* do nothing, buffer is empty*/
next_consumed = buffer[out];
next-consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE;
out = (out + 1) % BUFFER SIZE; counter--;
/* consume the item in next cons. */ /* consume the item in next cons. */
} }
6
PRODUCER/CONSUMER PROBLEM
(CONTINUES…)
7
PRODUCER/CONSUMER PROBLEM
(CONTINUES…)
10
RACE CONDITION
11
EXAMPLE: PRINTER PROCESS
SPOOLER
Info:
• When the process wants to print a file, it writes the name of
the file into a special spooler array.
• The other process (printer daemon) constantly checks if there
is a file to print.
• If there are files, it prints them and removes their names from
the array.
• Let's assume that the Spooler array contains many entries.
• out: points to the next file to print.
• in: points to the next free slot in the array.
12
EXAMPLE: PRINTER PROCESS
SPOOLER ( C O N T I N U E S … )
Scenario:
• Processes A and B want to print a file. A reads in and stores 7 in the local variable.
• A clock interrupt occurs and the CPU thinks it has run process A long enough
and jumps to process B.
• B process reads the variable in and gets 7 again.
• In this case, they both think the next free slot is 7.
• B continues executing and writes the file name to slot 7 to print. It sets the in
variable to 8.
• When A will be executed from where it left off, it wants to write the file name to 7.
It deletes the filename added by B and writes its own filename.
• Printer daemon doesn't notice anything wrong, it continues working.
• B never gets an answer.
13
CRITICAL SECTION PROBLEM
14
CRITICAL SECTION PROBLEM
(CONTINUES…)
15
CRITICAL SECTION PROBLEM
(CONTINUES…)
16
CRITICAL SECTION PROBLEM
(CONTINUES…)
17
CRITICAL SECTION PROBLEM
(CONTINUES…)
• Solutions
• Software
• Peterson Solution
• Hardware
• Mutex locks
• Semaphores
18
PETERSON SOLUTION
19
PETERSON SOLUTION (CONTINUES…)
20
PETERSON SOLUTION (CONTINUES…)
22
MUTEX LOCKS (CONTINUES…)
24
PTHREAD MUTEX API FUNCTIONS
• pthread_mutex_t lock
• Defines the mutex object.
• pthread_mutex_init(&lock, NULL)
• Creates the mutex object.
• pthread_mutex_lock(&lock)
• Locks the critical section of code block.
• pthread_mutex_unlock(&lock)
• Unlocks the lock on the critical section code block.
• pthread_mutex_destroy(&lock)
• Terminate the mutex object.
25
SPINLOCK MECHANISM
26
SPINLOCK MECHANISM (CONTINUES…)
27
SPINLOCK MECHANISM (CONTINUES…)
• pthread_spinlock_t spinlock
• Defines the spinlock object.
• pthread_spin_init(&lock, NULL)
• Creates the spinlock object.
• pthread_spin_lock(&lock)
• Locks the critical section of code block.
• pthread_spin_unlock(&lock)
• Unlocks the lock on the critical section code block.
• pthread_spin_destroy(&lock)
• Terminate the Spinlock object.
29
SEMAPHORES
30
SEMAPHORES (CONTINUES…)
31
SEMAPHORES (CONTINUES…)
32
SEMAPHORES (CONTINUES…)
33
SEMAPHORES (CONTINUES…)
• Hotel example
• The hotel's receptionist uses the semaphore variable to give
rooms (resources) to customers (process/thread).
• The number of rooms available in the hotel when the hotel
is completely empty (S = number of rooms in the hotel). The
number of rooms is reduced as customers arrive.
• This number is increased when leaving the room. When the
number reaches zero, there is no room left and those who
come can wait in the lobby (queue) if they want. Those
waiting are taken in order (FIFO).
• In a binary semaphore, there is only one room.
35
SEMAPHORE IMPLEMENTATION
36
SEMAPHORE IMPLEMENTATION
(CONTINUES…)
37
SEMAPHORE IMPLEMENTATION
(CONTINUES…)
wait()
signal()
39
CLASSIC SYNCHRONIZATION
PROBLEMS
40
REFERENCES
• Textbook:
• Operating System Concepts, Ninth Edition, Abraham
Silberschatz, Peter Bear Galvin, Greg Gagne
• http://www.albahari.com/threading/
41