comp.lang.ada
 help / color / mirror / Atom feed
* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
       [not found] <1151434435.502270.265470@m73g2000cwd.googlegroups.com>
@ 2006-06-27 20:50 ` Jeffrey R. Carter
  2006-06-27 21:03 ` Ludovic Brenta
  2006-06-28  2:41 ` Steve
  2 siblings, 0 replies; 7+ messages in thread
From: Jeffrey R. Carter @ 2006-06-27 20:50 UTC (permalink / raw)


Chris L wrote:
> These care routinely done in C/C++. How can they be done in Ada? Do you
> have a short program example of these?

The equivalent of spawn and fork are done by dynamically creating a 
task. When you need to execute another program, that's done by calling 
the OS, which is OS and compiler dependent. As an example, you can use 
GNAT.OS_Lib if your compiler is GNAT (works on all platforms supported 
by GNAT).

-- 
Jeff Carter
"Many times we're given rhymes that are quite unsingable."
Monty Python and the Holy Grail
57



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

* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
       [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
  2 siblings, 1 reply; 7+ messages in thread
From: Ludovic Brenta @ 2006-06-27 21:03 UTC (permalink / raw)


Chris L writes:
> These care routinely done in C/C++. How can they be done in Ada? Do
> you have a short program example of these?
>
> Thank you,
> Christopher Lusardi

fork, exec, and spawn are specific to Unix and Unix-like systems; so
the answer depends on your compiler and operating system.  If GNAT on
Unix, GNU, or *BSD, look at GNAT.Expect.Non_Blocking_Spawn.

You can also "routinely" call any C function from an Ada program, so
if you insist, you can really fork and exec.  But that would be the
wrong (error-prone) way of achieving the same result.

-- 
Ludovic Brenta.



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

* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
       [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  2:41 ` Steve
       [not found]   ` <1151501695.077579.108560@i40g2000cwc.googlegroups.com>
  2 siblings, 1 reply; 7+ messages in thread
From: Steve @ 2006-06-28  2:41 UTC (permalink / raw)


"Chris L" <clusardi2k@aol.com> wrote in message 
news:1151434435.502270.265470@m73g2000cwd.googlegroups.com...
> These care routinely done in C/C++. How can they be done in Ada? Do you
> have a short program example of these?
>
> Thank you,
> Christopher Lusardi
>

Are you:

A) An Ada programmer that has run into a special need to do a spawn, fork, 
or exec?

B) A C/C++ Unix programmer starting to use Ada for the first time and trying 
to do things the same way you are used to in C/C++?

If the answer is B, it's kind of like a shipbuilder that is used to working 
in steel asking a woodworker how to weld the parts of a drawer together.

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.

Steve
(The Duck)






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

* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
  2006-06-27 21:03 ` Ludovic Brenta
@ 2006-06-28 13:53   ` Frank J. Lhota
  0 siblings, 0 replies; 7+ messages in thread
From: Frank J. Lhota @ 2006-06-28 13:53 UTC (permalink / raw)


Ludovic Brenta wrote:
> fork, exec, and spawn are specific to Unix and Unix-like systems; so
> the answer depends on your compiler and operating system.  If GNAT on
> Unix, GNU, or *BSD, look at GNAT.Expect.Non_Blocking_Spawn.

One should also point out that on the MS Windows systems, there is no OS 
call equivalent to the Unix "fork" or "exec" functions. The "spawn" 
functionality can be achieved using the Win32 / Win64 function 
"CreateProcess".

> You can also "routinely" call any C function from an Ada program, so
> if you insist, you can really fork and exec.  But that would be the
> wrong (error-prone) way of achieving the same result.
> 



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

* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
       [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
  1 sibling, 1 reply; 7+ messages in thread
From: Martin Krischik @ 2006-06-28 15:56 UTC (permalink / raw)


Chris L wrote:

> 
> 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?

Depends in what you want to archive. Ada has build in support for threads
(called task in Ada and lightweight processes in unix) - so unless you want
to start "/bin/ls" or "/bin/sh" you might be better off using tasks instead
of processes.

If you still need processes then AdaCL [1] has the needed support.

Martin

[1] http://adacl.sf.net
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
  2006-06-28 15:56     ` Martin Krischik
@ 2006-06-28 20:42       ` Frank
  0 siblings, 0 replies; 7+ messages in thread
From: Frank @ 2006-06-28 20:42 UTC (permalink / raw)


Hi!

I give you these links in case it is useful.
It gives a course in how to create concurrency in Ada.

http://en.wikibooks.org/wiki/Ada_Programming/Tasking
http://linux.disco.unimib.it/~ferretti/lp/ada_task.html

Frank





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

* Re: How to spawn, fork, and exec within Ada (Do you have small example program)
       [not found]   ` <1151501695.077579.108560@i40g2000cwc.googlegroups.com>
  2006-06-28 15:56     ` Martin Krischik
@ 2006-06-29  2:02     ` Steve
  1 sibling, 0 replies; 7+ messages in thread
From: Steve @ 2006-06-29  2:02 UTC (permalink / raw)


"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
> 





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

end of thread, other threads:[~2006-06-29  2:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox