Operating_Systems/Assignment/Assignment1/source/21281280-柯劲帆-课后习题1.md
2024-09-05 13:31:02 +08:00

108 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>课程作业</center></h1>
<div style="text-align: center;">
<div><span style="display: inline-block; width: 65px; text-align: center;">课程名称</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 210px; font-weight: bold; text-align: left;">操作系统</span></div>
<div><span style="display: inline-block; width: 65px; text-align: center;">作业名称</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 210px; font-weight: bold; text-align: left;">课后习题1</span></div>
<div><span style="display: inline-block; width: 65px; text-align: center;">学号</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 210px; font-weight: bold; text-align: left;">21281280</span></div>
<div><span style="display: inline-block; width: 65px; text-align: center;">姓名</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 210px; font-weight: bold; text-align: left;">柯劲帆</span></div>
<div><span style="display: inline-block; width: 65px; text-align: center;">班级</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 210px; font-weight: bold; text-align: left;">物联网2101班</span></div>
</div>
---
## 1. 问题1
**哲学家进餐问题的变形。假设5支筷子都放在圆桌的中间哲学家进餐时拿起任意两支筷子就可以进餐。其他条件不变。请用信号量实现5个哲学家进程的同步。**
```pascal
Var
chopsticks, eating_philosophers: Semaphore := 5, 4;
Procedure Philosopher(i: integer);
Begin
Repeat
wait(eating_philosophers);
wait(chopsticks);
wait(chopsticks);
Eat;
signal(chopsticks);
signal(chopsticks);
signal(eating_philosophers);
Think;
Until False;
End;
```
## 2. 问题2
**四个进程P0,P1,P2,P3和四个信箱M0,M1,M2,M3进程间借助相邻的信箱传递消息Pi每次从Mi中取出一条消息经加工送入Mi+1(mod 4)中。其中M0,M1,M2,M3分别设有3,3,2,2个格子每个格子放一条消息初始时M0装满了三条消息其余为空。写出使用信号量实现进程 (i=0,1,2,3)同步的算法。**
```pascal
Var
full, empty, mutex: array[0..3] of Semaphore := (3, 0, 0, 0), (0, 3, 2, 2), (1, 1, 1, 1);
Procedure ProcessPi(i: integer);
Var
message: Message;
Begin
Repeat
wait(full[i]);
wait(mutex[i]);
message := Get_Message_From_Mailbox(i);
signal(mutex[i]);
signal(empty[i]);
ProcessMessage;
wait(empty[(i + 1) mod 4]);
wait(mutex[(i + 1) mod 4]);
Send_Message_To_Mail_box((i + 1) mod 4, message);
signal(mutex[(i + 1) mod 4]);
signal(full[(i + 1) mod 4]);
Until False;
End;
```
## 3. 问题3
**汽车司机与售票员之间必须协同工作,汽车每到一个站后,一方面,只有当司机已经停下,售票员才能开门上下客,另一方面,只有售票员把车门关好了司机才能开车。假定某辆公共汽车上有前后门各有一名售票员以及一名司机,汽车初始状态是正在始发站停车上客。试设必要的信号量及赋初值,实现售票员进程和司机进程的同步。**
```pascal
Var
driver, ticket_seller: Semaphore := 1, 2;
Procedure Driver;
Begin
Repeat
wait(driver);
Move;
Stop;
signal(ticket_seller);
signal(ticket_seller);
Until False;
End;
Procedure Ticket_seller(i: integer);
Begin
Repeat
wait(ticket_seller);
wait(ticket_seller);
Open;
signal(driver);
Close;
Until False;
End;
```