Professional Documents
Culture Documents
Model
Giuseppe Anastasi
g.anastasi@iet.unipi.it
Pervasive Computing & Networking Lab. (PerLab)
Dept. of Information Engineering, University of Pisa
PerLab
Overview
PerLab
Operating Systems
Objectives
PerLab
Operating Systems
Overview
PerLab
Operating Systems
Producer-Consumer Problem
PerLab
Operating Systems
Producer-Consumer Problem
PerLab
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Operating Systems
Producer-Consumer Problem
PerLab
Producer process
item nextProduced;
while (1) {
while (counter == BUFFER_SIZE); /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Operating Systems
Producer-Consumer Problem
PerLab
Consumer process
item nextConsumed;
while (1) {
while (counter == 0);
/* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
Operating Systems
Producer-Consumer Problem
PerLab
The statements
counter++;
counter--;
must be performed atomically.
Atomic operation means an operation that
Operating Systems
Producer-Consumer Problem
PerLab
register2 = counter
register2 = register2 1
counter = register2
10
Operating Systems
Producer-Consumer Problem
PerLab
11
Operating Systems
Race Condition
PerLab
statements is:
producer: register1 = counter (register1 = 5)
producer: register1 = register1 + 1 (register1 = 6)
consumer: register2 = counter (register2 = 5)
consumer: register2 = register2 1 (register2 = 4)
producer: counter = register1 (counter = 6)
consumer: counter = register2 (counter = 4)
The value of count may be either 4 or 6, where the correct
result should be 5.
Shared Memory Model
12
Operating Systems
Race Condition
PerLab
Race condition
must be synchronized.
13
Operating Systems
data
Each process has a code segment, called critical
14
Operating Systems
1. Mutual Exclusion
2. Progress
3. Bounded Waiting.
15
Operating Systems
do {
entry section
critical section
exit section
reminder section
} while (TRUE)
16
Operating Systems
Possible Solutions
PerLab
Software approaches
Hardware solutions
Interrupt disabling
Semaphores
Monitor
17
Operating Systems
Overview
PerLab
18
Operating Systems
A Software Solution
PerLab
Boolean lock=FALSE;
Process Pi {
do {
while (lock); // do nothing
lock=TRUE;
critical section
lock=FALSE;
remainder section
} while (TRUE);
}
Does it work?
19
Operating Systems
Petersons Solution
PerLab
are atomic
The two processes share two variables:
int turn;
Boolean flag[2]
The variable turn indicates whose turn it is to
Operating Systems
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = FALSE;
remainder section
} while (TRUE);
}
21
Operating Systems
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
22
Operating Systems
Overview
PerLab
23
Operating Systems
Synchronization Hardware
PerLab
hardware instructions
24
Operating Systems
Interrupt Disabling
PerLab
do {
disable interrupt;
critical section
enable interrupt;
remainder section
} while (1);
25
Operating Systems
Previous Solution
PerLab
do {
while (lock); // do nothing
lock=TRUE;
critical section
lock=FALSE;
remainder section
} while (1);
The solution does not guaranteed the mutual exclusion
because the test and set on lock are not atomic
26
Operating Systems
Test-And-Set Instruction
PerLab
Definition:
27
Operating Systems
Boolean lock=FALSE;
do {
while (TestAndSet (&lock )); // do nothing
critical section
lock = FALSE;
remainder section
} while (TRUE);
28
Operating Systems
Swap Instruction
PerLab
29
Operating Systems
do {
key = TRUE;
while ( key == TRUE) Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while (TRUE);
This solution guarantees mutual exclusion but not
bounded waiting
Shared Memory Model
30
Operating Systems
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key) key = TestAndSet(&lock);
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j]) j = (j + 1) % n;
if (j == i) lock = FALSE;
else waiting[j] = FALSE;
// remainder section
} while (TRUE);
31
Operating Systems
Overview
PerLab
32
Operating Systems
Semaphore
PerLab
waiting
Semaphore S integer variable
Can only be accessed via two indivisible (atomic)
operations
33
Operating Systems
Semaphore
PerLab
wait (S) {
while (S <= 0);
S--;
// do nothing
}
signal (S) {
S++;
}
wait() and signal() must be atomic
34
Operating Systems
Counting semaphore
Binary semaphore
semaphore
35
Operating Systems
Shared data:
semaphore mutex=1;
Process Pi:
do {
wait (mutex);
// Critical Section
signal (mutex);
// Remainder section
} while (TRUE);
36
Operating Systems
Semaphore Implementation
PerLab
37
Operating Systems
Semaphore Implementation
PerLab
typedef struct {
int value;
struct process *L;
} semaphore;
Assume two simple operations:
38
Operating Systems
Implementation
PerLab
39
Operating Systems
Pi
Pj
wait(flag)
signal(flag)
Operating Systems
40
Deadlock
two or more processes are waiting indefinitely for an event that can
be caused by only one of the waiting processes.
P0
P1
wait(S);
wait(Q);
wait(Q);
wait(S);
signal(S);
signal(Q);
signal(Q)
signal(S);
41
Operating Systems
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
42
Operating Systems
Bounded-Buffer Problem
PerLab
Operating Systems
43
Bounded-Buffer Problem
PerLab
Producer Process
Consumer Process
do {
do {
wait(full)
wait(mutex);
signal(mutex);
signal(empty);
} while (1);
wait(empty);
wait(mutex);
signal(mutex);
signal(full);
} while (1);
44
Operating Systems
Readers-Writers Problem
PerLab
concurrent processes
Problem
45
Operating Systems
Readers-Writers Problem
PerLab
Shared Data
Data set
46
Operating Systems
Readers-Writers Problem
PerLab
do {
wait (wrt) ;
// writing is performed
signal (wrt) ;
} while (TRUE);
47
Operating Systems
Readers-Writers Problem
PerLab
do {
wait (mutex) ;
readcount ++ ;
if (readcount == 1) wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0) signal (wrt) ;
signal (mutex) ;
} while (TRUE);
Shared Memory Model
48
Operating Systems
Dining-Philosophers Problem
PerLab
Shared data
Operating Systems
Dining-Philosophers Problem
PerLab
do {
wait (chopstick[i]);
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i]);
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
Shared Memory Model
50
Operating Systems
51
Operating Systems
Overview
PerLab
52
Operating Systems
Monitors
PerLab
time
monitor monitor-name {
// shared variable declarations
procedure P1 () { . }
procedure Pn () {}
Initialization code ( .) {
}
}
Shared Memory Model
53
Operating Systems
54
Operating Systems
Condition Variables
PerLab
condition x, y;
Two operations on a condition variable:
suspended.
invoked x.wait ()
55
Operating Systems
56
Operating Systems
monitor DP {
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self [i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
Shared Memory Model
57
Operating Systems
Operating Systems
59
Operating Systems
monitor ResourceAllocator {
boolean busy;
condition x;
void acquire(int time) {
if (busy) x.wait(time);
busy = TRUE;
}
void release() {
busy = FALSE;
x.signal();
}
initialization code() {
busy = FALSE;
}
}
60
Shared Memory Model
Operating Systems
Overview
PerLab
61
Operating Systems
Synchronization Examples
PerLab
Solaris
Windows XP
Linux
Pthreads
62
Operating Systems
Solaris Synchronization
PerLab
63
Operating Systems
Windows XP Synchronization
PerLab
objects
may act as either mutexes and semaphores
64
Operating Systems
Linux Synchronization
PerLab
Linux:
Linux provides:
semaphores
spin locks
65
Operating Systems
Pthreads Synchronization
PerLab
mutex locks
condition variables
read-write locks
spin locks
66
Operating Systems
Questions?
PerLab
67
Operating Systems