操作系统多进程设计 第3页
操作系统多进程设计 第3页
begin
FreeOnTerminate := True;
inherited Create(True);
end;
procedure TDemoProcedure.donWantSource;
begin
with Main do
begin
isDonotWantSource := not CheckBox1.checked;
if isDonotWantSource then
begin
//释放资源
edtuse.Text := '否';
Edit1.Text := '无';
edtSource.Text := intTostr(strToint(edtSource.Text) + 1);
AddinfoTolvinfo(ListIndex, '释放资源成功');
Addinfo(format('%s 试图释放资源---资源尚未锁定,释放成功', [strname]));
end
else
begin
需要完整内容的请联系QQ3249114,本文免费,转发请注明源于www.751com.cn
end;
end;
end;
procedure TDemoProcedure.donWantsourceOK;
begin
with Main do
begin
AddinfoTolvinfo(ListIndex, '释放资源');
Addinfo(format('%s 成功释放资源---释放资源后马上会自动终止本进程', [strname]));
end;
end;
procedure TDemoProcedure.EndThisRun;
begin
with Main do
begin
addinfoTolvinfo(listindex, '成功结束');
addinfo(format('%s 成功结束', [strName]));
end;
end;
procedure TDemoProcedure.Execute;
var
i: integer;
begin
i := 0;
repeat
synchronize(WantSource);
if isWantSource then break
else
sleep(RetryTime);
Inc(i);
until (i >= Times);
if i >= Times then
begin //未申请到资源退出
synchronize(self.ShowError);
self.Terminate;
end;
//进行运行态
synchronize(wantsourceOK);
//运行
sleep(waittime); //模拟
//运行完毕申请释放资源
i := 0;
repeat
synchronize(donWantSource);
if isDonotWantSource then break
else
sleep(RetryTime);
Inc(i);
until (i >= Times);
if i >= Times then
synchronize(self.ShowErrorEx);
self.Terminate;
end;
synchronize(donWantSourceOk);
synchronize(EndThisRun);
// self.Terminate;
end;
procedure TDemoProcedure.ShowError;
begin
with Main do
begin
addinfoTolvinfo(ListIndex, '超时错误并停止');
addinfo(format('%s 经过%d秒重试,仍然没有成功,超时并终止线程', [strName, RetryTime]));
end;
end;
procedure TDemoProcedure.ShowErrorEx;
begin
with Main do
begin
addinfoTolvinfo(ListIndex, '超时错误并停止');
addinfo(format('%s 经过%d秒重试,用户仍然锁定不准释放资源,为了防止死锁,强制释放并终止线程', [strName, RetryTime]));
edtuse.Text := '否';
Edit1.Text := '无';
edtSource.Text := intTostr(strToint(edtSource.Text) + 1);
end;
end;
procedure TDemoProcedure.WantSource;
begin
with Main do
begin
if edtuse.Text = '是' then
self.isWantSource := false
else
self.isWantSource := True;
if isWantSource then
begin
//申请资源
edtuse.Text := '是';
Edit1.Text := strname;
edtSource.Text := intTostr(strToint(edtSource.Text) - 1);
AddinfoTolvinfo(ListIndex, '申请资源成功');
Addinfo(format('%s 试图申请资源---资源尚未使用,申请成功', [strname]));
end
else
需要完整内容的请联系QQ3249114,本文免费,转发请注明源于www.751com.cn
end;
end;
end;
procedure TDemoProcedure.Wantsourceok;
begin
with Main do
begin
AddinfoTolvinfo(ListIndex, '使用资源状态');
Addinfo(format('%s 成功申请资源---正在使用过程中,将运行%d毫秒', [strname, waittime]));
end;
end;
end.