hoare管程实现电梯调度

Type diskhead = MONITOR
VAR bus:boolean;
    headpos:0..199;
	direction:(up,down);
	cylinder:Array[0..199] of condition;
	count:Array[0..199] of integer;
Define require, release;

Procedure require(dest:0..199)
Being
    If busy Then
	Begin
	   count[dest]:=count[dest] + 1;
	   wait(cylinder[dest]);
	End
	busy:=true;
	If dest < headpos Then
		direction:=down;
	Else
	If dest > headpos Then
		direction:=up;
	headpos:=dest
End;

Procedure upscan;
Var I:0..200;
    flag:(false,true);
Begin
    I:=headpos;
	flag:=false;
	While (I <= 199) and (count[I] = 0) Do
		I:=I + 1;
	If I<=199 Then
	Begin
		count[I]:=count[I] - 1;
		signal(cylinder[I]);
		flag:=true;
	End
End;

Procedure downscan;
Var I:-1..199
    flag:(false, true);
Begin
    I:=headpos;
	While (I >= 0) and (count[I] = 0) Do
		I:=I - 1;
	if I >= 0 Then
	Begin
		count[I]:=count[I] - 1;
		signal(cylinder[I]);
		flag:=true;
	End
End;

Procedure release;
Begin
	busy:=false;
	If direction = up Then
	Begin
		upscan;
		If (!flag) Then
			downscan;
	End
	Else
	Begin
		downscan;
		If (!flag) Then
			upscan;
	End
End;

Procedure initialize;
Var I:0..199;
Begin
	busy:=false;
	headpos:=0;
	direction:=up;
	For I:=0 To 199 Do
		count[I]:=0
End

Begin initialize End;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值