HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm...

36
CS143A Discussion 06: HW2 Review Instructor: Prof. Nalini Venkatasubramanian TA: Saehanseul Yi (Hans) May 5, 2020

Transcript of HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm...

Page 1: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

CS143ADiscussion 06: HW2 Review

Instructor: Prof. Nalini VenkatasubramanianTA: Saehanseul Yi (Hans)

May 5, 2020

Page 2: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

Agenda

• HW2 Q5, Q6

[email protected] 1

Page 3: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 Q5 Critical Section Algorithm

[email protected] 2

true? false?

true? false?

true? false?

true? false?

Page 4: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 Q5 Critical Section Algorithm

[email protected] 3

true? false?

true? false?

Bad news.. there is no systematic way to find out if mutual exclusion is satisfied

true? false?

true? false?

Page 5: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 Q5 Critical Section Algorithm

[email protected] 4

Bad news.. there is no systematic way to find out if mutual exclusion is satisfied

Init

Waiting

Critical Section(CS)

Remainder Section(RS)

Page 6: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 Q5 Critical Section Algorithm

[email protected] 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

Page 7: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(1) P0: init P1: init

[email protected] 6

true? false?

true? false?

TIP: fix one process at some point and make the other one proceed • Let’s focus on CS

Page 8: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(1) P0: init P1: init

[email protected] 7

true? false?

true? false?

• Only a while loop is above the critical section..

• In what case both processescan proceed to CS?

Page 9: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(1) P0: init P1: init

[email protected] 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

Page 10: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(1) P0: init P1: init

[email protected] 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

Page 11: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(2) P0: RS P1: waiting

[email protected] 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

Page 12: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(2) P0: RS P1: waiting

[email protected] 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] = ???

Page 13: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(2) P0: RS P1: waiting

[email protected] 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

Page 14: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(3) P0: waiting P1: waiting

[email protected] 13

truetrue

flag[0] = trueflag[1] = true

truetrue

• When both processes successfully set flags to true

Page 15: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(3) P0: waiting P1: waiting

[email protected] 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

Page 16: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(3) P0: waiting P1: waiting

[email protected] 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

Page 17: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(3) P0: waiting P1: waiting

[email protected] 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

Page 18: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

(3) P0: waiting P1: waiting

[email protected] 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

Page 19: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

Progress?

[email protected] 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

Page 20: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

Bounded Waiting?

[email protected] 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

Page 21: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

Bounded Waiting?

[email protected] 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

Page 22: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

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

[email protected] 21

Pr1

Pr2 Pr3

Pr4

Pr5

Pr6

Page 23: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• Serial() and Parallel() notation• Good when implementing• We will use top-down approach

[email protected] 22

Pr1

Pr2 Pr3

Pr4

Pr5

Pr6

Page 24: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• Serial() and Parallel() notation• Good when implementing• We will use top-down approach

[email protected] 23

Pr1

Pr2 Pr3

Pr4

Pr5

Pr6Box AParallel ( Pr1, Box A )

Page 25: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• Serial() and Parallel() notation• Good when implementing• We will use top-down approach

[email protected] 24

Pr2 Pr3

Pr4

Pr5

Pr6Box BParallel ( Pr1, Box A )Box A = Serial ( Pr2, Box B)

Page 26: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• Serial() and Parallel() notation• Good when implementing• We will use top-down approach

[email protected] 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

Page 27: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• Serial() and Parallel() notation• Good when implementing• We will use top-down approach

[email protected] 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

Page 28: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• Serial() and Parallel() notation• Good when implementing• We will use top-down approach

[email protected] 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)) ))

Page 29: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1:• Pr2:• Pr3:• Pr4:• Pr5:• Pr6:

[email protected] 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>

Page 30: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2:• Pr3:• Pr4:• Pr5:• Pr6:

[email protected] 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>

Page 31: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; • Pr3:• Pr4:• Pr5:• Pr6:

[email protected] 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>

Page 32: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

HW2 6 Semaphore

• s1 = 0; s2 = 0; s3 = 0; s4 = 0;• Pr1: body;• Pr2: body; V(s2); • Pr3: P(s2); body; • Pr4:• Pr5:• Pr6:

[email protected] 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>

Page 33: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

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:

[email protected] 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>

Page 34: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

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:

[email protected] 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>

Page 35: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

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;

[email protected] 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>

Page 36: HW2 Reviewics143/discussions/CS143_SQ20_Discussio… · HW2 Q5 Critical Section Algorithm saehansy@uci.edu 5 Bad news.. there is no systematic way to find out if mutual exclusion

• Thank you • Q&A

[email protected] 41