📄 os.pas
字号:
unit os;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, AHMControls, AHMTFillBackground, AHMTBackground,
AHMTOfficeButton, ProCtrls, ComCtrls, AHMTLabel, Grids, Buttons;
const m=50;//进程优先级个数
type
pointer1=^stu1;
stu1=record
sign:integer;
state:integer;
pri:integer;
lifetime:integer;
next:pointer1;
end;
//pointer1为进程结点接记录类型,包括sign--进程标志符
//state--状态值,包括0跟1. pri--优先级. lifetime--生命周期
//next--指向下一个结点的指针
pointer2=^stu2;
stu2=record
num:integer;
next:pointer2;
shell:pointer1;
end;
//pointer2为邻接表纵向链表结点记录类型,包括num--优先级.
//next--指向纵向链表下一个结点指针. shell--指向横向第一个
//进程结点.
TForm1 = class(TForm)
Edit1: TEdit;
Timer1: TTimer;
AHMBackground1: TAHMBackground;
Label10: TLabel;
Label11: TLabel;
Label14: TLabel;
AHMFillBackground1: TAHMFillBackground;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ProBorder1: TProBorder;
AHMOfficeButton1: TAHMOfficeButton;
AHMOfficeButton2: TAHMOfficeButton;
AHMOfficeButton3: TAHMOfficeButton;
ProBorder2: TProBorder;
StatusBar1: TStatusBar;
StringGrid1: TStringGrid;
AHMOfficeButton4: TAHMOfficeButton;
AHMLabel1: TAHMLabel;
AHMLabel2: TAHMLabel;
AHMLabel3: TAHMLabel;
AHMLabel4: TAHMLabel;
AHMLabel5: TAHMLabel;
AHMLabel6: TAHMLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure creat(var head:pointer2);
procedure tenor(var p:pointer1);
procedure repeal(x:integer);
procedure showtenor(p:pointer1);
procedure yunxing;
procedure Button3Click(Sender: TObject);
procedure jiedianzhuangyi(x:integer);
procedure AHMOfficeButton1Click(Sender: TObject);
procedure AHMOfficeButton2Click(Sender: TObject);
procedure AHMOfficeButton3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure show;
procedure Timer1Timer(Sender: TObject);
procedure AHMOfficeButton4Click(Sender: TObject);
procedure selection(c:tstringgrid;r,t:integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
head:pointer2;//邻接表头指针
o:pointer1;//进程结点
sign1:array[1..50]of integer;
//用来记录进程标志符是否被占用的数组,0为未被占用,其他则被占用
i,s:integer;
implementation
{$R *.DFM}
procedure tform1.creat(var head:pointer2);//创建邻接表的纵向链表
var
p,q:pointer2;
n:integer;
begin
n:=0;
new(p);
head:=p;q:=p;
repeat
n:=n+1;
if n=1 then head:=p
else q^.next:=p;
q:=p;
new(p);
p^.num:=n;
p^.shell:=nil;
until n=m;
dispose(p);
q^.next:=nil;
end;
procedure tform1.tenor(var p:pointer1); //产生新结点并插入到邻接表中
var i,j,k:integer;
q:pointer2;p1,p2:pointer1;
begin
randomize;
new(p);
p^.state:=0;{就绪态}
p^.lifetime:=random(5)+1;
p^.next:=nil;
p^.pri:=random(m-1)+1;
q:=head;
for k:= 1 to p^.pri do q:=q^.next;
p2:=q^.shell;
if q^.shell=nil then q^.shell:=p
else begin
while p2<>nil do
begin
p1:=p2;
p2:=p2^.next;
end;
p1^.next:=p;
end;
i:=1;j:=1;
repeat
//分配进程存在标志
if sign1[i]=0 then begin
j:=0; sign1[i]:=i;{xuanzhong}
end;
i:=i+1;
until j=0;
p^.sign:=i-1;
end;
procedure tform1.showtenor(p:pointer1);
//显示一个进程各项属性到菜单上
begin
label1.caption:=inttostr(p^.sign);
if p^.state=0 then label2.caption:='就绪态'
else label2.caption:='执行态';
label3.caption:=inttostr(p^.pri);
label4.caption:=inttostr(p^.lifetime);
end;
procedure tform1.repeal(x:integer);
//删除一个标志符为x的进程结点
var p,p1:pointer1;
q:pointer2;
begin
q:=head;
while (q^.next<>nil) do
begin
q:=q^.next;
if q^.shell<>nil then
begin
p:=q^.shell;
if p^.sign=x then
begin
p:=p^.next;
q^.shell:=p;
sign1[x]:=0;
AHMLabel6.Caption:='进程 '+inttostr(x)+' 结束!';
end
else
while p^.next<>nil do
begin
p1:=p;
p:=p^.next;
if p^.sign=x then
begin
sign1[x]:=0;
p1^.next:=p^.next;
AHMLabel6.Caption:='进程 '+inttostr(x)+' 结束!';
end;
end;
end;
end;
end;
procedure tform1.show;
//把所有进程结点属性显示到菜单上
var p,p1:pointer1;
q:pointer2;
i:integer;
begin
for i:=2 to 25 do
begin
StringGrid1.cells[i,0]:=' ';
StringGrid1.cells[i,1]:=' ';
StringGrid1.cells[i,2]:=' ';
StringGrid1.cells[i,3]:=' ';
StringGrid1.cells[i,5]:=' ';
StringGrid1.cells[i,6]:=' ';
StringGrid1.cells[i,7]:=' ';
StringGrid1.cells[i,8]:=' ';
end;
q:=head;
while (q^.next<>nil) do
begin
q:=q^.next;
if q^.shell<>nil then
begin
p:=q^.shell;
while p<>nil do
begin
if p^.sign+1<26 then
begin
StringGrid1.cells[p^.sign+1,0]:=inttostr(p^.sign);
StringGrid1.cells[p^.sign+1,1]:=inttostr(p^.state);
StringGrid1.cells[p^.sign+1,2]:=inttostr(p^.pri);
StringGrid1.cells[p^.sign+1,3]:=inttostr(p^.lifetime);
end
else
begin
StringGrid1.cells[p^.sign+1-24,5]:=inttostr(p^.sign);
StringGrid1.cells[p^.sign+1-24,6]:=inttostr(p^.state);
StringGrid1.cells[p^.sign+1-24,7]:=inttostr(p^.pri);
StringGrid1.cells[p^.sign+1-24,8]:=inttostr(p^.lifetime);
end;
//显示结点在stringgrid
p:=p^.next;
end;
end;
end;
end;
procedure tform1.jiedianzhuangyi(x:integer);
//结点转移时先把结点删除的过程,等价于过程repeal,唯一区别是
//没有显示输出
var p,p1:pointer1;
q:pointer2;
begin
q:=head;{write(q^.num);}
while (q^.next<>nil) do
begin
q:=q^.next;
if q^.shell<>nil then
begin
p:=q^.shell;
if p^.sign=x then
begin
p:=p^.next;
q^.shell:=p;
sign1[x]:=0;
end
else
while p^.next<>nil do
begin
p1:=p;
p:=p^.next;
if p^.sign=x then
begin
sign1[x]:=0;
p1^.next:=p^.next;
end;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
//初始化,创建邻接表
var i:integer;
begin
StringGrid1.cells[0,0]:='标';
StringGrid1.cells[1,0]:='识';
StringGrid1.cells[0,1]:='状';
StringGrid1.cells[1,1]:='态';
StringGrid1.cells[0,2]:='优';
StringGrid1.cells[1,2]:='先';
StringGrid1.cells[0,3]:='周';
StringGrid1.cells[1,3]:='期';
StringGrid1.cells[0,5]:='标';
StringGrid1.cells[1,5]:='识';
StringGrid1.cells[0,6]:='状';
StringGrid1.cells[1,6]:='态';
StringGrid1.cells[0,7]:='优';
StringGrid1.cells[1,7]:='先';
StringGrid1.cells[0,8]:='周';
StringGrid1.cells[1,8]:='期';
for i:=1 to 50 do sign1[i]:=0;
creat(head);
end;
procedure insert(p:pointer1);
//插入进程结点p到邻接表中,按p的优先级等于插入位置的纵向num的原则
var i,j,k:integer;
q:pointer2;
p1,p2:pointer1;
begin
q:=head;
for k:= 1 to p^.pri do q:=q^.next;
p2:=q^.shell;
if q^.shell=nil then q^.shell:=p
else begin
while p2<>nil do
begin
p1:=p2;
p2:=p2^.next;
end;
p1^.next:=p;
end;
end;
procedure tform1.selection(c:tstringgrid;r,t:integer);
//该过程在运行时在指定网格上高亮显示
var myRect:TGridRect;
begin
myRect.Left := r;
myRect.Top := t;
myRect.Right := r;
myRect.Bottom := t;
c.Selection:=myRect;
end;
procedure tform1.yunxing;
//一个时间片的运行,对邻接表中第一个非空结点进行假运行,如果
//时间片减一后为零则删除该结点进程,否则优先级增大,同时摘下
//该进程结点并重新插入到邻接表中
var p,p1,p0:pointer1;
q,q1:pointer2;
ke,i:integer;
begin
q:=head;
ke:=0;
for i:=1 to 50 do if sign1[i]>0 then ke:=1;
if ke=0
then begin
AHMLabel6.Caption:='不存在就绪进程!';
timer1.Enabled:=false;
show;
end
else
if(q^.next<>nil) then
begin
q:=q^.next;
while q^.shell=nil do q:=q^.next;
if q^.shell<>nil then
begin
p:=q^.shell;
begin
p^.state:=1;
if p^.sign+1<stringgrid1.ColCount then selection(stringgrid1,p^.sign+1,1)
else selection(stringgrid1,p^.sign+1-24,6);
show;
p^.lifetime:=p^.lifetime-1;
if p^.lifetime=0 then repeal(p^.sign)
else
begin
p^.state:=0;
if round((p^.pri+m)/2)<=49 then
p^.pri:=round((p^.pri+m)/2)
else p^.pri:=49;
jiedianzhuangyi(p^.sign);
sign1[p^.sign]:=p^.sign;
p^.next:=nil;
insert(p);
end;
end;
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);//运行
begin
yunxing;
end;
procedure TForm1.AHMOfficeButton1Click(Sender: TObject);
//新建一个随机进程结点
begin
new(o);
tenor(o);
showtenor(o);
show;
end;
procedure TForm1.AHMOfficeButton2Click(Sender: TObject);
//删除一个结点s
begin
s:=strtoint(edit1.text);
repeal(s);
show;
end;
procedure TForm1.AHMOfficeButton3Click(Sender: TObject);
//开始运行
begin
timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
yunxing;
end;
procedure TForm1.AHMOfficeButton4Click(Sender: TObject);
//暂停运行
begin
timer1.Enabled:=false;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -