comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: How to spawn, fork, and exec within Ada (Do you have small example program)
Date: Wed, 28 Jun 2006 19:02:12 -0700
Date: 2006-06-28T19:02:12-07:00	[thread overview]
Message-ID: <8P-dndFaB7bPqT7ZnZ2dnUVZ_tydnZ2d@comcast.com> (raw)
In-Reply-To: 1151501695.077579.108560@i40g2000cwc.googlegroups.com

"Chris L" <clusardi2k@aol.com> wrote in message 
news:1151501695.077579.108560@i40g2000cwc.googlegroups.com...
>
> Steve wrote:
>> The answer is the same: you may be able to do it, but you probably don't
>> really want to.  There are other ways of achieving the same functionality
>> that are a lot easier.
>
> Could you elaborate with an example? Do you mean use one process
> instead of a parent child pair of processies?

It is hard for me to answer your question without first having the answer to 
mine, but I'll try to give a little information and see if it helps.

If I understand correctly, in good ole Unix the only took for creating new 
threads of execution (prior to PThreads) is "fork" (I can't say for sure 
since I haven't worked a lot in Unix).  "fork" is a system call that 
replicates the calling process and returns a status telling the caller 
whether it is the parent or child.  It is a simple and powerful system, but 
is not portable.

In Ada, tasking is built into the language.  The Ada run time library 
abstracts the implementation of tasking on the target environment.  For 
example with GNAT on Linux two tasking models are available, one that uses 
pthreads, and one that uses separate processes, the underlying mechanism 
(usually) doesn't matter to the Ada application.

Here is an example of a simple tasking Ada program:

with Ada.Integer_Text_IO;
with Ada.Text_IO;
procedure SimpleTaskDemo is

  task type MakeNoise is
    entry Start( which : Natural );
  end MakeNoise;

  task body MakeNoise is
    me : Natural;
  begin
    accept Start( which : Natural ) do
        me := which;
    end;
    for i in 1 .. 10 loop
      Ada.Text_IO.Put( "Hello from: " );
      Ada.Integer_Text_IO.Put( me, 3 );
      Ada.Text_IO.Put( " Count: " );
      Ada.Integer_Text_IO.Put( i, 3 );
      Ada.Text_IO.New_Line;
      delay 1.0;
    end loop;
  end MakeNoise;

  task1 : MakeNoise;
  task2 : MakeNoise;
begin
  task1.Start( 1 );
  delay 0.5;
  task2.Start( 2 );
end SimpleTaskDemo;

It creates two instances of the task "MakeNoise", task1 and task2.  Each 
task displays "Hello from: <n> Count: <n> in a loop.  Here is the output:

Hello from:   1 Count:   1
Hello from:   2 Count:   1
Hello from:   1 Count:   2
Hello from:   2 Count:   2
Hello from:   1 Count:   3
Hello from:   2 Count:   3
Hello from:   1 Count:   4
Hello from:   2 Count:   4
Hello from:   1 Count:   5
Hello from:   2 Count:   5
Hello from:   1 Count:   6
Hello from:   2 Count:   6
Hello from:   1 Count:   7
Hello from:   2 Count:   7
Hello from:   1 Count:   8
Hello from:   2 Count:   8
Hello from:   1 Count:   9
Hello from:   2 Count:   9
Hello from:   1 Count:  10
Hello from:   2 Count:  10

I wrote this program using GNAT on Windows XP.  It will also run without 
modification on Linux and numerous other target systems without 
modification.

I successfully ported a non-windowed Ada application from Windows to Linux 
in a matter of a couple of hours.  The only reason the sources had to be 
modified is the interface to networking on the two operating systems are 
different.  If I were to do the same today I would use AdaSockets which 
abstracts the network interface so I shouldn't have to make any source 
modifications.

But then, I'm still not sure if this is what you are asking,

Steve
(The Duck)

>
> Thank you,
> Christopher Lusardi
> 





      parent reply	other threads:[~2006-06-29  2:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1151434435.502270.265470@m73g2000cwd.googlegroups.com>
2006-06-27 20:50 ` How to spawn, fork, and exec within Ada (Do you have small example program) Jeffrey R. Carter
2006-06-27 21:03 ` Ludovic Brenta
2006-06-28 13:53   ` Frank J. Lhota
2006-06-28  2:41 ` Steve
     [not found]   ` <1151501695.077579.108560@i40g2000cwc.googlegroups.com>
2006-06-28 15:56     ` Martin Krischik
2006-06-28 20:42       ` Frank
2006-06-29  2:02     ` Steve [this message]
replies disabled

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