LINUX TIPS AND TRICKS --- April 14, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Launching A New Process From A Running Process
by Danny Kalev

The exec() family of functions defines six different forms of launching
a program from another program. All these functions do the same thing --
they replace the currently running process (the terms "program" and 
"process" are interchangeable). If they succeed, they never return 
(because the process that invoked them is no longer running). If they 
fail, they return -1 to their caller and store the error code in errno. 
Note that unlike ordinary function calls, which return to their caller, 
exec() functions supersede the running process. It's as if you stopped 
the currently running process and launched another one. Here are their
prototypes:

int execl(const char * file, const char *arg0,..., const char *argn,
NULL);
int execle(const char * file, const char *arg0,..., const char *argn,
NULL, const char **env);
int execlp(const char * file, const char *arg0,..., const char *argn,
NULL);
int execv(const char * file, const char **argv);
int execve(const char * file, const char **argv, const char **env);
int execvp(const char * file, const char **argv);

Each function takes a name of an executable file or its path name as the
first argument. Traditionally, the following arg0 contains the name of
the program being launched (it's identical to the first argument). The
rest are additional command-line arguments that are passed to the
launched program. Only execve() is a system call under Linux. The others
are implemented as wrapper functions that ultimately call execve(). The
affixes 'l', 'p', 'e', and 'v' in a function's name indicate the
following attributes:

l - indicates that the arg0, arg1, etc. are passed as separate arguments
rather than as a single array. 

v - indicates that the arguments are packed in a single array.

p - indicates that if an explicit path wasn't provided and the program
wasn't found in the current directory, the function searches for the
program in the directories specified in the PATH environment variable. 

e - indicates whether the function gets a different environment instead
of inheriting the parent's environment.

In the following example, the program launches a new program called
"copy" using execl():

#include <process.h>
int main()
{
  execl("copy", /*program to be invoked*/
        "copy", /*traditionally, argv[0] is the name of  the 
program*/
        "source.txt", /* argv[1] passed to the called program*/
        "target.txt", /*argv[2] passed to the called program */
        NULL /*indicates end of argument list */
       );
  printf("failed to launch copy"); /* we get here only if execl() failed
*/
}

Next Week: Implementing Multiprocessing with fork()

*********************************************************************

NEW! COMMUNITY DISCUSSIONS

If this column has raised questions or comments, let's hear them. 
Join author Danny Kalev's Linux Software Development discussion 
on ITworld.com Forums. Current topic: Heading for world domination? 
Probably not: http://forums.itworld.com/webx?14@@.ee6b652/6!skip=

********************************************************************* 

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
