Ada2005_2.pdf

(117 KB) Pobierz
Temat zajęć:
Programowanie systemów czasu rzeczywistego – laboratorium
Ćwiczenie 2
Temat zajęć: pakiety, zadania, synchronizacja czasowa, mechanizm spotkań
Autor: dr inż. Sławomir Samolej
Zagadnienie 1. (Przykładowe programy)
Uruchomić przykładowe programy:
------------------------------------------------------------------------------
-- Implementacja pakietu
-- plik stack.ads:
package Stack is
procedure Push(X:Integer);
function Pop return Integer;
end Stack;
-- plik stack.adb:
package body Stack is
size: constant := 100;
A: array(1..Size) of Integer;
Top: Integer range 0..Size;
procedure Push(X: Integer) is
begin
If Top = size then return;
end if;
Top:=Top + 1;
A(Top):=X;
end Push;
function Pop return Integer is
begin
Top:= Top - 1;
return A(Top+1);
end Pop;
Begin
top:=0; -- nadanie wartości poczatkowej
end Stack;
-- plik main.adb:
with Ada.Text_IO; use Ada.Text_IO;
with ada.integer_text_io; use ada.integer_text_io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with stack; use Stack;
procedure main is
M: Integer :=12;
L: Integer :=125;
begin
Push(M);
Push(L);
Put(Pop);
Put(pop);
end Main;
--------------------------------------------------------------------------
-- Zadania – prosta synchronizacja
with Text_IO; use Text_IO;
with ada.integer_text_io; use ada.integer_text_io;
with ada.float_text_io; use ada.float_text_io;
procedure spotkanie is
task type odbiornik is
entry GET;
end Odbiornik;
task type Nadajnik;
task body Odbiornik is
begin
loop
accept GET do –- oczekiwanie na wejście
put("Odebrano komunikat");
New_Line;
end GET;
end loop;
end Odbiornik;
Odb : Odbiornik; -- uruchomienie zadania
task body Nadajnik is
c: Character;
begin
loop
Get(c);
exit when C='?';
Odb.GET; -- wołanie wejścia GET w zadaniu Odb
end loop;
end Nadajnik;
Nad : Nadajnik; -- uruchomienie zadania
begin
Put("Uruchomiono zadania");
New_Line;
end Spotkanie;
----------------------------------------------------------------------------
-- Implementacja sekcji krytycznej:
with Text_IO; use Text_IO;
with ada.integer_text_io; use ada.integer_text_io;
with ada.float_text_io; use ada.float_text_io;
with Ada.Calendar; use Ada.Calendar;
procedure main is
-- Specyfikacja
task type SEMAPHORE is
entry ACCES_TO;
entry FREE;
end SEMAPHORE;
-- Implmentacja
task body SEMAPHORE is
begin
loop
accept ACCES_TO;
accept FREE;
end loop;
end SEMAPHORE;
Drukarka : Integer;
Semafor : SEMAPHORE;
task type T(Init: Integer; Del: Integer);
task body T is
dana : Integer;
begin
loop
Dana := Init;
Semafor.ACCES_TO; -- początek sekcji krytycznej
Drukarka:=dana;
Put(Drukarka);
New_Line; -- koniec sekcji krytycznej
Semafor.FREE;
delay Duration(Del);
end loop;
end T;
Task1: T(2,1);
Task2: T(4,2);
begin
null;
end;
---------------------------------------------------------------------------
-- Bufor synchronizujący komunikacje pomiędzy producentem a konsumentem:
-- specyfikacja pakietu:
package S_MEMORY1 is
task type SHARED_MEMORY is
entry WRITE(X: in Integer);
entry READ(X: out Integer);
end SHARED_MEMORY;
end S_MEMORY1;
-- implementacja pakietu:
package body S_MEMORY1 is
task body SHARED_MEMORY is
PROTECTED_VARIABLE: Integer;
begin
loop
accept WRITE(x: in Integer) do
PROTECTED_vARIABLE:=X;
end WRITE;
accept READ(X: out Integer) do
X:=PROTECTED_VARIABLE;
end READ;
end loop;
end SHARED_mEMORY;
end S_MEMORY1;
-- przykładowe zastosowanie bufora:
with Ada.Text_IO; use Ada.Text_IO;
with ada.integer_text_io; use ada.integer_text_io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Calendar; use Calendar;
with S_MEMORY1; use S_MEMORY1;
procedure main is
task producer;
task consumer;
S_M1: SHARED_MEMORY;
task body producer is
begin
for I in 1..10 loop
S_M1.WRITE(Integer(i));
delay 0.5;
end loop;
end;
task Body Consumer is
Dana : Integer;
begin
loop
S_M1.READ(Dana);
if Dana < 10 then Put(Dana); New_Line;
end if;
delay 1.0;
end loop;
end Consumer;
begin
null;
end main;
---------------------------------------------------------------------------
-- zadanie cykliczne z eliminacją niekorzystnych składników czasowych:
with Calendar; use Calendar;
with Ada.Text_IO; use Ada.Text_IO;
with ada.integer_text_io; use ada.integer_text_io;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
procedure timer3 is
task type Periodic1;
task body Periodic1 is
Seconds: constant Duration := 0.5;
Period: constant Duration := 1.0 * Seconds;
Offset: constant Duration := 1.0 * Seconds;
Next_Time : Time := Clock + Offset;
ActTime : Time;
Sec1: Duration;
Month1: Integer;
Day1: Integer;
Year1: Integer;
begin
loop
delay until Next_Time;
ActTime := Clock;
Put("Biezacy czas:");
Split(Clock,Year1,Month1,Day1,Sec1);
Put(Year1); Put(Month1); Put(Day1); Put(Integer(Sec1));
New_Line(1);
Next_Time := Next_Time + Period;
end loop;
end Periodic1;
Task1 : Periodic1;
begin
null;
end timer3;
---------------------------------------------------------------------------
-- Producent – konsument + dzielona zmienna
-- zrealizowana z zastosowaniem obiektu chronionego:
with Ada.Text_IO; use Ada.Text_IO;
with ada.integer_text_io; use ada.integer_text_io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Calendar; use Calendar;
procedure ob_chr1 is
type moja_dana is mod 10;
Dana : Moja_Dana := 0;
procedure computes(Y: in Integer) is
begin
put("Konsumuje wartosc"); put(Y); New_Line;
Delay 0.4;
end;
procedure Produces(E: out Integer) is
begin
Dana:= Dana+1;
Delay 1.0;
Zgłoś jeśli naruszono regulamin