Metered concurrency

From Rosetta Code
Revision as of 08:33, 10 September 2007 by rosettacode>CzhBiz

www galiani com basi musicali celentano beverley mitchell brianeno bianca beauchamp yugihoo benny benasi buffy tyler video www fmcorp com zoccoli pescura brad pitt calendario yu yu binetto www sassari ircq it blessed union of souls www carpisa it www jennifer lopes www lucignolo it www pistoia eventi paesani www aladino it bohr barboncino your won main men victor lazlo www unicz bustarelle a noi piace freddo...! www bgonline it www raaga com baixar calular bennasy yonca www ltu de bella senz anima di cocciante batterie moto www 11settembre it bubblin blu www ryanair benny y bose banda beni broken video xerox www mugello bonalume z reticoli meganoidi yo voy f daddy yankee zelda awakening bugo athena e le sette sorelle wwo vienio pele dj 600volt xr 4750rds zeta cam blu bilancio societario 2004 britney spears nuda free www regione puglia it www actarus goldrake com ww suca it bebu silvetti banbabardo biagio antonacci alessandra zucchero ho scelto te www fernandodenoronha br www bossfilm it www siracusacalcio it www sfdk cl www lahora de tu muerte com ba com www la guerra it yo te voy a amar zxz www usl5 it balilla band blue brosher bugiarda tu www depelicula com www figcmarche it b c i www novalux it benni bennasi babilin bresciaonline barcellona di notte yari bruno lembo zocc al di là della legge www ubbi com br best sound brutta citta www zuker it agente 3 s 3 passaporto per l'inferno baby beesh bitter little simphony beautyful boys com zadok www emmelunga it bin tere blue che mi dici black ayed peas www acqualand it buddismo a milano baby kakes zvonko www sanjua yeah da scaricare in mp3 gratis amore in quattro dimensioni baseball gioco blue video a chi mi dice bandiara nera baldini ya mustafa blackeyed peas www altavista com mx bandi di gara potature alberi assalto dallo spazio www omega it bandida hector y tito www caraibi com bennasi brothers bachata video www napolisat com busted air hostess testo ya ghayeb any time any play www lindvall wecam ragazzi boffo rocco www rfef es www salue de bestie di tozzi xsat benny banasi satisfaction bologna accompagnatrici boys gay porn black sabbat bitch brooks bennasi bros feat dhany hit my heart bilal midi baby h mere naseeb bojate www lagos cd biblioteca nazionale di roma anno domini bloodpatche british tv beauty gets fired for 3 big bad love www ronaldo it big broders www vivicorato it bici sport firenze

Task
Metered concurrency
You are encouraged to solve this task according to the task description, using any language you may know.

The goal of this task is to create a counting semaphore used to control the execution of a set of concurrent units. This task intends to demonstrate coordination of active concurrent units through the use of a passive concurrent unit. The operations for a counting semaphore are acquire, release, and count. Each active concurrent unit should attempt to acquire the counting semaphore before executing its assigned duties. In this case the active concurrent unit should report that it has acquired the semaphore. It should sleep for 2 seconds and then release the semaphore.

Ada

Compiler: GNAT GPL 2006

The interface for the counting semaphore is defined in an Ada package specification:

package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;

The Acquire entry has a condition associated with it. A task can only execute the Acquire entry when Lock_Count is less than Max. This is the key to making this structure behave as a counting semaphore. This condition, and all the other aspects of Counting_Semaphore are contained in the package body.

package body Semaphores is

   ------------------------
   -- Counting_Semaphore --
   ------------------------ 

   protected body Counting_Semaphore is

      -------------
      -- Acquire --
      -------------

      entry Acquire when Lock_Count < Max is
      begin
         Lock_Count := Lock_Count   1;
      end Acquire;

      -----------
      -- Count --
      -----------

      function Count return Natural is
      begin
         return Lock_Count;
      end Count;

      -------------
      -- Release --
      -------------

      procedure Release is
      begin
         if Lock_Count > 0 then
            Lock_Count := Lock_Count - 1;
         end if;
      end Release;

   end Counting_Semaphore;

end Semaphores;

We now need a set of tasks to properly call an instance of Counting_Semaphore.

with Semaphores;
with Ada.Text_Io; use Ada.Text_Io;

procedure Semaphores_Main is
   -- Create an instance of a Counting_Semaphore with Max set to 3
   Lock : Semaphores.Counting_Semaphore(3);

   -- Define a task type to interact with the Lock object declared above
   task type Worker is
      entry Start (Sleep : in Duration; Id : in Positive);
   end Worker;

   task body Worker is
      Sleep_Time : Duration;
      My_Id : Positive;
   begin
      accept Start(Sleep : in Duration; Id : in Positive) do
         My_Id := Id;
         Sleep_Time := Sleep;
      end Start;
      --Acquire the lock. The task wil

2000 l suspend until the Acquire call completes

      Lock.Acquire;
      Put_Line("Task #"