Semaphores Cosc 450: Programming Paradigms 06
Total Page:16
File Type:pdf, Size:1020Kb
CoSc 450: Programming Paradigms 06 Semaphores CoSc 450: Programming Paradigms 06 Semaphore Purpose: To prevent inefficient spin lock of the await statement. Idea: Instead of spinning, the process calls a method that puts it in a special queue of PCBs, the “blocked on semaphore” queue. 71447_CH08_Chapter08.qxd 1/28/09 12:27 AM Page 422 422 Chapter 8 Process Management The PCB contains additional information to help the operating system schedule the CPU. An example is a unique process identification number assigned by the system, labeled Process ID in Figure 8.18, that serves to reference the process. Sup- pose a user wants to terminate a process before it completes execution normally, and he knows the ID number is 782. He could issue a KILL(782) command that would cause the operating system to search through the queue of PCBs, find the PCB with ID 782, remove it from the queue, and deallocate it. Another example of information stored in the PCB is a record of the total amount of CPU time used so far by the suspended process. If the CPU becomes available and the operating system must decide which of several suspended processes gets the CPU, it can use the recorded time to make a fairFigure decision. 8.19 As a job progresses through the system toward completion, it passes through several states, as Figure 8.19 shows. The figure is in the form of a state transition diagram and is another example of a finite state machine. Each transition is labeled with the event that causes the change of state. Waiting Figure 8.19 for I/O The state transition diagram for a I/O complete I/Orequest job in an operating system. Select to run Start Ready Running Finish Create Terminate Time out process process When a user submits a job for processing, the operating system creates a process for it by allocating a new PCB and attaching it to a queue of processes that are waiting for CPU time. It loads the program into main memory and sets the copy of PC in the PCB to the address of the first instruction of the process. That puts the job in the ready state. Eventually, the operating system should select the job to receive some processing time. It sets the alarm clock to generate an interrupt after a quantum of time and puts the copies of the registers from the PCB into the CPU. That puts the job in the running state. While in the running state, three things can happen: (1) The running process Transitions from the running state may time out if it is still executing when the alarm clock interrupts. If so, the oper- ating system attaches the process’s PCB to the ready queue, which puts it back in the ready state. (2) The process may complete its execution normally, in which case the last instruction it executes is an SVC to request that the operating system termi- nate it. (3) The process may need some input, in which case it executes an SVC for the request. The operating system would transfer the request to the appropriate I/O device and put the PCB in another queue of processes that are waiting for their I/O operations to complete. That puts the process in the waiting-for-I/O state. CoSc 450: Programming Paradigms 06 Add fourth state for a process 1. Ready in the ready queue 2. Running in the cpu 3. Waiting for I/O in the I/O-wait queue 4. Blocked in the blocked-on-semaphore queue Figure 8.29 Waiting for I/O I/O complete I/O request Select to run Start Ready Running Finish Create Terminate Time out process process signal(s) Waiting wait(s) for s State Changes of a Process - ª - inactive ready - running completed HY H HH ? HH H blocked M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.1 ≠ p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p ⇒# ∨ ≡ $ want p CoSc 450: Programming¬ Paradigms 06 !" ⇒# $ q9 ! ⇒# ← $ turn = 1Semaphore type Semaphore! S hasturn two= 2 fields: " S.V S.L CoSc 450: Programming Paradigms 06 Semaphore atomic operations Memorize this p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p ⇒# ∨ ≡ $ want p ¬ !" ⇒# $ q9 ! ⇒# ← $ turn = 1 ! turn = 2 " CoSc 450: Programming S .ParadigmsV 06 Semaphore Satomic.L operations Memorize this S (k, /0 ) ← wait(S) S.V > 0 S.V S.V 1 ← − S.L S.L p ← ∪ p.state ← signal(S) S.L = /0 S.V S.V + 1 ← q S.L S.L S.L q ← − { } q.state ← p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " p2 p8 turn = 2 ∧ ¬ ⇒ ¬ turn = 2 ! " " ⇒# $ turn = 2 p6 " ⇒# !"$ p3..5 p8..10 want p p6 ⇒# ∨ ≡ $ want p !" p3..5 p8..10 want¬ p ⇒# ∨ ≡!"$ want p ⇒# $ ¬ q9 !" ⇒# ! $ q9 ⇒# ← $ = ! turn 1 ⇒# ← $ turn = 1 ! turn = 2 ! turn = 2 " CoSc 450: Programming S .ParadigmsV 06 " S.V Semaphore Satomic.L operations S.L Memorize this S (k, /0 ) ← S (k, /0 ) ← wait(S) wait(S) S.V > 0 S.V > 0 S.V S.V 1 S.V S.V 1 ← − ← − S.L S.L p S.L S.L p ← ∪ ← ∪ p.state p.state ← ← signal(S) signal(S) S.L = /0 S.L = /0 S.V S.V + 1 S.V S.V + 1 ← ← q S.L q S.L S.L S.L q S.L S.L q ← − { } ← − { } q.state q.state ← ← p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 p2 p8 turn = 2 ∧ ¬ ⇒ !"¬ p3..5 p8..10 want p ! ⇒#" ∨ ≡ $ want p p2 p8 turn = 2 ¬ ∧ ¬ ⇒ ¬ turn = 2 !" ! " ⇒# $ " q9 ⇒# $ turn = 2 p6 ! " ⇒# ← $ ⇒# !"$ p3..5 turnp8..=101 want p p6 ⇒# ∨ ≡ $ want p! turn = 2 !" p3..5 p8..10 want¬ p ⇒# ∨ ≡!"$ " want p ⇒# S.V $ ¬ q9 !" ⇒# ! $ S.L q9 ⇒# ← $ = ! turn 1 ⇒# ← $ turn = 1 ! turn = 2 ! turn = 2 " S (k, /0 ) CoSc 450: Programming S .ParadigmsV ← 06 " S.V Semaphore Satomic.L wait operations(S) S.L Memorize this S.V > 0 S.V S.V 1 ← − S (k, /0 )S.L S.L p ← ← ∪ S (k, /0 ) p.state ← ← wait(S) wait(S) S.V >signal0 (S) S.V > 0 S.V S.SV.L =1 /0 S.V S.V 1 ← −S.V S.V + 1 ← − ← S.L S.L p S.L S.L p ← ∪ q S.L ← ∪ p.state p.state ← S.L S.L q ← − { } ← q.state signal(S) ← signal(S) S.L = /0 S.L = /0 S.V S.V + 1 S.V S.V + 1 ← ← q S.L q S.L S.L S.L q S.L S.L q ← − { } ← − { } q.state q.state ← ← CoSc 450: Programming Paradigms 06 Binary semaphore The value S.V is only allowed to be 0 or 1. Also called “mutex” for mutual exclusion. CoSc 450: Programming Paradigms 06 Critical section The critical section problem is trivial when you have semaphores. Algorithm 6.1: Critical section with semaphores (two processes) binary semaphore S (1, ) ¿ ; p q loop forever loop forever p1: non-critical section q1: non-critical section p2: wait(S) q2: wait(S) p3: critical section q3: critical section p4: signal(S) q4: signal(S) M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.2 ≠ p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p p2 p8 turn = 2 ⇒# ∨ ≡ $ ∧ ¬ ⇒ ¬ want p ¬ ! " !" ⇒# $ turn = 2 q9 " ! ⇒# $ ⇒# ← $ p6 turn = 1 !" p3..5 p8..10 want p! turn = 2 ⇒# ∨ ≡ $ want p " ¬ S.V !" ⇒# $ CoScq 9450: Programming Paradigms S.L 06 ! ⇒# ← $ Class exercise turn = 1 Construct the first part of the state transition diagram: ! turn = 2 S (k, /0 ) p, p, q, q, p, p," (p and q), ... ← S.V wait(S) Algorithm 6.1: Critical section with semaphores (two processes) S.L binary semaphore S (1, ) . > ¿ ;S V 0 p S.V q S.V 1 loop forever loop forever ← − p1: non-critical section q1: non-critical section p2: wait(S) q2: wait(S)S.L S.L p ← ∪ p3: criticalS section(k, /0 ) q3: criticalp section.state p4: signal(S)← q4: signal(S) ← wait(S) signal(S) S.V > 0 S.L = /0 S.V S.V + 1 S.V S.V 1 ← ← − M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.2 . S.L S.L p ≠q S L ← ∪ S.L S.L q p.state ← − { } ← q.state ← signal(S) S.L = /0 S.V S.V + 1 ← q S.L S.L S.L q ← − { } q.state ← p1,q1 (1, /0 ) p1,q1 p2,q1 p1,q1 (1, /0 ) (1, /0 ) (1, /0 ) p2,q1 p2,q1 p3,q1 (1, /0 ) (0, /0 ) (1, /0 ) p1,q1 p3,q1 (1, /0 ) p1,q1 p3,q2 p(03,,/0q)1 (1, /0 ) (0, /0 ) (0, /0 ) p2,q1 p3,q2 p1,q1 (p11, ,/0 q) 1 p2,q1 p3,q3 p(03,,/0q)2 (1, /0 ) ! (1, /0 ) (1, /0 ) (0, q ) (0, /0 ) p3,q1 p2,q1 { } p3,q3! p1,q1 (p02, ,/0 q) 1 (p13,,/0 q)1 (0, q ) p4,q3! p3,q3! (1, {/0 ) } p(31,,q/0 2) (0, /0 ) A (0, q ) B (0, q ) C D p3,q1 { } p4,{q3}! (0, /0 ) p1,q1 p p2,q1 p p3,q1 q (p03,,/0 q)2 (0, q ) p(11,,/0q)3 p4,q3! (1, {/0 ) } p(30,,q/0 3)! (p03,,q/0 )2 (0, /0 ) (0, q ) p1,{q3} (0, q ) (0, /0 )q p2,q1 p3,q1 F p3{,q}2 E p3,q3! (0, /0 ) p(12,,/0q)3 p (p01,,/0 q)3 p p(40,,q/0 3)! p (p03,,qq3! ) { } (0, /0 ) (0, q ) (0, q ) (0, /0 ) p2,q3 { } { } p3,q1 p3,q2 p3,q3 p4,q3! (0, /0 q) ! (0, /0 ) (0, /0 ) p(1,,q3q ) (p04,,qq3! ) p1,q4 p2,q3 0 { } (0, /0 {) } (0, q ) (0, /0 ) (p01,,/0q)4 { } p3,q2 p3,q3! p4,q3! p1,q3 (0, /0 ) p ,q p1,q3 (0, /0 ) (0, q ) (20, 3q ) (0, /0 ) p1,{q4} (0, /0 {) } (0, /0 ) p3,q3 (0, /0 ) States !E and F havep4,q no3! transitionp1 on,q3 q becausep q2, ,isqq 3 p1,q4 2 3 blocked.(0, q ) q3’ means(0, qq3) cannot execute.(0, /0 ) (00,,/0 /0 )) { } { } (0, /0 ) p4,q3! p1,q3 p2,q3 p11,,qq44 (0, q ) (00,,/0 /0 )) { } (0, /0 ) (0, /0 ) p1,q3 p2,q3 p1,q4 (0, /0 ) (0, /0 ) (0, /0 ) p2,q3 p1,q4 (0, /0 ) (0, /0 ) p1,q4 (0, /0 ) p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p p2 p8 turn = 2 ⇒# ∨ ≡ $ ∧ ¬ ⇒ ¬ want p ! " ¬ !" ⇒# $ turn = 2 q9 " ! ⇒# $ ⇒# ← $ p6 turn = 1 !" p3..5 p8..10 want p! turn = 2 ⇒# ∨ ≡ $ want p " ¬ S.V !" ⇒# $ CoScq 9450: Programming Paradigms S.L 06 ! ⇒# ← $ turn = 1 Class exercise ! turn = 2 Construct the complete state transitionS ( kdiagram, /0 ) " ← S.V Algorithm 6.2: Critical section withwait semaphores(S) (two proc., abbrev.) S.L binary semaphore S (1S, .V) > 0 ¿ ; p S.V S.Vq 1 ← − loop forever loop forever p1: wait(S) q1: wait(S)S.L S.L p ← ∪ p2: signal(S) S (k, /0 ) q2: signal(S)p.state ← ← wait(S) signal(S) S.V > 0 S.L = /0 S.V S.V + 1 S.V S.V 1 ← ← − S.L S.L p q S.L ← ∪ S.L S.L q p.state ← − { } M.