Professional Documents
Culture Documents
Operating Systems: Process Synchronization
Operating Systems: Process Synchronization
Chapter 5
Process Synchronization
Week 09+10, CS3104-Operating Systems
Syed Sherjeel Ahmad Gilani, Spring 2018
Process Synchronization
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Semaphores
Synchronization Examples
Atomic Transactions
POSIX Synchronization
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
item nextProduced;
while (1) {
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
6 Operating Systems (Spring 2018)
Bounded-Buffer
Consumer process
item nextConsumed;
while (1) {
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
counter++;
counter--;
register1 = counter
register1 = register1 + 1
counter = register1
register2 = counter
register2 = register2 – 1
9 counter = register2
Operating Systems (Spring 2018)
Bounded Buffer
If both the producer and consumer attempt to
update the buffer concurrently, the assembly
language statements may get interleaved.
Provable that
1. Mutual exclusion is preserved
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
19 Operating Systems (Spring 2018)
Synchronization Hardware
Many systems provide hardware support for
critical section code
Modern machines provide special atomic
hardware instructions
Atomic instruction = non-interruptable
Race conditions are prevented by requiring
that critical regions be protected by locks
That is, a process must acquire a lock before
entering a critical section
It releases the lock when it exits the critical
section
Value: -6
Queue:
Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
25 Operating Systems (Spring 2018)
Semaphore Implementation
Define a semaphore as a record
typedef struct {
int value;
struct process *list;
} semaphore;
signal(S):
S.value++;
if (S.value <= 0) {
remove a process P from S.list;
wakeup(P);
}
http://docs.oracle.com/javase/1.5.0/docs/api/ja
va/util/concurrent/Semaphore.html
Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs
another one
Dining-Philosophers Problem
Initially:
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
37 Operating Systems (Spring 2018)
Bounded-Buffer Problem Consumer Process
do {
wait(full)
wait(mutex);
…
remove an item from buffer to nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
} while (1);
38 Operating Systems (Spring 2018)
Bounded-Buffer Problem Producer Consumer
Shared data
semaphore full, empty, mutex;
Initially:
full = 0, empty = n, mutex = 1
//Producer //Consumer
do { do {
… wait(full)
produce an item in nextp wait(mutex);
… …
wait(empty); remove an item from buffer to
nextc
wait(mutex);
…
…
signal(mutex);
add nextp to buffer
signal(empty);
…
…
signal(mutex);
consume the item in nextc
signal(full);
…
} while (1);
} while (1);
39 Operating Systems (Spring 2018)
Readers/Writers Problem
Any number of readers may simultaneously
read the file
Only one writer at a time may write to the file
If a writer is writing to the file, no reader may
read it
Shared data
semaphore chopstick[5];
Initially all values are 1
43 Operating Systems (Spring 2018)
Dining-Philosophers Problem
Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
…
eat
…
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
…
think
…
} while (1);
44 Operating Systems (S2018)
Dining-Philosophers Problem - Deadlock
Suppose that all five philosophers become hungry
at the same time and each grabs her left
chopstick
Deadlock will occur
Solutions could be;
Allow at most four philosophers to be sitting
simultaneously at the table.
Allow a philosopher to pick up her chopsticks only if
both chopsticks are available (to do this, she must
pick them up in a critical section).
Use an asymmetric solution—that is, an odd-
numbered philosopher picks up first her left
chopstick and then her right chopstick, whereas an
even-numbered philosopher picks up her right
chopstick and then her left chopstick
45 Operating Systems (S2018)
PROCESS SYNCHRONIZATION
High-level synchronization construct that allows the safe sharing
of an abstract data type among concurrent processes.
monitor monitor-name
{
shared variable declarations
procedure body P1 (…) {
...
}
procedure body P2 (…) {
...
}
procedure body Pn (…) {
...
}
{
initialization code
}
46 Operating Systems (S2018)
}
Modern OSs
Windows, Linux, and Solaris provide
mechanisms such as semaphores, mutex
locks, spinlocks, and condition variables to
control access to shared data.
48
Operating Systems (S2018)
Linux Synchronization
Linux:
Prior to kernel Version 2.6, disables interrupts to
implement short critical sections
Version 2.6 and later, fully preemptive
Linux provides:
semaphores
spinlocks
reader-writer versions of both