Title: Wednesday, June 21, 2006
1Wednesday, June 21, 2006
- "The future belongs to him who knows how to wait.
" - - Russian Proverb
2- 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.
3- Preemptive kernels
- Solaris
- Non-preemptive kernels
- Windows, Unix, Linux 2.6 and above
4Problems of Synchronization
5- 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.
6(No Transcript)
7Semaphore
- Semaphore is an integer variable that apart from
initialization is accessed only through two
atomic operations - wait
- signal
8Semaphore 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.
9Implementation
- Semaphore operations now defined as
- wait(S) S.value--
- if (S.value lt 0)
- add this process to S.L block
-
- signal(S) S.value
- if (S.value lt 0)
- remove a process P from S.L wakeup(P)
-
10Critical 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?
11- 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?
12wait operation - waiting queue associated with
semaphore signal operation - change from waiting
state to ready state Negative value of semaphore
indicates what?
13Binary Counting Semaphores
- Counting Semaphores 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)
14Binary 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
15Binary 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
16Semaphore as a General Synchronization Tool
- Execute B in Pj only after A executed in Pi
17Semaphore 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
18Sequencing 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?
19What 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)
20Deadlock 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
which it is suspended.
21Classical Problems of Synchronization
- Bounded-Buffer Problem
- Readers and Writers Problem
- Dining-Philosophers Problem
- Used for testing new synchronization mechanisms
22Bounded-Buffer Problem
- Shared datasemaphore full, empty,
mutexInitiallyfull 0, empty n, mutex 1
23Producer Process
Consumer Process
do wait(full) wait(mutex) remove an
item from buffer to nextc signal(mutex) sig
nal(empty) consume the item in nextc
while (1)
do produce an item in
nextp wait(empty) wait(mutex) add
nextp to buffer signal(mutex) signal(full)
while (1)
24What happens here?
Producer Process
Consumer Process
do wait(mutex) wait(full) remove an
item from buffer to nextc signal(empty) sig
nal(mutex) consume the item in nextc
while (1)
do produce an item in
nextp wait(mutex) wait(empty) add
nextp to buffer signal(full) signal(mutex)
while (1)
25Readers-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
-
-
26Readers-Writers Problem
- Shared datasemaphore mutex, wrtInitiallymut
ex 1, wrt 1, readcount 0 -
-
27Writer Process
wait(wrt) writing is performed
signal(wrt)
28Reader Process
wait(mutex) readcount if (readcount
1) wait(wrt) signal(mutex)
reading is performed wait(mutex)
readcount-- if (readcount
0) signal(wrt) signal(mutex)
29- Deadlock free does not imply starvation free.
30Dining-Philosophers Problem
Allocate several resources among processes in
deadlock-free, starvation-free manner
- Shared data
- semaphore chopstick5
- Initially all values are 1
31Dining-Philosophers Problem
- Philosopher i
- do
- wait(chopsticki)
- wait(chopstick(i1) 5)
-
- eat
-
- signal(chopsticki)
- signal(chopstick(i1) 5)
-
- think
-
- while (1)