Metered concurrency

From Rosetta Code
Revision as of 14:19, 10 September 2007 by rosettacode>CzhBiz

gay annunci galesburg michael vieth vasco in tuor frigorifero rex rc200 vostro figlio e fratello like a virgin renee zellweger maysa leak syberia 2 caldereria sony mavica mvc fd92 cordless mx gocce dal cielo capo di banda i vampiri dello spazio il simbolo del sesso amori rai it infedeli goliath e la schiava ribelle sun bitches kilie minogue video your eyes maschi con maschi nudi ingrid milord free crediveneto giada isola dei famosi nuda mercedes a 150 classic prodotti gatti xerox com ar pryda powerbook 15 superdrive costa rei vacanze new keys arabesque accessorio macchina nederlandse zenders euro dance 1 shuttle accessori cicc www googlee it fun magic 1 4 fax crime de sylvestre bonnard le arbour 3 06 lavatrice indesit stampante hp 3745 nvidia fx1400 128 mb foza venite gente foto di animali delfini haiducii gabry dragostea hector y tito videoclips ryan cabrera take all away video pono gratis frigorifero combinato fonderie acciaierie michael jackson invincible canzone ciapa la galeina terratec grabster av 250 accessorio alcatel gto great teacher onizuka disco 4 acer altos opportunita di pace adobe creative suite 1 6 premium emma dumas sexo latinas abbigliamento donna elegante acropora io con te non ci sto piu dvd rw divx con hd lettori hana melonova ddr 400 mhz 512 mb testo sunrise normativa licenziamento califfo film porno barbara chiappini la historia del carro snoop dogg feat pharrell beaut quintet gomma e materie plastiche campeggio ischia ghiacciolo un altro ballo mp3 sigla spot the blood diamond notebook lock absit iniuria verbo big cyc plasma hyundai hqp421hr codec nero mpeg2 rode helge seannateen campolongu monitor cruscotto limone del garda remos ladro e gentiluomo tragico oriente noe we are free km0 bmw 120 umidita risalita terapia luce herceptin iv 1 fl 150 mg blu chat eizo monitor cg21 hp nx6110 512 centrino tom hanks violadores de la selva satyricom nonne ose i lupi attaccano in branco case di riposo private www hotel de ce mea minti shopping egno aparasphenodon musica en el renacimiento tv lcd 20 keymat flevoland sony dcrpc103e melissa vieni a cantare md4100 adsl modemrouter fotocamera 2 0 pixel cv 150f verbatim cdr 80 min 700 mb 52x anelli oro bianco www mirabilandia it hp q7971a piacenza bsx mocassini prada stampanti laser hp 3500 traduzione di the power of love di celin racchetta beach tennis king www pitneybowes com tiziano ferro basi www pesci di mare it distributore philips italia guasti clio la bella antonia sanyo fotocamere digitali zalman vf700cu docking station serie m rivoluzione russa hello lionel richie nuova honda benzina auto nuove lo zingaro www ministero istruzione padova le arene a catania l acqua e poca hard disk maxtor 160 gb racconti erotici cani acer lcd 15 slim ps2 epox amd 939 il segno di robin hood se qualcuno deve morire banni s45 pentax toto cutugno canzioni calendario azzurre cappa 80 canon powershot pro 1 resident evil main title giacche diesel gio di tonno imetec dolcevita monitor lcd sharp lacie 500gb videos de hombres desnudos gigi d alessio testi auto noleggio orbetello pastificio la ginestra kao the kangaroo offerta montagna asus a7n8x x latte ei suoi derivati grupo exterminador com chicco de matteo mahshar music felicity huffman red castle delfines nervatura vendita stampante laser blaupunkt casablanca mp54 you can leave tour hat on guanti pesistica concorso regione salerno rs mmc 512 dv nokia 6680 scarica suonerie senza costo jamelia davis aspidogaster gruppi islamici angel killer iii corte isolani legno telecel bolivia gabriel garko fazio wines fot gay gratis indirizzi parrocchie san (fiume) mutuo agevolato

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 #"