2.11 Process and Environment Control

The process-control routines allow you to start, stop, and manage processes from within a program. Environment-control routines allow you to get and change information about the operating-system environment.

A “process” is a program being executed by the operating system. It consists of the program's code and data, plus information about the process, such as the number of open files. Whenever you execute a program at the operating-system level, you start a process. All process-control functions except signal are declared in the include file PROCESS.H. The signal function is declared in SIGNAL.H. The abort, exit, and system functions are also declared in the STDLIB.H include file. The environment-control routines (getenv and _putenv) are declared in STDLIB.H.

Routine Use

abort Aborts a process without flushing buffers or calling functions registered by atexit and _onexit
assert Tests for logic error
atexit Schedules routines for execution at program termination
_cexit Performs the exit termination procedures (such as flushing buffers) and returns control to the calling program
_c_exit Performs the _exit termination procedures and returns control to the calling program
_execl Executes child process with argument list
_execle Executes child process with argument list and given environment
_execlp Executes child process using PATH variable and argument list
_execlpe Executes child process using PATH variable, given environment, and argument list
_execv Executes child process with argument array
_execve Executes child process with argument array and given environment
_execvp Executes child process using PATH variable and argument array
_execvpe Executes child process using PATH variable, given environment, and argument array
exit Calls functions registered by atexit and _onexit, then flushes all buffers and closes all open files before terminating the process
_exit Terminates process without processing atexit or _onexit functions or flushing buffers
_fatexit Schedules routines for execution at program termination (memory-model independent)
_fonexit Schedules routines for execution at program termination (memory-model independent)
getenv Gets the value of an environment variable
_getpid Gets process ID number
longjmp Restores a saved stack environment
_onexit Schedules routines for execution at program termination
perror Prints error message
_putenv Adds or changes the value of an environment variable
raise Sends a signal to the calling process
setjmp Saves a stack environment
signal Handles an interrupt signal
_spawnl Executes child process with argument list
_spawnle Executes child process with argument list and given environment
_spawnlp Executes child process using PATH variable and argument list
_spawnlpe Executes child process using PATH variable, given environment, and argument list
_spawnv Executes child process with argument array
_spawnve Executes child process with argument array and given environment
_spawnvp Executes child process using PATH variable and argument array
_spawnvpe Executes child process using PATH variable, given environment, and argument array
system Executes an operating-system command

The atexit and _onexit routines create a list of functions to be executed when the calling program terminates. The only difference between the two is that atexit is part of the ANSI standard. The _onexit function is offered for compatibility with previous versions of Microsoft C.

The _exit routine terminates a process immediately, whereas exit terminates the process only after flushing buffers and calling any functions previously registered by atexit and _onexit. The _cexit and _c_exit routines are identical to exit and _exit, respectively, except that they return control to the calling program without terminating the process.

The setjmp and longjmp routines save and restore a stack environment. These allow you to execute a nonlocal goto.

The _exec and _spawn routines start a new process called the “child” process. The difference between the _exec and _spawn routines is that the _spawn routines are capable of returning control from the child process to its caller (the “parent” process). Both the parent process and the child process are present in memory (unless _P_OVERLAY is specified). In the _exec routines, the child process overlays the parent process, so returning control to the parent process is impossible (unless an error occurs when attempting to start execution of the child process).

There are eight forms each of the _exec and _spawn routines (see Table 2.1). The differences among the forms involve the method of locating the file to be executed as the child process, the method for passing arguments to the child process, and the method of setting the environment.

Passing an argument list means that the arguments to the child process are listed separately in the _exec or _spawn call. Passing an argument array means that the arguments are stored in an array, and a pointer to the array is passed to the child process. The argument-list method is typically used when the number of arguments is constant or is known at compile time. The argument-array method is useful when the number of arguments must be determined at run time.

Table 2.1 Forms of the _spawn and _exec Routines


Routines

Locating the File
Argument-Passing Convention
Environment Settings

_execl, _spawnl Do not use PATH Argument list Inherited from parent
_execle, _spawnle Do not use PATH Argument list , Pointer to environment table for child process passed as last argument  
_execlp, _spawnlp Use PATH Argument list Inherited from parent
_execlpe, _spawnlpe Use PATH Argument list Pointer to environment table for child process passed as last argument
_execv, _spawnv Do not use PATH Argument array , Inherited from parent  
_execve, _spawnve Do not use PATH Argument array Pointer to environment table for child process passed as last argument
_execvp, _spawnvp Use PATH Argument array Inherited from parent
_execvpe, _spawnvpe Use PATH Argument array Pointer to environment table for child process passed as last argument

The assert macro is typically used to test for logic errors. It prints a message when a given “assertion” fails to hold true. Defining the identifier NDEBUG to any value causes occurrences of assert to be removed from the source file, thus allowing you to turn off assertion checking without modifying the source file.