04 Shared Memory
04 Shared Memory
Model
Giuseppe Anastasi
[email protected]
Pervasive Computing & Networking Lab. (PerLab)
Dept. of Information Engineering, University of Pisa
PerLab
Overview
PerLab
Objectives
PerLab
Overview
PerLab
Producer-Consumer Problem
PerLab
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;
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++;
}
Producer-Consumer Problem
PerLab
Consumer process
item nextConsumed;
while (1) {
while (counter == 0);
/* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
Producer-Consumer Problem
PerLab
The statements
counter++;
counter--;
must be performed atomically.
Atomic operation means an operation that
completes in its entirety without interruption.
Producer-Consumer Problem
PerLab
10
Producer-Consumer Problem
PerLab
11
Race Condition
PerLab
12
Race Condition
PerLab
Race condition
The situation where several processes access and
manipulate shared data concurrently.
The final value of the shared data depends upon which
process finishes last.
13
14
1. Mutual Exclusion
If process Pi is executing in its critical section, then no other
processes can be executing in their critical sections.
2. Progress
If no process is executing in its critical section and there exist
some processes that wish to enter their critical section, then the
selection of the processes that will enter the critical section next
cannot be postponed indefinitely.
3. Bounded Waiting.
A bound must exist on the number of times that other processes
are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request
is granted.
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n processes.
Shared Memory Model
15
16
Possible Solutions
PerLab
Software approaches
Hardware solutions
Interrupt disabling
Special machine instructions
17
Overview
PerLab
18
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?
Shared Memory Model
19
Petersons Solution
PerLab
20
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = FALSE;
remainder section
} while (TRUE);
}
21
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
22
Overview
PerLab
23
Synchronization Hardware
PerLab
24
Interrupt Disabling
PerLab
do {
disable interrupt;
critical section
enable interrupt;
remainder section
} while (1);
25
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
Test-And-Set Instruction
PerLab
Definition:
boolean TestAndSet (boolean *target) {
boolean rv = *target;
*target = TRUE;
return rv;
}
27
Boolean lock=FALSE;
do {
while (TestAndSet (&lock )); // do nothing
critical section
lock = FALSE;
remainder section
} while (TRUE);
28
Swap Instruction
PerLab
29
30
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);
Shared Memory Model
31
Overview
PerLab
32
Semaphore
PerLab
33
Semaphore
PerLab
wait (S) {
while (S <= 0);
// do nothing
S--;
}
signal (S) {
S++;
}
wait() and signal() must be atomic
Shared Memory Model
34
Counting semaphore
integer value can range over an unrestricted domain
Binary semaphore
integer value can range only between 0 and 1; can be simpler to
implement
Also known as mutex locks
35
Shared data:
semaphore mutex=1;
Process Pi:
do {
wait (mutex);
// Critical Section
signal (mutex);
// Remainder section
} while (TRUE);
36
Semaphore Implementation
PerLab
37
Semaphore Implementation
PerLab
38
Implementation
PerLab
39
Pi
Pj
wait(flag)
signal(flag)
40
Deadlock
two or more processes are waiting indefinitely for an event that can
be caused by only one of the waiting processes.
P1
wait(S);
wait(Q);
wait(Q);
wait(S);
signal(S);
signal(Q);
signal(Q)
signal(S);
41
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
42
Bounded-Buffer Problem
PerLab
43
Bounded-Buffer Problem
PerLab
Consumer Process
Producer Process
do {
wait(full)
wait(mutex);
signal(mutex);
signal(empty);
} while (1);
do {
wait(empty);
wait(mutex);
signal(mutex);
signal(full);
} while (1);
Shared Memory Model
44
Readers-Writers Problem
PerLab
Problem
Allow multiple readers to read at the same time.
Only one single writer can access the shared data at the same time
Variants
No new reader must wait when a writer is waiting for data access
No new reader can start reading when a writer is waiting for data
access
45
Readers-Writers Problem
PerLab
Shared Data
Data set
Integer readcount initialized to 0
Semaphore mutex initialized to 1
Mutual exclusion on readcount
46
Readers-Writers Problem
PerLab
47
Readers-Writers Problem
PerLab
48
Dining-Philosophers Problem
PerLab
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Shared Memory Model
49
Dining-Philosophers Problem
PerLab
50
Dining-Philosophers Problem
PerLab
Deadlock
A deadlock occurs if all philosophers start eating
simultaneously
Starvation
Any solution must avoid that a philosopher may starve
Shared Memory Model
51
52
Overview
PerLab
53
Monitors
PerLab
procedure Pn () {}
Initialization code ( .) {
}
}
Shared Memory Model
54
55
56
Condition Variables
PerLab
condition x, y;
Two operations on a condition variable:
x.wait () a process that invokes the operation is (always)
suspended.
x.signal () resumes one of processes (if any) that
invoked x.wait ().
57
Based on monitors
The solution assumes that
A philosopher can take his/her chopsticks only when
are both free
58
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
59
60
While (1) {
DP.pickup (i);
Eat;
DP.putdown (i);
Think;
}
Shared Memory Model
61
Overview
PerLab
62
Synchronization Examples
PerLab
Solaris
Windows XP
Linux
Pthreads
63
Solaris Synchronization
PerLab
64
Windows XP Synchronization
PerLab
65
Linux Synchronization
PerLab
Linux:
Prior Version 2.6, non-preemptive kernel
A task executed in system mode cannot be interrupted, even by a
higher-priority thread
Linux provides:
semaphores
spin locks
Linux kernel
Multi-processor
Enable/disable spinlocks (active only for short times)
Single-processor
Disable/Enable preemption
66
Pthreads Synchronization
PerLab
67
Questions?
PerLab
68