Professional Documents
Culture Documents
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
6.2
Background
Concurrent access to shared data may result in data
inconsistency
6.3
Producer
while (true)
/* produce an item and put in nextProduced
while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
count++;
}
6.4
Consumer
while (1)
{
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
/* consume the item in nextConsumed
}
6.5
Race Condition
6.6
6.7
Petersons Solution
Two process solution
Assume that the LOAD and STORE instructions are atomic;
int turn;
Boolean flag[2]
critical section.
6.8
6.9
Synchronization Hardware
Many systems provide hardware support for critical section
code
instructions
Atomic = non-interruptable
6.10
TestAndndSet Instruction
Definition:
6.11
do {
while ( TestAndSet (&lock ))
; /* do nothing
//
critical section
lock = FALSE;
//
remainder section
} while ( TRUE);
6.12
Swap Instruction
Definition:
6.13
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
//
critical section
lock = FALSE;
//
remainder section
} while ( TRUE);
6.14
Semaphore
Less complicated
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
6.15
domain
Semaphore S;
wait (S);
// initialized to 1
Critical Section
signal (S);
6.16
Semaphore Implementation
Must guarantee that no two processes can execute wait () and
where the wait and signal code are placed in the crtical section.
6.17
Two operations:
6.18
Implementation of wait:
wait (S){
value--;
if (value < 0) {
add this process to waiting queue
block(); }
}
Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
6.19
P0
P1
wait (S);
wait (Q);
wait (Q);
wait (S);
signal (S);
signal (Q);
signal (Q);
signal (S);
6.20
6.21
Bounded-Buffer Problem
N buffers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value N.
6.22
6.23
6.24
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
one single writer can access the shared data at the same time.
Shared Data
Data set
6.25
do {
wait (wrt) ;
//
writing is performed
signal (wrt) ;
} while (true)
6.26
6.27
Dining-Philosophers Problem
Shared data
6.28
6.29
6.30
Monitors
procedure Pn () {}
Initialization code ( .) { }
}
}
6.31
6.32
Condition Variables
condition x, y;
Two operations on a condition variable:
6.33
6.34
6.35
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
6.36
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads
6.37
Solaris Synchronization
Implements a variety of locks to support multitasking,
6.38
Windows XP Synchronization
Uses interrupt masks to protect access to global resources on
uniprocessor systems
and semaphores
6.39
Linux Synchronization
Linux:
Linux provides:
semaphores
spin locks
6.40
Pthreads Synchronization
Pthreads API is OS-independent
It provides:
mutex locks
condition variables
read-write locks
spin locks
6.41
End of Chapter 6