- •Курсовая работа на тему:
- •К. Т. Н., доцент
- •Оглавление
- •Введение
- •1.Теоретические сведения
- •1.1 Методы синхронизации
- •1.2 Синхронизация потоков, классические задачи
- •2. Постановка задачи.
- •Проблемы
- •Решение задачи. Иерархия ресурсов.
- •3. ИспользованныеApi-функции
- •4. Модульная схема
- •Использованная литература
- •Приложение Основной модуль Filosof.Pas
- •Interface
- •Implementation
- •UnitUnit1
- •Interface
- •Implementation
- •UnitUnit2
- •Interface
- •Implementation
Implementation
{$R *.dfm}
uses Unit2;
//------------------------------------------------------------------------------
procedure ShowState(State:Byte;FilosofIndex:Byte);
var Img:TImage;
scCenter,imgCenter,Vektor,stateImgCenter:TPointF;
begin
if State = PrevStateOfFilosof[FilosofIndex] then Exit;
Img:=TImage.Create(Main);
Img.Parent:=Main;
Img.Stretch:=true;
if ImgOfState[FilosofIndex]<> nil then ImgOfState[FilosofIndex].Free;
ImgOfState[FilosofIndex]:=Img;
scCenter.x:=Main.Width div 2;
scCenter.y:=Main.Height div 2;
imgCenter.x:=Images[FilosofIndex].Left+Images[FilosofIndex].Width div 2;
imgCenter.y:=Images[FilosofIndex].Top+Images[FilosofIndex].Height div 2;
Vektor.x:=scCenter.x-imgCenter.x;
Vektor.y:=scCenter.y-imgCenter.y;
case State of
0: // Думает
begin
Img.Left:=imgCenter.x;
Img.Top:=imgCenter.y-140;
If FilosofIndex = 3 then Img.Top:=Img.Top+30;
Img.Left:=Img.Left-Img.Width div 2;
Img.Top:=Img.Top-Img.Height div 2;
Img.Picture.LoadFromFile('./img/380.jpg');
end;
1: // Ест
begin
Img.Picture.LoadFromFile('./img/382.jpg');
stateImgCenter.x:=imgCenter.x+Round(Vektor.x*0.4);
stateImgCenter.y:=imgCenter.y+Round(Vektor.y*0.4);
stateImgCenter.x:=stateImgCenter.x-Img.Width div 2;
stateImgCenter.y:=stateImgCenter.y-Img.Height div 2;
Img.Left:=stateImgCenter.x;
Img.Top:=stateImgCenter.y;
end;
2: // Голодает
begin
Img.Left:=imgCenter.x;
Img.Top:=imgCenter.y-140;
If FilosofIndex = 3 then Img.Top:=Img.Top+30;
Img.Left:=Img.Left-Img.Width div 2;
Img.Top:=Img.Top-Img.Height div 2;
Img.Stretch:=false;
Img.AutoSize:=true;
Img.Picture.LoadFromFile('./img/381.jpg');
end;
3: // Резерв
begin
Img.Free;
ImgOfState[FilosofIndex]:=nil;
end;
4:
begin
Img.Left:=imgCenter.x;
Img.Top:=imgCenter.y-140;
If FilosofIndex = 3 then Img.Top:=Img.Top+30;
Img.Left:=Img.Left-Img.Width div 2;
Img.Top:=Img.Top-Img.Height div 2;
Img.Stretch:=false;
Img.AutoSize:=true;
Img.Picture.LoadFromFile('./img/383.jpg');
end;
end;
PrevStateOfFilosof[FilosofIndex]:=State;
end;
//------------------------------------------------------------------------------
procedure TMain.FilosofNojCapture(Sender: TObject);
begin
InterlockedDecrement(NojCnt);
end;
procedure TMain.FilosofNojPut(Sender: TObject);
begin
InterlockedIncrement(NojCnt);
end;
procedure TMain.FilosofVilkaCapture(Sender: TObject);
begin
InterlockedDecrement(VilkaCnt);
end;
procedure TMain.FilosofVilkaPut(Sender: TObject);
begin
InterlockedIncrement(VilkaCnt);
end;
procedure TMain.FormShow(Sender: TObject);
var i:integer;
begin
VilkaHandle:=CreateSemaphore(nil,0,nmax,nil);
NojHandle:=CreateSemaphore(nil,0,nmax,nil);
for i:=1 to nFil do
begin
Filosofs[i]:=TFilosof.Create(VilkaHandle,NojHandle);
Filosofs[i].OnVilkaCapture:=FilosofVilkaCapture;
Filosofs[i].OnNojCapture:=FilosofNojCapture;
Filosofs[i].OnVilkaPut:=FilosofVilkaPut;
Filosofs[i].OnNojPut:=FilosofNojPut;
end;
Images[1]:=JvImage2;
Images[2]:=JvImage1;
Images[3]:=JvImage3;
Images[4]:=JvImage4;
Images[5]:=JvImage5;
end;
procedure TMain.Timer1Timer(Sender: TObject);
var i:integer;
begin
Form2.Left:=Main.Left+Main.Width+10;
Form2.Top:=Main.Top+10;
if not Form2.Visible then begin Form2.Show; Main.SetFocus; end;
for i:=1 to nFil do
ShowState(Filosofs[i].State,i);
Label6.Caption:=IntTOStr(VilkaCnt);
Label7.Caption:=IntTOStr(NojCnt);
end;
end.
//-------------------------------------------------------------------------------------