Caution: This version of this document is no longer maintained. For the latest documentation, see


Spawn a child process, given a list of arguments and an environment


#include <process.h>

int spawnle( int mode, 
             const char * path, 
             const char * arg0, 
             const char * arg1..., 
             const char * argn, 
             const char * envp[] );


How you want to load the child process, and how you want the parent program to behave after the child program is initiated:
The full path name of the executable.
arg0, argn, NULL
The arguments that you want to pass to the new process. You must terminate the list with an argument of NULL.
NULL, or a pointer to an array of character pointers, each pointing to a string that defines an environment variable. The array is terminated with a NULL pointer. Each pointer points to a character string of the form:

that's used to define an environment variable.



Use the -l c option to qcc to link against this library. This library is usually included automatically.


The spawnle() function creates and executes a new child process, named in path with NULL-terminated list of arguments in arg0 ... argn and with the environment specified in envp. This function calls spawnve().

Note: If the new child process is a shell script, the first line must start with #!, followed by the path and arguments of the shell to be run to interpret the script. The script must also be marked as executable.

The spawnle() function isn't a POSIX 1003.1 function, and isn't guaranteed to behave the same on all operating systems. It builds argv[ ] and envp[ ] arrays before calling spawn().

To view the documentation for a function, click its name in this diagram:

spawn spawnve spawnl spawnv spawnle spawnp spawnvpe spawnlpe spawnvp spawnlp

How the spawn functions are related

Most of the spawn*() functions do a lot of work before a message is sent to procnto.

Arguments are passed to the child process by supplying one or more pointers to character strings as arguments. These character strings are concatenated with spaces inserted to separate the arguments to form one argument string for the child process. At least one argument, arg0, must be passed to the child process. By convention, this first argument is a pointer to the name of the new child process.

If envp is NULL, the child process inherits the environment of the parent process. The new process can access its environment by using the environ global variable (found in <unistd.h>).

Note: A parent/child relationship doesn't imply that the child process dies when the parent process dies.


The spawnle() function's return value depends on the mode argument:

mode Return value
P_WAIT The exit status of the child process.
P_NOWAIT The process ID of the child process. To get the exit status for a P_NOWAIT process, you must use the waitpid() function, giving it this process ID.
P_NOWAITO The process ID of the child process, or 0 if the process is being started on a remote node. You can't get the exit status of a P_NOWAITO process.

If an error occurs, -1 is returned (errno is set).


The number of bytes used by the argument list or environment list of the new child process is greater than ARG_MAX bytes.
Search permission is denied for a directory listed in the path prefix of the new child process or the new child process's file doesn't have the execute bit set.
Insufficient resources available to create the child process.
An error occurred duplicating open file descriptors to the new process.
One of the buffers specified in the function call is invalid.
Too many levels of symbolic links or prefixes.
Insufficient resources available to load the new executable image or to remap file descriptors in the child process.
The length of path exceeds PATH_MAX or a pathname component is longer than NAME_MAX.
The file identified by the path argument is empty, or one or more components of the pathname of the child process don't exist.
The child process's file has the correct permissions, but isn't in the correct format for an executable.
Insufficient memory available to create the child process.
The spawnle() function isn't implemented for the filesystem specified in path.
A component of the path prefix of the child process isn't a directory.


Run myprog as if the user had typed:

myprog ARG1 ARG2

at the command-line:

#include <stddef.h>
#include <process.h>

char *env_list[] = { "SOURCE=MYDATA",

spawnle( P_WAIT, "myprog",
    "myprog", "ARG1", "ARG2", NULL,
     env_list );

The program is found if myprog is in the current working directory. The environment for the child program consists of the three environment variables SOURCE, TARGET and lines.



Cancellation point Read the Caveats
Interrupt handler No
Signal handler No
Thread Yes


If mode is P_WAIT, this function is a cancellation point.

See also:

execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), getenv(), putenv(), setenv(), spawn(), spawnl(), spawnlp(), spawnlpe(), spawnp(), spawnv(), spawnve(), spawnvp(), spawnvpe(), wait(), waitpid()