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;

你可能感兴趣的:(大学的nothing)