comp.lang.ada
 help / color / mirror / Atom feed
* Simp,e example for 2 tasks
@ 2014-09-21 13:31 Stribor40
  2014-09-21 15:59 ` Dennis Lee Bieber
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Stribor40 @ 2014-09-21 13:31 UTC (permalink / raw)


Would anyone be able to post simple example of ada program showing 2 tasks taking to each other please.  For example one task sends message to another saying "hi" and second taks replying "hi back". 
I am having troubles finding example of simple program like this online


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 13:31 Simp,e example for 2 tasks Stribor40
@ 2014-09-21 15:59 ` Dennis Lee Bieber
  2014-09-21 16:03   ` Stribor40
  2014-09-21 16:09 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Dennis Lee Bieber @ 2014-09-21 15:59 UTC (permalink / raw)


On Sun, 21 Sep 2014 06:31:28 -0700 (PDT), Stribor40 <ikamzic@gmail.com>
declaimed the following:

>Would anyone be able to post simple example of ada program showing 2 tasks taking to each other please.  For example one task sends message to another saying "hi" and second taks replying "hi back". 
>I am having troubles finding example of simple program like this online

	Well... The problem domain is slightly under-specified.

	Ada doesn't "send message" in the way you may be thinking. It's more a
"pass a note during a handshake" (Rendezvous).

	The problem with your description is that of sequencing. A straight
linear interpretation is:

Task A										Task B
begin										begin
rendezvous with Task B					wait for rendezvous from any task
												end rendezvous
wait for rendezvous from any task			rendezvous with Task A
	end rendezvous
end											end

	There is nothing being passed there -- the rendezvous itself is the
"message". There is also no work being done inside the rendezvous. AND the
sequence is pretty much lock-step: B does nothing until A connects, then A
does nothing until B decides to connect to it.

	An alternative would be using an IN and OUT parameter on the rendezvous

begin										begin
rendezvous with B("Hello", reply)			wait for rendezvous(IN msg,
																OUT reply)
												reply = "Hello Back"
												end rendezvous
end											end

	Here there is only one rendezvous... one "handshake"; A does nothing
during the rendezvous until B ends the rendezvous after doing whatever it
wants (setting reply).

											
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 15:59 ` Dennis Lee Bieber
@ 2014-09-21 16:03   ` Stribor40
  2014-09-21 22:26     ` Dennis Lee Bieber
  0 siblings, 1 reply; 12+ messages in thread
From: Stribor40 @ 2014-09-21 16:03 UTC (permalink / raw)



> 
> 
> 
> >Would anyone be able to post simple example of ada program showing 2 tasks taking to each other please.  For example one task sends message to another saying "hi" and second tasks replying "hi back". 
> 
> >I am having troubles finding example of simple program like this online
> 
> 
> 
> 	Well... The problem domain is slightly under-specified.
> 
> 
> 
> 	Ada doesn't "send message" in the way you may be thinking. It's more a
> 
> "pass a note during a handshake" (Rendezvous).
> 
> 
> 
> 	The problem with your description is that of sequencing. A straight
> 
> linear interpretation is:
> 
> 
> 
> Task A										Task B
> 
> begin										begin
> 
> rendezvous with Task B					wait for rendezvous from any task
> 
> 												end rendezvous
> 
> wait for rendezvous from any task			rendezvous with Task A
> 
> 	end rendezvous
> 
> end											end
> 
> 
> 
> 	There is nothing being passed there -- the rendezvous itself is the
> 
> "message". There is also no work being done inside the rendezvous. AND the
> 
> sequence is pretty much lock-step: B does nothing until A connects, then A
> 
> does nothing until B decides to connect to it.
> 
> 
> 
> 	An alternative would be using an IN and OUT parameter on the rendezvous
> 
> 
> 
> begin										begin
> 
> rendezvous with B("Hello", reply)			wait for rendezvous(IN msg,
> 
> 																OUT reply)
> 
> 												reply = "Hello Back"
> 
> 												end rendezvous
> 
> end											end
> 
> 
> 
> 	Here there is only one rendezvous... one "handshake"; A does nothing
> 
> during the rendezvous until B ends the rendezvous after doing whatever it
> 
> wants (setting reply).
> 
> 
> 
> 											
> 
> -- 
> 


can I be greedy and ask you can make actuall code that will compile so i can play with it please.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 13:31 Simp,e example for 2 tasks Stribor40
  2014-09-21 15:59 ` Dennis Lee Bieber
@ 2014-09-21 16:09 ` Dmitry A. Kazakov
  2014-09-21 16:28 ` Stribor40
  2014-09-21 17:00 ` Brad Moore
  3 siblings, 0 replies; 12+ messages in thread
From: Dmitry A. Kazakov @ 2014-09-21 16:09 UTC (permalink / raw)


On Sun, 21 Sep 2014 06:31:28 -0700 (PDT), Stribor40 wrote:

> Would anyone be able to post simple example of ada program showing 2 tasks
> taking to each other please.  For example one task sends message to
> another saying "hi" and second taks replying "hi back".
 
> I am having troubles finding example of simple program like this online

Because there are problems with that:

1. It is bad design, tight coupling. There should never be so that two
object knew each other. Only one task may know another, e.g. client knows
server, server does not know any clients.

2. Sending messages is not the method of task communication. Tasks have
entry points which are alike to procedures not raw data (messages).

3. Messages as marshaled parameters of some asynchronous requests are not
implemented using tasks. There are protected objects which serve this
purpose in Ada.

4. It is not clear what sort of communication you want. Your description
misses many crucial design details required. E.g. are parameters (messages)
marshaled, is exchange synchronous, how knows whom etc.

Anyway, here is an example of text exchange between two tasks A and B:

with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Text_IO;  use Ada.Text_IO; 

procedure Talk is
   task A is
      entry Ask (Message : in out Unbounded_String);
   end A;

   task B is
   end B;
   
   task body A is
   begin
      loop
         select
            accept Ask (Message : in out Unbounded_String) do
               Message := To_Unbounded_String ("Hi back");
            end Ask;
         or terminate;
         end select;
      end loop;
   end A;
   
   task body B is
      Message : Unbounded_String;
   begin
      Message := To_Unbounded_String ("Hi");
      Put_Line ("Send " & To_String (Message));
      A.Ask (Message);
      Put_Line ("Received " & To_String (Message));
   end B;

begin
   null;
end Talk;

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 13:31 Simp,e example for 2 tasks Stribor40
  2014-09-21 15:59 ` Dennis Lee Bieber
  2014-09-21 16:09 ` Dmitry A. Kazakov
@ 2014-09-21 16:28 ` Stribor40
  2014-09-21 17:00 ` Brad Moore
  3 siblings, 0 replies; 12+ messages in thread
From: Stribor40 @ 2014-09-21 16:28 UTC (permalink / raw)


i just wanted to see how it works..This is not homework or work just curious how the tasks talk to each other regardless on knowing each other


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 13:31 Simp,e example for 2 tasks Stribor40
                   ` (2 preceding siblings ...)
  2014-09-21 16:28 ` Stribor40
@ 2014-09-21 17:00 ` Brad Moore
  2014-09-21 20:33   ` Jeffrey Carter
  3 siblings, 1 reply; 12+ messages in thread
From: Brad Moore @ 2014-09-21 17:00 UTC (permalink / raw)


On 2014-09-21 7:31 AM, Stribor40 wrote:
> Would anyone be able to post simple example of ada program showing 2 tasks taking to each other please.  For example one task sends message to another saying "hi" and second taks replying "hi back".
> I am having troubles finding example of simple program like this online
>

As others have mentioned, the problem needs to be better specified.
My assumptions are;
    1. No content to the message, as none is needed, so no parameters
       are needed for the entry calls
    2. The sender and listener tasks are tightly coupled and know of each
       other, so can send message to each other directly.
    3. The listener needs to send back a separate message to the sender.
       In other words, the response message is not sent as part of the
       rendezvous, and is sent separately. Dmitry's example handles the
       other case where only one rendezvous is needed.

procedure Task_Example
is
    task Listener is
       entry Send_Message;
    end Listener;

    task Sender is
       entry Give_Reply;
    end Sender;

    task body Listener is
    begin
       accept Send_Message;
       Put_Line ("Sender says Hi");
       Sender.Give_Reply;
    end Listener;

    task body Sender is
    begin
       Listener.Send_Message;
       accept Give_Reply;
       Put_Line ("Listener says Hi Back!");
    end Sender;

begin
       null;
end Task_Example;

The task communication here is via task rendezvous. For a more 
complicated example, I might have chosen to use a protected object
to handle the communication instead. I generally prefer to use
protected objects for task communication, over rendezvous.

procedure Task_Example
is
       protected Manager is
          procedure Send_Message;
          entry Wait_For_Message;
          procedure Send_Response;
          entry Wait_For_Response;
       private
          Received_Message : Boolean := False;
          Received_Response : Boolean := False;
       end Manager;

       protected body Manager is
          procedure Send_Message is
          begin
             Received_Message := True;
          end Send_Message;

          entry Wait_For_Message when Received_Message is
          begin
             null;
          end Wait_For_Message;

          procedure Send_Response is
          begin
             Received_Response := True;
          end Send_Response;

          entry Wait_For_Response when Received_Response is
          begin
             null;
          end Wait_For_Response;
       end Manager;

       task Listener;
       task Sender;

       task body Listener is
       begin
          Manager.Wait_For_Message;
          Put_Line ("Sender says Hi");
          Manager.Send_Response;
       end Listener;

       task body Sender is
       begin
          Manager.Send_Message;
          Manager.Wait_For_Response;
          Put_Line ("Listener says Hi Back!");
       end Sender;
begin
    null;
end Task_Example;

Brad


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 17:00 ` Brad Moore
@ 2014-09-21 20:33   ` Jeffrey Carter
  2014-09-22  7:18     ` Niklas Holsti
  0 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2014-09-21 20:33 UTC (permalink / raw)


On 2014-09-21 7:31 AM, Stribor40 wrote:
> Would anyone be able to post simple example of ada program showing 2 tasks
> taking to each other please.  For example one task sends message to another
> saying "hi" and second taks replying "hi back".

As others have pointed out, Ada tasking is based on synchronous communication,
not the message queues found in other languages. This why Ada had to introduce
the partition concept to achieve distribution, while Erlang processes distribute
quite nicely by themselves, as the ping-pong example shows.

It follows that to have message passing in Ada one must create the mechanism
that stores a message from one task and allows another task to retrieve it. In
Ada 12 one can use the synchronized-queue containers for that.

with Ada.Containers.Synchronized_Queue_Interfaces;
with Ada.Containers.Bounded_Synchronized_Queues;
with Ada.Strings.Unbounded;

procedure Task_Example is
   package String_Queue_IF is new Ada.Containers.Synchronized_Queue_Interfaces
      (Element_Type => Ada.Strings.Unbounded.Unbounded_String);
   package String_Queues is new Ada.Containers.Bounded_Synchronized_Queues
      (Queue_Interfaces => String_Queue_IF, Default_Capacity => 1);

   A_To_B : String_Queues.Queue;
   B_To_A : String_Queues.Queue;

   task A;
   task B;

   task body A is
      Message : Ada.Strings.Unbounded.Unbounded_String;
   begin -- A
      A_To_B.Enqueue (New_Item => Ada.Strings.Unbounded.To_String ("Hi") );
      B_To_A.Dequeue (Element => Message);
   end A;

   task body B is
      Message : Ada.Strings.Unbounded.Unbounded_String;
   begin -- B
      A_To_B.Dequeue (Element => Message);
      B_To_A.Enqueue (New_Item => Ada.Strings.Unbounded.To_String ("Hi back") );
   end B;
begin -- Task_Example
   null;
end Task_Example;

-- 
Jeff Carter
"How'd you like to hide the egg and gurgitate
a few saucers of mocha java?"
Never Give a Sucker an Even Break
101


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 16:03   ` Stribor40
@ 2014-09-21 22:26     ` Dennis Lee Bieber
  0 siblings, 0 replies; 12+ messages in thread
From: Dennis Lee Bieber @ 2014-09-21 22:26 UTC (permalink / raw)


On Sun, 21 Sep 2014 09:03:39 -0700 (PDT), Stribor40 <ikamzic@gmail.com>
declaimed the following:

>
>
>can I be greedy and ask you can make actuall code that will compile so i can play with it please.

	You can be greedy -- nothing I can do about that.

	You can ask... But given that the described problem is so basic to the
concepts of "communicating sequential processes", and "interprocess
communication", it comes across as some sort of homework assignment... And
homework assignments will not, normally, be answered with functional code.

	Read some Ada text books in the section discussing "rendezvous" (that
IS the language used for Ada's IPC system -- and also for "protected
objects" which I did not reference in my pseudo-code).

	When you can provide a code sample that is not working as expected,
then you may ask for assistance with the code -- but for basic concepts you
must do the studying...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-21 20:33   ` Jeffrey Carter
@ 2014-09-22  7:18     ` Niklas Holsti
  2014-09-22 17:48       ` Jeffrey Carter
  0 siblings, 1 reply; 12+ messages in thread
From: Niklas Holsti @ 2014-09-22  7:18 UTC (permalink / raw)


On 14-09-21 23:33 , Jeffrey Carter wrote:
> On 2014-09-21 7:31 AM, Stribor40 wrote:
>> Would anyone be able to post simple example of ada program showing 2 tasks
>> taking to each other please.  For example one task sends message to another
>> saying "hi" and second taks replying "hi back".
> 
> As others have pointed out, Ada tasking is based on synchronous communication,
> not the message queues found in other languages. This why Ada had to introduce
> the partition concept to achieve distribution, while Erlang processes distribute
> quite nicely by themselves, as the ping-pong example shows.

Hmm... I don't quite agree with that. It seems to me that rendez-vous
interaction between tasks could be implemented in a distributed system,
as long as the entry parameters can be passed by value or by copy-in
copy-out. It is the global shared variables and reference parameters
that would make a distributed approach difficult for Ada programs using
such constructs. I don't know Erlang well enough to understand if it has
some solution for that, or if it simply forbids such things.

Of course this does not alter the fact that rendez-vous is synchronous,
and that a queue has to be implemented if asynchronous communication is
needed.

For reque statements, I believe that the RTS typically packs up the
entry parameters in a record structure, like a message, which the RTS
can manipulate more easily than it could manage parameters pushed on the
stack, as for a normal call.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-22  7:18     ` Niklas Holsti
@ 2014-09-22 17:48       ` Jeffrey Carter
  2014-09-22 18:16         ` Niklas Holsti
  0 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2014-09-22 17:48 UTC (permalink / raw)


On 09/22/2014 12:18 AM, Niklas Holsti wrote:
> 
> Hmm... I don't quite agree with that. It seems to me that rendez-vous
> interaction between tasks could be implemented in a distributed system,
> as long as the entry parameters can be passed by value or by copy-in
> copy-out. It is the global shared variables and reference parameters
> that would make a distributed approach difficult for Ada programs using
> such constructs. I don't know Erlang well enough to understand if it has
> some solution for that, or if it simply forbids such things.

Well, of course one can implement rendezvous between tasks in different
partitions in a distributed system, since that's possible through the DSA. It's
a lot of additional work, however, compared to implementing distributed
messaging. You're right about the shared-memory aspect of tasking being another
factor that complicates things.

-- 
Jeff Carter
"Blessed are they who convert their neighbors'
oxen, for they shall inhibit their girth."
Monty Python's Life of Brian
83


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-22 17:48       ` Jeffrey Carter
@ 2014-09-22 18:16         ` Niklas Holsti
  2014-09-22 19:27           ` Dmitry A. Kazakov
  0 siblings, 1 reply; 12+ messages in thread
From: Niklas Holsti @ 2014-09-22 18:16 UTC (permalink / raw)


On 14-09-22 20:48 , Jeffrey Carter wrote:
> On 09/22/2014 12:18 AM, Niklas Holsti wrote:
>>
>> Hmm... I don't quite agree with that. It seems to me that rendez-vous
>> interaction between tasks could be implemented in a distributed system,
>> as long as the entry parameters can be passed by value or by copy-in
>> copy-out. It is the global shared variables and reference parameters
>> that would make a distributed approach difficult for Ada programs using
>> such constructs. I don't know Erlang well enough to understand if it has
>> some solution for that, or if it simply forbids such things.
> 
> Well, of course one can implement rendezvous between tasks in different
> partitions in a distributed system, since that's possible through the DSA.

I meant, rather, that an Ada RTS could have implemented distributed
rendez-vous without (and before) the DSA -- that the mere fact that
rendez-vous is synchronous does not mean that it cannot be distributed.
Of course the performance, in terms of latency, would be of a different
order than rendez-vous within tasks on the same core.

I'd bet that most distributed systems today communicate through
synchronously used, bidirectional, TCP request-reply streams, not
through asynchronous message-passing with long queues. But I include
web-based applications as distributed systems.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Simp,e example for 2 tasks
  2014-09-22 18:16         ` Niklas Holsti
@ 2014-09-22 19:27           ` Dmitry A. Kazakov
  0 siblings, 0 replies; 12+ messages in thread
From: Dmitry A. Kazakov @ 2014-09-22 19:27 UTC (permalink / raw)


On Mon, 22 Sep 2014 21:16:14 +0300, Niklas Holsti wrote:

> On 14-09-22 20:48 , Jeffrey Carter wrote:
>> On 09/22/2014 12:18 AM, Niklas Holsti wrote:
>>>
>>> Hmm... I don't quite agree with that. It seems to me that rendez-vous
>>> interaction between tasks could be implemented in a distributed system,
>>> as long as the entry parameters can be passed by value or by copy-in
>>> copy-out. It is the global shared variables and reference parameters
>>> that would make a distributed approach difficult for Ada programs using
>>> such constructs. I don't know Erlang well enough to understand if it has
>>> some solution for that, or if it simply forbids such things.
>> 
>> Well, of course one can implement rendezvous between tasks in different
>> partitions in a distributed system, since that's possible through the DSA.
> 
> I meant, rather, that an Ada RTS could have implemented distributed
> rendez-vous without (and before) the DSA -- that the mere fact that
> rendez-vous is synchronous does not mean that it cannot be distributed.

Which can and is. Another name for it is RPC.

> I'd bet that most distributed systems today communicate through
> synchronously used, bidirectional, TCP request-reply streams, not
> through asynchronous message-passing with long queues. But I include
> web-based applications as distributed systems.

Synchronous calls are much easier to use program. They become less
expensive with more cores.

Asynchronous messaging in this context make no sense at all. Provided under
messaging we understand a request-response schema. Buffering does not
improve latencies when a positive acknowledge or other response is
required.

IMO, the real use case for asynchronous communication is one directional
exchange, e.g. data and event distribution without positive acknowledging.
Specifically, publisher/subscriber schemas with avoiding priority inversion
or blocking the publisher etc. This is where asynchronous communication
really shines.

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2014-09-22 19:27 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-21 13:31 Simp,e example for 2 tasks Stribor40
2014-09-21 15:59 ` Dennis Lee Bieber
2014-09-21 16:03   ` Stribor40
2014-09-21 22:26     ` Dennis Lee Bieber
2014-09-21 16:09 ` Dmitry A. Kazakov
2014-09-21 16:28 ` Stribor40
2014-09-21 17:00 ` Brad Moore
2014-09-21 20:33   ` Jeffrey Carter
2014-09-22  7:18     ` Niklas Holsti
2014-09-22 17:48       ` Jeffrey Carter
2014-09-22 18:16         ` Niklas Holsti
2014-09-22 19:27           ` Dmitry A. Kazakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox