Chapter 3 Problems

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Chapter 3 Problems

PROBLEM 1

List 5 real applications (you have used) that use IPC

- Multi-threading
- Operating system: MacOS, Windows, Linux, …
- Web browsers: Safari, Google Chrome, …
- Graphic processing in video games: League of Legends, Team Fight Tactics, …
- Messaging Apps: Messenger, Zalo, Telegram, …

PROBLEM 2

List 5 applications / situations in real worlds that need synchronization

- Traffic Systems
- Watching online videos: Youtube, Netflix, …
- Manufacturing processes: manufacturing a computer, a chip, …
- Online gaming: Candy Crushes Saga, …
- GPS

PROBLEM 3

For all the classical synchronization problems, run step by step a certain sequence of instructions among
the processes. Update the state of the used semaphores, and prove such sequence meets all the
requirements of the synchronization constraints.

*Bounded buffer problem: M = 1, E = 3, F = 0

R1 R2 W R3 W

R1:

Wait(F) -> F = -1 -> Queue S -> L, block R1

R2:

Wait(F) -> F = -2 -> Queue S -> L, block R2

W:

Wait(E) -> E = 2

Wait(M) -> M = 0

Write

Signal(M) -> M = 1
Signal(F) -> F = -1;

R3:

Wait(F) -> F = -2 -> Queue S -> L, block R3

W:

Wait(E) -> E = 1

Wait(M) -> M = 0

Write

Signal(M) -> M = 1

Signal(F) -> F = -1;

*Reader-writers problem: wrt = 1, readcount = 0, M = 1

R1 – R2 – W:

R1:

Wait(M) -> M = 0

Readcount++ -> readcount = 1;

Wait(wrt) = 0

Signal(M) -> M = 1

R2:

Wait(M) -> M = 0

Readcount++ -> readcount = 2;

Signal(M) = M = 1

R1:

Read(data);

wait(M) -> M = 0

readcount-- -> readcount = 1;

signal(M) -> M = 1

R2:

Read(data);

wait(M); -> M = 0

2
readcount-- -> readcount = 0

signal(wrt) -> wrt = 1;

signal(M) -> M = 1

W:

wait(wrt) -> wrt = 0

write(data_set);

signal(wrt) -> wrt = 1

*Dining-philosophers problem: chopstick[5]

-P2-P1-P3

P2:

Wait(chop[2]) chop2 =0;

Wait(chop3) chop3=0;

P1:

Wait(chop1) chop1=0;

Wait(chop2) chop2=-1;

P3:

Wait(chop3) chop3=-1;

Wait(chop4) chop4=0;

P2:

Eat

Signal(chop2) ->chop2 =0 ->remove P1 from queue

Signal(chop3) ->chop3 =0 ->remove P3 from queue

Think

P3:

Eat

Signal(chop3) ->chop2 =1

Signal(chop4) ->chop4 =1

3
Think

P1:

Eat

Signal(chop1) ->chop1 =1

Signal(chop2) ->chop2 =1

Think

PROBLEM 4: BARRIER

Barrier is a synchronization problem with the following constraints:

+ We have N processes

+ We need a point at which all the processes MUST reach in order to continue next instructions

An example in the above figure:

+ Figure a) 4 processes running concurrently

+ Figure b) 3 processes reach the barrier, they MUST wait for process C which hasn’t reached the barrier

+ Figure c) when all the processes reach the barrier, all of them can proceed with their next instructions

Use the semaphore to solve this synchronization problem. Write the program is pseudo-code like those
in the slides Hint: used counting semaphore.

4
(Ví dụ trong thực tế, chuyến xe bus chở khách du lịch đi về, cần phải đảm bảo mọi người lên xe thì xe
mới khởi hành đi về được)

N initialized with 4, B = 0, M = 1

Process {

toBarrier()

wait(M);

count++;

signal(M);

if(count == N) signal(B)

wait(B)

signal(B)

passBarrier()

PROBLEM 5: TWO GOATS GOING OVER THE BRIDGE

Use semaphore to solve the famous story:

+ A narrow bridge which can allow a SINGLE goat to go over

+ Two goats want to go over the bridge.

+ Each goat is on one side of the bridge

Process:

M initialized with 1:

While(True) {

Wait(M) -> M = 0 (1 con dê đi trước)

Walkby(Goat, bridge)

Singal(M) -> M = 1 (1 con dê đã đi qua)

5
Hint: divide the bridge into 2 sides, each side is a shared resource. (Chia cây cầu thành 2 nửa, mỗi nửa là
một tài nguyên chia sẻ => bài toán trở về bài toán cổ điển: bữa ăn tối của triết gia với 2 người)

PROBLEM 6: CROSSROAD

This is a real-word problem: crossroad is a place where two roads meet and cross each other

Suppose each road has two lanes, and the road only allow us to move straight forward (no turning left or
right).

Use semaphore to solve the problem

Road[4] road

Vehicle {

Wait(Road[i]);

Wait(Road[i+2] % 4);

GoStraight(vehicle, road);

Signal(Road[i]);

Signal(Road[i+2] % 4);

6
Hint: divide the crossroad into 4 conners, each conner is a shared resource (Chia ngã 4 thành 4 nửa, mỗi
góc là một tài nguyên chia sẻ=> bài toán trở về bài toán cổ điển: bữa ăn tối của triết gia với 4 người, chỉ
có điều tài nguyên bây giờ không phải là tài nguyên bên trái và bên phải nữa)

You might also like