Processes are the primitive units for allocation of system resources. Each process has its own address space and (usually) one thread of control. A process executes a program; you can have multiple processes executing the same program, but each process has its own copy of the program within its own address space and executes it independently of the other copies.
Processes are organized hierarchically. Each process has a parent process which explicitly arranged to create it. The processes created by a given parent are called its child processes. A child inherits many of its attributes from the parent process.
This chapter describes how a program can create, terminate, and control child processes. Actually, there are three distinct operations involved: creating a new child process, causing the new process to execute a program, and coordinating the completion of the child process with the original program.
The system function provides a simple, portable mechanism for running another program; it does all three steps automatically. If you need more control over the details of how this is done, you can use the primitive functions to do each step individually instead.
The easy way to run another program is to use the system function. This function does all the work of running a subprogram, but it doesn't give you much control over the details: you have to wait until the subprogram terminates before you can do anything else.
int function>system/function> (const char *command) This function executes command as a shell command. In the GNU C library, it always uses the default shell sh to run the command. In particular, it searches the directories in PATH to find programs to execute. The return value is -1 if it wasn't possible to create the shell process, and otherwise is the status of the shell process. the section called “Process Completion”, for details on how this status code can be interpreted.
If the command argument is a null pointer, a return value of zero indicates that no command processor is available.
This function is a cancellation point in multi-threaded programs. This is a problem if the thread allocates some resources (like memory, file descriptors, semaphores or whatever) at the time system is called. If the thread gets canceled these resources stay allocated until the program ends. To avoid this calls to system should be protected using cancellation handlers.
The system function is declared in the header file stdlib.h.
Portability Note: Some C implementations may not have any notion of a command processor that can execute other programs. You can determine whether a command processor exists by executing system (NULL); if the return value is nonzero, a command processor is available.
The popen and pclose functions (the section called “Pipe to a Subprocess”) are closely related to the system function. They allow the parent process to communicate with the standard input and output channels of the command being executed.