Download Lecture 10 - Suraj @ LUMS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Scheduling (computing) wikipedia , lookup

Spectral density wikipedia , lookup

Signal (software) wikipedia , lookup

Transcript
Wednesday, June 21, 2006
"The future belongs to him who
knows how to wait. "
- Russian Proverb
1
 Race conditions must be avoided on
kernel data structures
 Handling of critical sections in an
operating system:


Preemptive kernels
Non-preemptive kernels (yield, block, exit)
• prevent interrupts from occurring while shared
data is accessed.
2


Preemptive kernels
• Solaris
Non-preemptive kernels
• Windows, Unix, Linux 2.6 and above
3
Problems of Synchronization
 Bounded buffer problem
4
 A lock that uses busy waiting is called a
spinlock
 Synchronization tool that allow a process
to block instead of wasting CPU time,
when the process is not allowed to enter
its critical region.
5
6
Semaphore
Semaphore is an integer variable that apart
from initialization is accessed only through
two atomic operations
 wait
 signal
7
Semaphore Implementation
 Define a semaphore as a record
typedef struct {
int value;
struct process *L;
} semaphore;
 Assume two system calls:


block suspends the process that invokes it.
wakeup(P) resumes the execution of a
blocked process P.
8
Implementation
 Semaphore operations now defined as
wait(S):
S.value--;
if (S.value < 0) {
add this process to S.L;
block;
}
signal(S):
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
9
Critical Section of n Processes
 Shared data:
semaphore mutex; //initially mutex = 1
 Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
How do we
ensure Bounded
Waiting?
10
 Modification to the integer value of the
semaphore in the wait and signal operations
must be executed indivisibly (i.e. when one
process modifies the semaphore value, no
other process can simultaneously modify it)
 Proper initialization of semaphore is very
important.
 Solution to milk problem?
11
wait operation: - waiting queue associated with semaphore
signal operation: - change from waiting state to ready state
Negative value of semaphore indicates what?
12
Binary & Counting Semaphores
 Counting Semaphore’s integer value can
range over an unrestricted domain
 Binary Semaphore is a semaphore with an
integer value that can range only between 0
and 1 (mutex)
13
Binary Semaphores
struct Binary_Semaphore {
value (0,1)
queue: list of processes
} s;
waitB(s):
if s.value ==1
then s.value =0;
else place this process in s.queue
block this process
end
14
Binary Semaphores (continued)
signalB(s):
if s.queue is empty
then s.value =1;
else remove a process from s.queue
place process P on ready list
end
Do Yourself: Article 7.4.4
15
Semaphore as a General Synchronization Tool
 Execute B in Pj only after A executed
in Pi
16
Semaphore as a General Synchronization Tool
 Execute B in Pj only after A executed
in Pi
 Use semaphore flag initialized to 0
 Code:
Pi
Pj


A
wait(flag)
signal(flag)
B
17
Sequencing example
 There are three processes, each produces
balls of some color (e.g. Red, Green, Blue)
Sequencing required:
 Red ball followed by Green followed by
Blue and so-on
 Initialization of semaphores for above
problem?
18
What happens here?
 Let S and Q be two semaphores initialized to 1
P0
P1
wait(S);
wait(Q);
wait(Q);
wait(S);


signal(S);
signal(Q);
signal(Q)
signal(S);
19
Deadlock and Starvation
 Deadlock – two or more processes are waiting
indefinitely for an event that can be caused by only
one of the waiting processes.
 Let S and Q be two semaphores initialized to 1
P0
P1
wait(S);
wait(Q);
wait(Q);
wait(S);


signal(S);
signal(Q);
signal(Q)
signal(S);
 Starvation – indefinite blocking. A process may
never be removed from the semaphore queue in 20
which it is suspended.
Classical Problems of Synchronization
 Bounded-Buffer Problem
 Readers and Writers Problem
 Dining-Philosophers Problem
Used for testing new synchronization
mechanisms
21
Bounded-Buffer Problem
 Shared data
semaphore full, empty, mutex;
Initially:
full = 0, empty = n, mutex = 1
22
Producer Process
Consumer Process
do {
do {
wait(full)
…
wait(mutex);
produce an item in nextp
…
…
remove an item from buffer
wait(empty);
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);
23
What happens here?
Producer Process
Consumer Process
do {
do {
wait(mutex);
…
wait(full)
produce an item in nextp
…
…
remove an item from buffer
wait(mutex);
to nextc
wait(empty);
…
…
signal(empty);
add nextp to buffer
signal(mutex);
…
…
signal(full);
consume the item in nextc
signal(mutex);
…
} while (1);
24
} while (1);
Readers-Writers Problem
 Data object (e.g. file, record etc.) is
shared among several concurrent
processes (could be readers or writers)
 Two or more readers can access shared
data simultaneously
 Only one writer can access it at a time
25
Readers-Writers Problem
 Shared data
semaphore mutex, wrt;
Initially
mutex = 1, wrt = 1, readcount = 0
26
Writer Process
wait(wrt);
…
writing is performed
…
signal(wrt);
27
Reader Process
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex);
28
 Deadlock free does not imply
starvation free.
29
Dining-Philosophers Problem
Allocate
several
resources
among
processes in
deadlock-free,
starvation-free
manner
 Shared data
semaphore chopstick[5];
Initially all values are 1
30
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);
31