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

"Dmitry A. Kazakov" <>
Tue, 28 Aug 2007 22:41:49 +0200

          From comp.compilers

Related articles
Any book covers materials on implementing parallel/concurrent programm (2007-08-28)
Re: Any book covers materials on implementing parallel/concurrent prog (Dmitry A. Kazakov) (2007-08-28)
Re: Any book covers materials on implementing parallel/concurrent prog (2007-08-29)
Re: Any book covers materials on implementing parallel/concurrent prog (Matthew Danish) (2007-08-31)
| List of all articles for this month |

From: "Dmitry A. Kazakov" <>
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, 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

> 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 -----------
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;
            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);
            G : Generator;
      end Problem;

      protected body Problem is
            procedure Get_Time_To_Solve (Time : out Duration) is
                  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
                  Free := False;
            end Pick_Up;
            procedure Put_Down is
                  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;
            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);
            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");

      Audience : array (Chair_Number) of Philosopher;
      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 -----------

Dmitry A. Kazakov

Post a followup to this message

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