For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2006 > Relationship Between fork() and execle()









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 Relationship Between fork() and execle()
David T. Ashley

2006-12-21, 7:05 pm

I'm a brand new(*) Unix/Linux(**) programmer, and my C-language program
would like to execute another compiled C-language program, passing it some
command-line parameters, and wait until the child process terminates and use
its result code.

I expected to find a C library function that simply ran a program as a child
(such as one has in PHP), but the available function calls [exec..()] seem
to overlay the _current_ process' memory rather than spawning a new process.

Here are my questions:

a)Is there a function that will launch a child (rather than overlay the
current process)?

b)If NO to (a), I'm assuming that I'd need to fork() followed by exec..()?

c)Are there any Unix/Linux programming books to get me started with this
stuff? I'll also need to learn about pipes and related IPC mechanisms.

Thanks, Dave.

(*) = I've written a "Hello, world!" program and it ran but done nothing
more elaborate on Unix/Linux.

(**) = If it makes a difference, the target system is Linux.



Kohn Emil Dan

2006-12-21, 7:05 pm



On Thu, 21 Dec 2006, David T. Ashley wrote:

> I'm a brand new(*) Unix/Linux(**) programmer, and my C-language program
> would like to execute another compiled C-language program, passing it some
> command-line parameters, and wait until the child process terminates and use
> its result code.
>
> I expected to find a C library function that simply ran a program as a child
> (such as one has in PHP), but the available function calls [exec..()] seem
> to overlay the _current_ process' memory rather than spawning a new process.
>
> Here are my questions:
>
> a)Is there a function that will launch a child (rather than overlay the
> current process)?



I think that for your case you can try the system() function. It's a
standard C function. If you want to capture the standard output of your
command or pass data to its standard input, but not both, you can try the
popen() function. Look up in the man pages for their descriptions.


>
> b)If NO to (a), I'm assuming that I'd need to fork() followed by exec..()?
>


Whether (a) is NO or YES depends on whether you accept the limitations of
the alternatives I have suggested.

> c)Are there any Unix/Linux programming books to get me started with this
> stuff? I'll also need to learn about pipes and related IPC mechanisms.


Richard W. Stevens: "Advanced Programming in the UNIX Environment"

Also by Stevens: "UNIX Network Programming", volume 2. Volume 2 deals with
various IPC mechanisms.

Emil



>
> Thanks, Dave.
>
> (*) = I've written a "Hello, world!" program and it ran but done nothing
> more elaborate on Unix/Linux.
>
> (**) = If it makes a difference, the target system is Linux.
>
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com