Re: Any book covers materials on implementing parallel/concurrent programming language?

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Tue, 28 Aug 2007 22:41:49 +0200

          From comp.compilers

Related articles
Any book covers materials on implementing parallel/concurrent programm climber.cui@gmail.com (2007-08-28)
Re: Any book covers materials on implementing parallel/concurrent prog mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2007-08-28)
Re: Any book covers materials on implementing parallel/concurrent prog climber.cui@gmail.com (2007-08-29)
Re: Any book covers materials on implementing parallel/concurrent prog mrd@cs.cmu.edu (Matthew Danish) (2007-08-31)
| List of all articles for this month |

From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Newsgroups: comp.compilers
Date: Tue, 28 Aug 2007 22:41:49 +0200
Organization: cbb software GmbH
References: 07-08-084
Keywords: parallel, design
Posted-Date: 28 Aug 2007 16:44:14 EDT

On Tue, 28 Aug 2007 09:09:14 -0700, climber.cui@gmail.com wrote:


> The language design is nearly finished, we extend Pascal with Box
> type and actions, each box is very similar to a monitor with the
> exception that actions defined within a box runs autonomously, a piece
> of code from the Dining-philosopher program is attached below.


From your example, it looks quite similar to Ada's tasks and protected
objects.


> Any suggestions?


You might find useful books on concurrency in Ada.


> type
> box Philosopher (Index: integer);
> const
> LIMIT=5 : integer;
> var
> times_eaten: integer;
>
> proc Say(msg:String)
> begin
> writeln("Philosopher", Index, msg);
> end
>
> action start
> begin
> for times_eaten=1 to LIMIT loop
> butler.Enter_sitdown;
>
> F(Index + 1).Pick_Up;
> Say ("has right fork and is waiting for left");
> F(Index).Pick_Up;
> Say ("got the left fork and is eating");


This does not solve the philosophers problem, because it potentially
deadlocks, when each task (philosopher) gets preempted after seizing
its first mutex (right fork).


The following is a possible solution in Ada based on ordering of
protected objects used to implement a fork. Philosophers eat and think
random time for which another protected object is used to allow
concurrent calls to a random generator.


------------ begin Philosophers.ads -----------
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;


procedure Philosophers is
      type Chair_Number is mod 5;


      protected type Fork is
            entry Pick_Up;
            procedure Put_Down;
      private
            Free : Boolean := True;
      end Fork;


      task type Philosopher is
            entry Get_Chair (Chair : Chair_Number; Name : String);
      end Philosopher;


      protected Problem is
            procedure Get_Time_To_Solve (Time : out Duration);
      private
            G : Generator;
      end Problem;


      protected body Problem is
            procedure Get_Time_To_Solve (Time : out Duration) is
            begin
                  Time := Duration (Random (G) * 0.5); -- 0...0.5 seconds
            end Get_Time_To_Solve;
      end Problem;


      protected body Fork is
            entry Pick_Up when Free is
            begin
                  Free := False;
            end Pick_Up;
            procedure Put_Down is
            begin
                  Free := True;
            end Put_Down;
      end Fork;


      Forks : array (Chair_Number) of Fork;


      task body Philosopher is
            Busy_Time : Duration;
            First : Chair_Number;
            Second : Chair_Number;
            Name : Unbounded_String;
      begin
            accept Get_Chair (Chair : Chair_Number; Name : String) do
                  First := Chair_Number'Max (Chair, Chair + 1);
                  Second := Chair_Number'Min (Chair, Chair + 1);
                  Philosopher.Name := To_Unbounded_String (Name);
            end;
            for Life_Cycle in 1..50 loop
                  Problem.Get_Time_To_Solve (Busy_Time);
                        Put_Line (To_String (Name) & " is thinking");
                  delay Busy_Time;
                        Put_Line (To_String (Name) & " is hungry");
                  Forks (First).Pick_Up;
                  Forks (Second).Pick_Up;
                        Put_Line (To_String (Name) & " is eating");
                  Problem.Get_Time_To_Solve (Busy_Time);
                  delay Busy_Time;
                  Forks (First).Put_Down;
                  Forks (Second).Put_Down;
            end loop;
            Put_Line (To_String (Name) & " is leaving");
      end;


      Audience : array (Chair_Number) of Philosopher;
begin
      Audience (0).Get_Chair (0, "Aristotle");
      Audience (1).Get_Chair (1, "Spinoza");
      Audience (2).Get_Chair (2, "Kant");
      Audience (3).Get_Chair (3, "Marx");
      Audience (4).Get_Chair (4, "Russel");
end Philosophers;
------------ end Philosophers.adb -----------


--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.