Using EXEC and SPAWN to Link to Another Program

If you are writing a program in C or C++ there are occasions when you want another program to execute. Sometimes you need your program to continue running while this other program runs and other times you just want to start the other program and terminate your program.

The commands to start other programs from within a C or C++ program are called EXEC and SPAWN and they come in a number of different versions depending on how you want to pass parameters to the other program.

Spawn

The Spawn command is used to start execution of another program without terminating the existing program. The first parameter with the Spawn command tells the system whether the current program should wait for the new program to finish before continuing (P_WAIT) or to execute in parallel (P_NOWAIT). The other parameters identify the program to be executed, any arguments to be passed to the program to be executed, and any environment variables that need to be set before the program is executed.

The program to be executed needs to have a path specified for it unless the program can be found in the current working directory. If no file type is specified then a program of file type .com will be executed (if one exists) otherwise one of file type .exe will be executed (if that exists). If no path is specified in the program argument and the program doesn't exist in the current directory then the directories referenced in the path statement will be searched if you use the spawnlp, spawnlpe, spawnvp, or spawnvpe command. If you use spawnl, spawnle, spawnv, or spawnve and the specified program is not found in the current working directory (or specified path if one was specified) then the command will fail.

Arguments can be passed to the new program in a number of different ways. They can each be passed separately (using spawnl, spawnle, spawnlp, or spawnlpe) or they can be passed as a vector of pointers (using spawnv, spawnve, spawnvp, or spawnvpe). When the arguments are passed separately, you need to follow the arguments with an additional NULL argument to terminate the list.

The environment variables to be used by the new program can be inherited from your calling program (using spawnl, spawnlp, spawnv, or spawnvp) or can be defined using an environment argument passed in the span command following the program parameters (using spawnle, spawnlpe, spawnve, or spawnvpe).

The following are the different versions of the Spawn command as described above:

int spawnl(  mode, path, arg0, arg1,...,argn, NULL);
int spawnle( mode, path, arg0, arg1,...,argn, NULL, envp);
int spawnlp( mode, file, arg0, arg1,...,argn, NULL);
int spawnlpe(mode, file, arg0, arg1,...,argn, NULL, envp);
int spawnv(  mode, path, argv);
int spawnve( mode, path, argv, envp);
int spawnvp( mode, file, argv);
int spawnvpe(mode, file, argv, envp);

Exec

The Exec command is used to start execution of another program and end the execution of the existing program. The parameters identify the program to be executed, any arguments to be passed to the program to be executed, and any environment variables that need to be set before the program is executed.

The program to be executed needs to have a path specified for it unless the program can be found in the current working directory. If no file type is specified then a program of file type .com will be executed (if one exists) otherwise one of file type .exe will be executed (if that exists). If no path is specified in the program argument and the program doesn't exist in the current directory then the directories referenced in the path statement will be searched if you use the execlp, execlpe, execvp, or execvpe command. If you use execl, execle, execv, or execve and the specified program is not found in the current working directory (or specified path if one was specified) then the command will fail.

Arguments can be passed to the new program in a number of different ways. They can each be passed separately (using execl, execle, execlp, or execlpe) or they can be passed as a vector of pointers (using execv, execve, execvp, or execvpe). When the arguments are passed separately, you need to follow the arguments with an additional NULL argument to terminate the list.

The environment variables to be used by the new program can be inherited from your calling program (using execl, execlp, execv, or execvp) or can be defined using an environment argument passed in the span command following the program parameters (using execle, execlpe, execve, or execvpe).

The following are the different versions of the Exec command as described above:

int execl(  path, arg0, arg1, ..., argn, NULL);
int execle( path, arg0, arg1, ..., argn, NULL, envp);
int execlp( file, arg0, arg1, ..., argn, NULL);
int execlpe(file, arg0, arg1, ..., argn, NULL, envp);
int execv(  path, argv);
int execve( path, argv, envp);
int execvp( file, argv);
int execvpe(file, argv, envp);

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate