V0.3 CSS430 Process Synchronization Textbook Chapter 6 Instructor: Stephen G. Dame e-mail: [email protected]These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne), Professor Munehiro Fukuda and the instructor’s class materials. 1 CSS430 Operating Systems : Process Synchronization
CSS430 Process Synchronization Textbook Chapter 6. Instructor: Stephen G. Dame e -mail: [email protected]. These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne), Professor Munehiro Fukuda and the instructor’s class materials. WKP 17. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
V0.3 1CSS430 Operating Systems : Process Synchronization
These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne), Professor Munehiro Fukuda and the instructor’s class materials.
V0.3 2
WKP 17
“Programming is the process of converting caffeine into error
messages” - Unknown
V0.3 3CSS430 Operating Systems : Process Synchronization
Learning Objectives
Introduction to the Critical-Section Problem
Discuss HW & SW solutions to C-S Problem
Atomic Transactions and mechanisms Java Synchronization techniques:
Mutex Semaphores Monitors
V0.3 4CSS430 Operating Systems : Process Synchronization
Revisiting Bounded Buffer
Producer Process
I I I
Buffer[0] [1] [2] [3] [4]
Consumer Process
in=4out=1
Let’s Dissect the problem!
V0.3 5CSS430 Operating Systems : Process Synchronization
Race Condition
The outcome of concurrent thread execution depends on the particular order in which the access takes place race condition!
V0.3 6CSS430 Operating Systems : Process Synchronization
The critical section is a block of code in which no two processes
can be executing their instructions at the same time.
The Critical Section (“CS”)
while (true) {entry sectioncritical sectionexit sectionremainder section
}
V0.3 7CSS430 Operating Systems : Process Synchronization
Critical Section (3) Requirements
① Mutual Exclusion. If process Pi is executing in its critical section(CS), then no other processes can be executing in their corresponding critical sections.
② Progress. If no process is executing in its CS, and there exist some processes that wish to enter their CS, then the selection of the processes that will enter the CS next cannot be postponed indefinitely.
③ Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their CS after a process has made a request to enter its CS and before that request is granted.
Critical Section
① only one process
When exiting from CS
② Pick up a process to enter
③ Bounded times
When entering CS
V0.3 8CSS430 Operating Systems : Process Synchronization
Peterson’s Solution (Algorithm)
int turn; // shared varboolean flag[2]; // shared var
Thread 0 finds out flag[1] is true, and waits for Thread 1.
V0.3 14CSS430 Operating Systems : Process Synchronization
Algorithm 3 (Mixed of 1 and 2)
(Peterson’s Solution)
Complies with all three CS rules– Even in case both threads declared, will enter CS in an
orderly manner Turn eventually points to either thread A or B!
V0.3 15CSS430 Operating Systems : Process Synchronization
There no guarantees that Peterson’s solution will work
correctly on modern computer architectures due to complex
load and store instructions
No Guarantees!
V0.3 16CSS430 Operating Systems : Process Synchronization
Discussion 1① What is the definition of atomicity, and where does it
apply?
② What is the meaning of busy waiting? What is an alternative to busy waiting?
③ Fill out the following table with your analysis of the different CS methods:
Advantages Disadvantages Implementation(HW, OS, or Language)
TestandSet,Swap
Semaphore
Monitor
V0.3 17CSS430 Operating Systems : Process Synchronization
Synchronization
Software solutions: Algorithm 3 (Peterson’s Solution) works only for a pair of threads.
How about a mutual execution (n > 2) threads? Lamport’s Algorithm (See Appendix).
Interrupt Masking: Disables even time interrupts, thus not allowing preemption. Malicious user program may hog CPU forever. Operating systems using this technique are not broadly scalable
Hardware solutions: Modern machines provide special atomic hardware
instructions Many systems provide hardware support for critical section code Atomic = non-interruptible sequence of instructions
Test-and-set (or read-modify-write) Swap contents of two memory words
V0.3 18CSS430 Operating Systems : Process Synchronization
Critical Section (“CS”) Locks
while (true) {acquire lockcritical sectionrelease lockremainder section
}
V0.3 19CSS430 Operating Systems : Process Synchronization
Hardware Data Simulator
V0.3 20CSS430 Operating Systems : Process Synchronization
Sleep Utilities
V0.3 21CSS430 Operating Systems : Process Synchronization
Mutual Exclusion
V0.3 22CSS430 Operating Systems : Process Synchronization
Worker Thread – Test and Set
V0.3 23CSS430 Operating Systems : Process Synchronization
H/W Algorithm Test and Set
V0.3 24CSS430 Operating Systems : Process Synchronization
Test and Set Factory
V0.3 25CSS430 Operating Systems : Process Synchronization
Run!
Test and Set
V0.3 26CSS430 Operating Systems : Process Synchronization
H/W Algorithm Swap
V0.3 27CSS430 Operating Systems : Process Synchronization
Worker Thread - Swap
V0.3 28CSS430 Operating Systems : Process Synchronization
Swap Factory
V0.3 29CSS430 Operating Systems : Process Synchronization
Run!
Swap
V0.3 30CSS430 Operating Systems : Process Synchronization
Semaphore Synchronization tool that does not require busy waiting at a
user level Semaphore S – integer variable Two standard operations modify S: acquire() and release()
Originally called P() and V() [Dutch P proberen, meaning “to test ”) and V (from verhogen, meaning “to increment”]
Less complicated (AND implemented by import java.util.concurrent.*)
Can only be accessed via two indivisible (atomic) operationsP V
P V
P V
acquire( ) { while value <= 0 ; // no-op value--;}
release( ) { value++; wakeup( );}
V0.3 31CSS430 Operating Systems : Process Synchronization
Semaphore Eliminating Busy-Waiting
Bee1Bee3
Bee0
Bee2
Bee4
P V
Bee1Bee3
Bee0
Bee2
Bee4
P V
Waiting List
Waiting List
Wake one up
V0.3 32CSS430 Operating Systems : Process Synchronization
Semaphore Worker Thread
Private Data+
Constructor
Main Body of Worker BEE
Critical and Remainder Sections Sim
V0.3 33CSS430 Operating Systems : Process Synchronization
Semaphore FactorySemaphoreFactoryInitialize and Start N “worker bees”
V0.3 34CSS430 Operating Systems : Process Synchronization
Run!
Semaphore
V0.3 35CSS430 Operating Systems : Process Synchronization
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 1P0 P1
P(S); P(Q);P(Q); P(S); V(Q); V(S);V(S); V(Q);
Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. What if processes are waiting at P(S) in LIFO order
V0.3 36CSS430 Operating Systems : Process Synchronization
Classical problem 1:Bounded-Buffer Problem
mutex.P( )
mutex.V( )
empty.P( )(empty--)
empty.V( )(empty++)
full.P( )(full--)
full.V( )(full++)
signalsig
nal
producer consumer
V0.3 37CSS430 Operating Systems : Process Synchronization
Insert and Remove Methods
Lock
Unlock 1
Lock
Unlock 1
V0.3 38CSS430 Operating Systems : Process Synchronization
Producer and Consumer Threads
buffer
V0.3 39CSS430 Operating Systems : Process Synchronization
Bounded Buffer Problem: Factory
V0.3 40CSS430 Operating Systems : Process Synchronization
Concurrent Programming“Programming concurrent applications is a
difficult and error-prone undertaking” – Dietel**
When thread synchronization is required use the following guidelines (in order of complexity): ① Use existing classes from the Java API
(e.g. import java.util.concurrent.*)
② Use synchronized keyword and Object methods wait, notify and notifyAll
V0.3 41CSS430 Operating Systems : Process Synchronization
Java Thread Model
task com
pletes
New
runnable
timed waiting terminatedwaiting blocked
acquire lock, interrupt,I/O completes
notify
notifyAll
wait
issue I/O request
enter synchronized
statement
Inte
rval
ex
pire
s N
otify
notif
yAllwait
sleep
V0.3 42CSS430 Operating Systems : Process Synchronization
Monitors
High-level language construct Only one process allowed in a monitor, thus
executing its method A process in the monitor can wait on a
condition variable, say x, thus relinquishing the monitor and allowing another process to enter
A process can signal another process waiting on a condition variable (on x).
A process signaling another process should exit from the monitor, because the signal process may have begun to work in the monitor.
MethodAMethodB
MethodCx.wait( );
x.signal( )
X:
Y: p5
p1p4
p1
p3
p2
p8p7p6Entry queue
“…the happens-before relationship. This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement. “
V0.3 43CSS430 Operating Systems : Process Synchronization
Java Synchronization
JavaSE Reference on Synchronization (PLEASE READ IN DETAIL!)