Unix Programming - why not exec( ) then fork( )

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2005 > why not exec( ) then fork( )





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author why not exec( ) then fork( )
50295@web.de

2005-02-11, 7:59 am

I tried doing:

exec(...);
fork();

with the hope of having a process excecute a process, and the
duplicating that process, but nothing happeneds. Why?

Thanks,

- Olumide

Jens.Toerring@physik.fu-berlin.de

2005-02-11, 7:59 am

50295@web.de wrote:
> I tried doing:


> exec(...);
> fork();


> with the hope of having a process excecute a process, and the
> duplicating that process, but nothing happeneds. Why?


exec() replaces the running program by a completely new program,
that then starts running. So exec() never returns (unless it
failed) and so the fork() will never happen. So if you have a
program A which executes the line

exec( "B",...);

then, after this line, program A isn't running anymore but
program B instead. And since A doesn't exist anymore there's
nothing that would do the fork().
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Måns Rullgård

2005-02-11, 7:59 am

50295@web.de writes:

> I tried doing:
>
> exec(...);
> fork();
>
> with the hope of having a process excecute a process, and the
> duplicating that process, but nothing happeneds. Why?


The exec() call entirely replaces your process with the new
executable, leaving no trace of the old one, except for open file
descriptors.

What exactly are you trying to achieve?

--
Måns Rullgård
mru@inprovide.com
50295@web.de

2005-02-11, 6:03 pm

Thanks Jens,

maybe that's why I get nothing when I add a puts() to the end of the
fork and execs, like so:

exec(...);
fork();
puts("Do you see me?"); /* I dont get to see this */

- Olumide

Jens.Toerring@physik.fu-berlin.de

2005-02-11, 6:03 pm

50295@web.de wrote:
> maybe that's why I get nothing when I add a puts() to the end of the
> fork and execs, like so:


> exec(...);
> fork();
> puts("Do you see me?"); /* I dont get to see this */


Yes, definitely. Unless exec() fails nothing after the exec-call
ever gets executed.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Jim Cochrane

2005-02-11, 6:03 pm

In article <1108131427.889571.291590@c13g2000cwb.googlegroups.com>, 50295@web.de wrote:
> Thanks Jens,
>
> maybe that's why I get nothing when I add a puts() to the end of the
> fork and execs, like so:
>
> exec(...);
> fork();
> puts("Do you see me?"); /* I dont get to see this */
>
> - Olumide


It's like a judge ordering a sentence of death, followed by 30 years of
hard labor. :-)

--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
Nitin

2005-02-13, 5:50 pm

why don't u try popen or system ?????

David Schwartz

2005-02-14, 5:54 pm


<50295@web.de> wrote in message
news:1108130155.157368.203550@c13g2000cwb.googlegroups.com...

>I tried doing:
>
> exec(...);
> fork();


> with the hope of having a process excecute a process, and the
> duplicating that process, but nothing happeneds. Why?


You want to fork then exec twice. Unless there's an error, the code
after 'exec' will never run because at that point you're executing another
program.

DS


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com