Re: How to spawn, fork, and exec within Ada (Do you have small example program)
- From: "Steve" <nospam_steved94@xxxxxxxxxxx>
- Date: Wed, 28 Jun 2006 19:02:12 -0700
"Chris L" <clusardi2k@xxxxxxx> wrote in message
news:1151501695.077579.108560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- Prev by Date: Re: Ada in Debian: most libraries will switch to the pure GPL in Etch
- Next by Date: Re: How would I determine the contents of a directory
- Previous by thread: Re: How to spawn, fork, and exec within Ada (Do you have small example program)
- Next by thread: comp.lang.ada.leypold
- Index(es):
Relevant Pages
|