实验一:生产者消费者进程(delphi代码+程序)
实验报告:点击下载
源文件: 点击下载
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons,StrUtils;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
GroupBox4: TGroupBox;
GroupBox5: TGroupBox;
Button1: TButton;
Edit1: TEdit;
Label2: TLabel;
memo1: TMemo;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Label1: TLabel;
memo2: TMemo;
Label3: TLabel;
procedure Button2Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
MyArray=array[0..14] of string;
var
Form1: TForm1;
memo1: TMemo;
memo2: TMemo;
hcdl,zsdl,jxdl:MyArray;
steps:integer=0;
function sta(dl:MyArray):string;
function searchPC(pc:string;dl:MyArray):integer;
function dlval(dl:MyArray;pos:integer):string;
procedure PC(var dl:MyArray;pos:integer);
procedure ZS(var dl:MyArray;pos:integer);
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
self.Close;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
i:integer;
begin
listbox1.Items.Clear;
listbox2.Items.Clear;
listbox3.Items.Clear;
memo1.Clear;
memo2.Clear;
edit1.Clear;
bitbtn3.Enabled:=false;
steps:=0;
for i:=0 to 14 do
begin
hcdl[i]:='';
zsdl[i]:='';
jxdl[i]:='';
listbox1.Items.Add('');
listbox2.Items.Add('');
listbox3.Items.Add('');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
num,rnd,i:integer;
begin
if edit1.text='' then
begin
exit;
end;
num:=strtoint(edit1.text);
if (num>15) or (num<=0) then
begin
showMessage ('请输入1-15的数字!');
edit1.Clear;
edit1.SetFocus;
end
else
begin
for i:=0 to num-1 do
begin
Randomize;
rnd:=random(8)+1;
if (rnd>=1)and (rnd<=4) then
jxdl[i]:='P'+inttostr(rnd)
else
jxdl[i]:='C'+inttostr(rnd-4);
end;
listbox3.Items.Clear;
for i:=low(jxdl) to num-1 do
begin
listbox3.Items.Add(jxdl[i]);
end;
steps:=steps+1;
memo2.Lines.Add(inttostr(steps)+':随机生成了'+inttostr(num)+'个进程!');
for i:=num to high(jxdl) do
begin
listbox3.Items.Add('');
end;
end;
bitbtn3.Enabled:=true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
self.Close;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
self.FormActivate(Sender);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
var
zsdlsta,hcdlsta,jxdlsta:string;
jxtop:string;
begin
zsdlsta:=sta(zsdl);//判断阻塞队列状态,空与非空
hcdlsta:=sta(hcdl);//判断缓冲池状态
jxdlsta:=sta(jxdl);//就绪队列状态
jxtop:=dlval(jxdl,0);//判断就绪队列第一个进程的类型
if zsdlsta<>'empty' then
begin //阻塞不为空 ,就从阻塞队列拿
{steps:=steps+1;
memo2.Lines.Add(inttostr(steps)+':阻塞队列不为空');
}
if hcdlsta='empty' then //缓冲为空,需要生产者
begin
// showmessage('在