Post on 29-Jul-2020
CS143ADiscussion 06: HW2 Review
Instructor: Prof. Nalini VenkatasubramanianTA: Saehanseul Yi (Hans)
May 5, 2020
Agenda
• HW2 Q5, Q6
saehansy@uci.edu 1
HW2 Q5 Critical Section Algorithm
saehansy@uci.edu 2
true? false?
true? false?
true? false?
true? false?
HW2 Q5 Critical Section Algorithm
saehansy@uci.edu 3
true? false?
true? false?
Bad news.. there is no systematic way to find out if mutual exclusion is satisfied
true? false?
true? false?
HW2 Q5 Critical Section Algorithm
saehansy@uci.edu 4
Bad news.. there is no systematic way to find out if mutual exclusion is satisfied
Init
Waiting
Critical Section(CS)
Remainder Section(RS)
HW2 Q5 Critical Section Algorithm
saehansy@uci.edu 5
Bad news.. there is no systematic way to find out if mutual exclusion is satisfiedInitWaitingCSRS
InitWaitingCSRS
Examine case by case
P0 P1
But not every case..(1) They’re symmetric(2) Some of them are closely related
(e.g. init-waiting -> init->CS)
We’re going to look below three cases
(1) init-init(2) RS-waiting(3) waiting-waiting
(1) P0: init P1: init
saehansy@uci.edu 6
true? false?
true? false?
TIP: fix one process at some point and make the other one proceed • Let’s focus on CS
(1) P0: init P1: init
saehansy@uci.edu 7
true? false?
true? false?
• Only a while loop is above the critical section..
• In what case both processescan proceed to CS?
(1) P0: init P1: init
saehansy@uci.edu 8
true? false?
true? false?
• Only a while loop is above the critical section..
• In what case both processescan proceed to CS?
à if both flags are false
(1) P0: init P1: init
saehansy@uci.edu 9
true? false?
• Only a while loop is above the critical section..
• In what case both processescan proceed to CS?
à if both flags are false• If one of them examine ‘while’
condition before the other sets flag: No problem
truetrue
flag[0] = trueflag[1] = false
(2) P0: RS P1: waiting
saehansy@uci.edu 10
true
true? false?
• If P1 was waiting, it should be on line 5 (no-op)
• When P0 sets flag[0] to false,P1 can break out of the loop
true
flag[0] = falseflag[1] = false
(2) P0: RS P1: waiting
saehansy@uci.edu 11
true
true? false?
• If P1 was waiting, it should be on line 5 (no-op)
• When P0 sets flag[0] to false,P1 can break out of the loop
• P1 sets flag[1] to ____• flag[1] affects P0true
flag[0] = falseflag[1] = ???
(2) P0: RS P1: waiting
saehansy@uci.edu 12
true
• If P1 was waiting, it should be on line 5 (no-op)
• When P0 sets flag[0] to false,P1 can break out of the loop
• P1 sets flag[1] to ____• flag[1] affects P0• To prevent P0 from entering
CS, we should make flag[1]true while P1 is executing CS
true
flag[0] = falseflag[1] = true
truetrue
(3) P0: waiting P1: waiting
saehansy@uci.edu 13
truetrue
flag[0] = trueflag[1] = true
truetrue
• When both processes successfully set flags to true
(3) P0: waiting P1: waiting
saehansy@uci.edu 14
truetrue
flag[0] = trueflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
(3) P0: waiting P1: waiting
saehansy@uci.edu 15
truetrue
flag[0] = falseflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
• Now both flags are false, any one of them could break out of the loop
• It depends on who reachesline 2 first
(3) P0: waiting P1: waiting
saehansy@uci.edu 16
truetrue
flag[0] = falseflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
• Now both flags are false, any one of them could break out of the loop
• It depends on who reachesline 2 first
• If P0 reaches first, flag[0] would have been set to trueà P1 will be stuck in the loop againà P0 proceed to CS
(3) P0: waiting P1: waiting
saehansy@uci.edu 17
truetrue
flag[0] = falseflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
• Now both flags are false, any one of them could break out of the loop
• It depends on who reachesline 2 first
• If P0 reaches first, flag[0] would have been set to trueà P1 will be stuck in the loop againà P0 proceed to CS
• They can’t proceed to CS atthe same time
• Mutual exclusion is satisfied
Progress?
saehansy@uci.edu 18
truetrue
flag[0] = falseflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
• Now both flags are false, any one of them could break out of the loop
• It depends on who reachesline 2 first
• Whoever reaches, the flag for the other will be set to trueà will be stuck in the loop
• When P0 reaches line 2, P1 could’ve set flag[1] to true
• When P1 reaches line 2, P2 could’ve set flag[0] to true
Bounded Waiting?
saehansy@uci.edu 19
truetrue
flag[0] = falseflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
• Now both flags are false, any one of them could break out of the loop
• It depends on who reachesline 2 first
• Whoever reaches, the flag for the other will be set to trueà will be stuck in the loop
• There is no order between two processes
Bounded Waiting?
saehansy@uci.edu 20
truetrue
flag[0] = falseflag[1] = false
truetrue
• When both processes successfully set flags to true
• If one of them proceed, that process will end up in line 5
• Now both flags are false, any one of them could break out of the loop
• It depends on who reachesline 2 first
• Whoever reaches, the flag for the other will be set to trueà will be stuck in the loop
• There is no order between two processes
• They will compete and one ofthem might win every time
HW2 6 Semaphore
• Dots can be interpreted as system states• State transition occurs when all the arcs are satisfied
(e.g. the last state needs Pr1, Pr5 & Pr6)• There is an execution order
(e.g. Pr5 cannot run before Pr3 and Pr2because Pr5 can run only after Pr3and Pr3 can run only after P2)• We want to enforce this order with semaphores
saehansy@uci.edu 21
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
saehansy@uci.edu 22
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
saehansy@uci.edu 23
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6Box AParallel ( Pr1, Box A )
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
saehansy@uci.edu 24
Pr2 Pr3
Pr4
Pr5
Pr6Box BParallel ( Pr1, Box A )Box A = Serial ( Pr2, Box B)
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
saehansy@uci.edu 25
Pr3
Pr4
Pr5
Pr6
Box C
Parallel ( Pr1, Box A )Box A = Serial ( Pr2, Box B)Box B = Parallel (Box C, Box D) Box D
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
saehansy@uci.edu 26
Pr3
Pr4
Pr5
Pr6
Box C
Parallel ( Pr1, Box A )Box A = Serial ( Pr2, Box B)Box B = Parallel (Box C, Box D)Box C = Serial (Pr3, Pr5)Box D = Serial (Pr4, Pr6)
Box D
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
saehansy@uci.edu 27
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6Parallel ( Pr1, Box A )Box A = Serial ( Pr2, Box B)Box B = Parallel (Box C, Box D)Box C = Serial (Pr3, Pr5)Box D = Serial (Pr4, Pr6)Parallel (Pr1, Serial(Pr2, Parallel(Serial(Pr3, Pr5), Serial (Pr4, Pr6)) ))
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1:• Pr2:• Pr3:• Pr4:• Pr5:• Pr6:
saehansy@uci.edu 28
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2:• Pr3:• Pr4:• Pr5:• Pr6:
saehansy@uci.edu 29
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; • Pr3:• Pr4:• Pr5:• Pr6:
saehansy@uci.edu 30
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; V(s2); • Pr3: P(s2); body; • Pr4:• Pr5:• Pr6:
saehansy@uci.edu 31
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; V(s2); V(s2);• Pr3: P(s2); body; • Pr4: P(s2); body; • Pr5:• Pr6:
saehansy@uci.edu 32
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; V(s2); V(s2);• Pr3: P(s2); body; V(s3);• Pr4: P(s2); body; • Pr5: P(s3); body;• Pr6:
saehansy@uci.edu 33
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
HW2 6 Semaphore
• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; V(s2); V(s2);• Pr3: P(s2); body; V(s3);• Pr4: P(s2); body; V(s4);• Pr5: P(s3); body;• Pr6: P(s4); body;
saehansy@uci.edu 34
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
Body
V(sem)
Body
P(sem)Pr A Pr B
waiting some process
signal
lock_release()signal()…
lock_acquire()wait()…
<Example: We want Pr B to wait until Pr A finishes>
• Thank you • Q&A
saehansy@uci.edu 41