Re: How to spawn, fork, and exec within Ada (Do you have small example program)



"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



.



Relevant Pages

  • High CPU in tasking
    ... I wrote an data stream decoder and output manifolder using Ada ... Debugging turned out: ... - Tasking overhead caused the CPU load. ...
    (comp.lang.ada)
  • Re: C++ threads vs. Ada tasks - surprised
    ... from having tasking built-in the language. ... none *functional* gain (there is an obvious gain in readability, ... This makes sense, except that with a higher-level tasking model, there ... I would not have expected much speed difference; Ada tasks map quite ...
    (comp.lang.ada)
  • Re: Isnt this in favour of Ada??
    ... A good text that not only had step-by-step introduction of tasking but a bunch of practical examples of when to use it would be a good thing. ... We have five or six different books lying around the office with names like "Object Oriented Programming in Ada", "Ada as a second language", etc. ... None do a good job of covering advanced features of Ada, such as tasking, or Ada's object oriented programming. ...
    (comp.lang.ada)
  • Re: High CPU in tasking
    ... > I wrote an data stream decoder and output manifolder using Ada ... > tasking and a protected ringbuffer. ... > - Every writing to the ringbuffer wakes up all reader tasks. ... > - Tasking overhead caused the CPU load. ...
    (comp.lang.ada)