Advertisement
csquare

comp2310lab3synchronized

Aug 11th, 2019
1,660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.74 KB | None | 0 0
  1. -- repair this program according to the lab sheet.
  2.  
  3. with Ada.Text_IO; use Ada.Text_IO;
  4.  
  5. procedure Counter_Test_Synchronized is
  6.  
  7.    -- Lamport's bakery Algorithm
  8.    -- References :
  9.    -- https://en.wikipedia.org/wiki/Lamport%27s_bakery_algorithm
  10.    -- https://www.geeksforgeeks.org/operating-system-bakery-algorithm/
  11.    subtype Task_Index is Positive range 1 .. 4;
  12.    type Ticket_Array is array (Task_Index) of Integer;
  13.  
  14.    Choosing : array (Task_Index) of Boolean := (Task_Index'First .. Task_Index'Last => False);
  15.    Number : Ticket_Array := (Task_Index'First .. Task_Index'Last => 0);
  16.  
  17.    function Get_Max (Array_X : Ticket_Array) return Integer is
  18.       max : Integer := Integer'First;
  19.    begin
  20.       for I in Array_X'Range loop
  21.          if max < Array_X (I) then
  22.             max := Array_X (I);
  23.          end if;
  24.       end loop;
  25.  
  26.       return max;
  27.    end Get_Max;
  28.  
  29.    procedure Lock (Task_ID : Task_Index) is
  30.    begin
  31.       Choosing (Task_ID) := True;  -- indicate intent to enter critical section
  32.       Number (Task_ID) := 1 + Get_Max (Number); -- gives the highest ticket number
  33.       Choosing (Task_ID) := False; -- to indicate that the ticket number has been obtained
  34.  
  35.       for I in Task_Index loop
  36.          while Choosing (I) loop
  37.             null;  -- wait until thread I receives its ticket
  38.          end loop;
  39.          while
  40.             Number (I) /= 0 and then
  41.                   (Number (I) < Number (Task_ID) or else
  42.                        (Number (I) = Number (Task_ID) and then I < Task_ID))
  43.                 loop
  44.             -- wait until all thread with smaller ticket or with the same ticket
  45.             -- number but with higher priority (i.e. smaller task ID) finish their
  46.             -- work
  47.             null;
  48.          end loop;
  49.  
  50.       end loop;
  51.    end Lock;
  52.  
  53.    procedure Unlock (Task_ID : Task_Index) is
  54.    begin
  55.       Number (Task_ID) := 0;
  56.    end Unlock;
  57.  
  58.    -- End of bakery algorithm
  59.  
  60.    -- Shared variable
  61.    Sum : Natural := 50;
  62.    --
  63.  
  64.    task type Counter (Id : Positive; Goal : Natural);
  65.  
  66.    task body Counter is
  67.  
  68.    begin
  69.       -- start critical section
  70.       Lock (Id);
  71.       while Sum /= Goal loop
  72.          Sum := (if Sum > Goal then Sum - 1 else Sum + 1);
  73.          Put (Natural'Image (Sum));
  74.       end loop;
  75.       New_Line;
  76.       Put_Line ("Counter task" & Positive'Image (Id) & " terminates with sum being:" & Natural'Image (Sum));
  77.       Unlock (Id);
  78.       -- end critical section
  79.    end Counter;
  80.  
  81.    -- Declare tasks
  82.    Counter_1 : Counter (1, 30);
  83.    Counter_2 : Counter (2, 40);
  84.    Counter_3 : Counter (3, 60);
  85.    Counter_4 : Counter (4, 70);
  86.  
  87. begin
  88.    New_Line;
  89.    Put_Line ("Counter_Test terminates with sum being:" & Natural'Image (Sum));
  90. end Counter_Test_Synchronized;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement