Fork–exec
   HOME

TheInfoList



OR:

Fork–exec is a commonly used technique in Unix whereby an executing process spawns a new program.


Description

fork() is the name of the system call that the parent process uses to "divide" itself ("fork") into two identical processes. After calling fork(), the created child process is an exact copy of the parent except for the return value of the fork() call. This includes open files, register state, and all memory allocations, which includes the program's executable code. In some cases the two continue to run the same binary, but often one (usually the child) switches to running another binary executable using the exec() system call. When a process forks, a complete copy of the executing program is made into the new process. This new process is a child of the parent process, and has a new process identifier (PID). The fork() function returns the child's PID to the parent process. The fork() function returns 0 to the child process. This enables the two otherwise identical processes to distinguish one another. The parent process can either continue execution or wait for the child process to complete. The child, after discovering that it is the child, replaces itself completely with another program, so that the
code In communications and information processing, code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form, sometimes shortened or secret, for communication through a communication ...
and
address space In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity. For software programs to save and retrieve st ...
of the original program are lost. If the parent chooses to wait for the child to die, then the parent will receive the
exit code The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred t ...
of the program that the child executed. To prevent the child becoming a zombie the parent should call wait on its children, either periodically or upon receiving the SIGCHLD signal, which indicates a child process has terminated. One can also asynchronously wait on their children to finish, by using a signal handler for SIGCHLD, if they need to ensure everything is cleaned up. Here's an example of a signal handler that catches any incoming SIGCHLD signals and handles multiple concurrent signals received. void cleanup(int signal) When the child process calls exec(), all data in the original program is lost, and it is replaced with a running copy of the new program. This is known as overlaying. Although all data are replaced, the
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically have ...
s that were open in the parent are closed only if the program has explicitly marked them ''close-on-exec''. This allows for the common practice of the parent creating a pipe prior to calling fork() and using it to communicate with the executed program.
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
does not support the fork-exec model, as it does not have a system call analogous to fork(). The spawn() family of functions declared in process.h can replace it in cases where the call to fork() is followed directly by exec().


References


"File descriptors across fork(2)/exec(2)"
Operating Systems (Course 304-427B), Franco Callari, Electrical Engineering Department, McGill University
"fork and exec"
Tim Love, University of Cambridge Engineering Department * ''Advanced Programming in the UNIX Environment'',
W. Richard Stevens William Richard (Rich) Stevens (February 5, 1951September 1, 1999) was a Northern Rhodesia-born American author of computer science books, in particular books on UNIX and TCP/IP. Biography Richard Stevens was born in 1951 in Luanshya, Northern Rh ...
, Addison-Wesley * ''Unix Power Tools'', Jerry Peek, Tim O'Reilly, Mike Loukides, O'Reilly, {{DEFAULTSORT:Fork-Exec C POSIX library Process (computing)