操作系统学习笔记一

经典进程同步问题

在这里插入图片描述

1 生产者-消费者问题
生产者-消费者问题是相互合作的进程关系的一种抽象,比如输入时,输入进程是生产者,计算进程是消费者,输出时,计算进程是生产者,打印进程是消费者。

解决方案一:用记录型信号量解决
假设生产者消费者之间有一共用缓冲池,具有n个缓冲区,利用互斥信号量mutex实现诸进程对缓冲池的互斥作用。empty表示空缓冲池数,full表示满缓冲池数。则可描述为:

Var mutex,empty,full:semaphore:= 1, n, 0;
buffer:array[0,…,n-1]of item;
in, out:integer:=0,0;//指向缓冲区的指针
begin
parbegin
procedure:begin
repeat

producer an item nextp //将一个单位资源放进nextp缓冲变量

wait(empty);//若缓冲区有空位
wait(mutex);//并且缓冲区空闲
buffer[in] := nextp;//将nextp里的值放进缓冲区
in:=(in+1)mod n;//指针指向下一个空的缓冲区
signal(mutex);//解锁缓冲区
signal(full);//满的缓冲区数加一
until false;
end
consumer:begin
repeat
wait(full);
wait(mutex);
nextc:=buffer(out);
out:=(out+1) mod n;
signal(mutex);
signal(empty);
consumer the item in nextc;
until false;
end
parend
end

解决方案二:利用AND信号量解决
用SWait(empty,mutex)代替wait(empty)和wait(mutex)
用Ssignal(mutex,full)代替signal(mutex)和signal(full)
用SWait(full,mutex)代替wait(full)和wait(mutex)
用Ssignal(mutex,empty)代替signal(mutex)和signal(empty),则描述如下

**相比于记录型信号量,他把两个wait步骤写在一起,能够避免在执行过程中的一些死锁问题。

	Var mutex,empty,full:semaphore:=1,n,0;
		buffer:array[0,...,n-1] of item;
		in out:integer:=0,0;
	begin
		parbegin
			producer:begin
						repeat
						...
						produce an item in nextp;
						...
						Swait(empty,mutex);
						...
						buffer(in):=nextp;
						in:=(in+1)mod n;
						Ssignal(mutex,full);
						until false;
					 end
			consumer:begin
						Swait(full,mutex);
						nextc:=buffer(out);
						out:=(out+1) mod n;
						Ssignal(mutex,empty);
						consumer the item in nextc;
						until false;
					 end
		parend
	end

解决方案三:利用管程解决

管程的语法描述:

type monitor_name = MONITOR;
<共享变量说明>:
define<能被其他模块引用的过程名列表>
use<要调用的本模块外引用的过程列表>
procedure<过程名>(<形式参数>):
begin

end
function<函数名>(<形式参数>):值类型:
begin

end

begin
<管程的局部数据初始化语句序列>:
end

则解决时首先为它们建立一个管程,并命名为ProducerConsumer,简称PC。其中包括两个过程:
put(item)过程。count表示缓冲池中已有资源的数目,当count大于n时,表示缓冲池已满。
get(item)过程。当count<=0 时,表示缓冲池已无可取资源。
PC管程描述如下:

type ProducerConsumer = monitor
Var in,out,count:integer;
buffer:array[0,…,n-1] of item;
notfull,notempty:condition;
procedure entry put(item)
begin
if count >= n then notfull.wait;//如果已有资源大于缓冲区数目则等待
buffer(in):=nextp;//将缓冲变量的值放入到缓冲区
in:=(in+1)mod n;
count:=count+1;
if notempty.queue then notempty.signal;//唤醒等待队列的进程
end
procedure entry get(item)
begin
if count <= 0 then notempty.wait;//如果已有资源小于0则等待
nextc:=buffer(out);//将缓冲区的资源送入nextc缓冲变量
out:=(out+1)mod n;
count:=count-1;
if nofull.queue then notfull.signal:
end
begin in:=out:=0;
cout:=0;
end

因此生产者和消费者可以描述为:
producer:begin
		repeat
			produce an item in nextp;
			PC.put(item);
		until false;
		end
consumer:begin
		repeat
			PC.get(item);
			consume the item in nextc;
		until false;
		end

2 哲学家进餐的问题:
问题描述:五个哲学家共用一张圆桌,分别坐在周围五张椅子上,在圆桌上有五个碗和五只筷子,他们的生活方式是交替的思考和进餐。一个思考,饥饿的时候便试图获取
其左右离他最近的两只筷子,只有拿到两只筷子才能进餐,进餐完毕,放下筷子,继续思考问题。

1 利用记录型洗好解决
问题思考:筷子是临界资源,则令一只筷子成为一个信号量,构成信号量组,初始化为1,则第i加哲学家的活动可描述为:

Var chopsticks:array[0,…,4] of semaphore;
repeat
wait(chopsticks[i]);
wait(chopsticks[(i+1)mod 5]);

eat;

signal(chopsticks[i]);
signal(chopsticks[(i+1) mod 5]);

think;
until false;

但该描述容易造成死锁,因此提供以下几种解决方法:
(1)至多只允许有四位哲学家同时去拿左边的筷子。
(2)仅当哲学家左右两只筷子均可用时,才允许其拿起筷子。
(3)规定奇数号哲学家先拿他左边的筷子,而偶数则相反。

2 利用AND型信号解决

AND同步
Var chopsticks array of semaphore:=(1,1,1,1,1);
processi
repeat
think;
Sswait(chopsticks[(i+1)mod 5],chopsticks[i]);
eat;
Ssignal(chopsticks[(i+1)mod 5],chopsticks[i]);
until false;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值