This section gives an overview of processes and of the steps involved in creating a process and making it run another program.
A new processes is created when one of the functions
posix_spawn
, fork
, _Fork
, vfork
, or
pidfd_spawn
is called. (The system
and popen
also
create new processes internally.) Due to the name of the fork
function, the act of creating a new process is sometimes called
forking a process. Each new process (the child process or
subprocess) is allocated a process ID, distinct from the process
ID of the parent process. See Process Identification.
After forking a child process, both the parent and child processes
continue to execute normally. If you want your program to wait for a
child process to finish executing before continuing, you must do this
explicitly after the fork operation, by calling wait
or
waitpid
(see Process Completion). These functions give you
limited information about why the child terminated—for example, its
exit status code.
A newly forked child process continues to execute the same program as
its parent process, at the point where the fork
or _Fork
call returns. You can use the return value from fork
or
_Fork
to tell whether the program is running in the parent process
or the child.
Having several processes run the same program is only occasionally
useful. But the child can execute another program using one of the
exec
functions; see Executing a File. The program that the
process is executing is called its process image. Starting
execution of a new program causes the process to forget all about its
previous process image; when the new program exits, the process exits
too, instead of returning to the previous process image.