HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm...
Transcript of HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm...
CS143ADiscussion 06: HW2 Review
Instructor: Prof. Nalini VenkatasubramanianTA: Saehanseul Yi (Hans)
May 5, 2020
HW2 Q5 Critical Section Algorithm
true? false?
true? false?
true? false?
true? false?
HW2 Q5 Critical Section Algorithm
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
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
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
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
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
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
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
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
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
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
truetrue
flag[0] = trueflag[1] = true
truetrue
• When both processes successfully set flags to true
(3) P0: waiting P1: waiting
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
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
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
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?
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?
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?
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
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
Pr1
Pr2 Pr3
Pr4
Pr5
Pr6
HW2 6 Semaphore
• Serial() and Parallel() notation• Good when implementing• We will use top-down approach
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
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
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
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
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:
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:
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:
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:
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:
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:
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;
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