Fork-exec

Fork-exec

Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new program. 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 actually an exact copy of the parent - which would probably be of limited use - so it replaces itself with another process using the system call exec().

When a process forks, a complete copy of the executing program is made into the new process. This new process (which is a child of the parent) has a new process identifier (PID). The fork() function returns the child's PID to the parent, while it returns 0 to the child, in order to allow the two identical processes to distinguish from each other.

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 and address space 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 of the program that the child executed. Otherwise, the parent can ignore the child process and continue executing as it normally would; to prevent the child becoming a zombie it should wait on children at intervals or on SIGCHLD.

When the child process calls exec(), all data in the original program is lost, and replaced with a running copy of the new program. This is known as overlaying. Although all data is replaced, the file descriptors 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.

References

* [http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/node91.html File descriptors across fork(2)/exec(2) paper in Mcgill University]
* [http://www-h.eng.cam.ac.uk/help/tpl/unix/fork.html fork page in the Engineering Department of Cambridge University]
*"Advanced Programming in the UNIX Environment", W.Richard Stevens, Addison-Wesley ISBN 0-201-56317-7
*"Unix Power Tools", J Peek, T O'Reilly, M Loukides, O'Reilly, ISBN 1-56592-260-3


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Fork (operating system) — In computing, when a process forks, it creates a copy of itself, which is called a child process. The original process is then called the parent process . More generally, a fork in a multithreading environment means that a thread of execution is… …   Wikipedia

  • Exec (operating system) — The exec functions of Unix like operating systems are a collection of functions that causes the running process to be completely replaced by the program passed as argument to the function. As a new process is not created, the process ID (PID)… …   Wikipedia

  • Fork — У этого термина существуют и другие значения, см. Форк (значения). В Unix системах, fork()  системный вызов, создающий новый процесс( потомок), который является почти точной копией процесса( родителя), выполняющего этот вызов. Между… …   Википедия

  • Fork bomb — The concept behind the fork bomb – the processes recursively fork until a denial of service or a crash occurs In computing, the fork bomb is a form of denial of service attack against a computer system which makes use of the fork operation (or… …   Wikipedia

  • fork — У этого термина существуют и другие значения, см. Форк (значения). В Unix системах, fork()  системный вызов, создающий новый процесс (потомок), который является практически полной копией процесса родителя, выполняющего этот вызов. Между… …   Википедия

  • Spawn (computing) — Spawn is a function that loads and executes a new child process in the Microsoft Windows line of operating systems. The current process may or may not continue to execute asynchronously. Creating a new subprocess requires enough memory in which… …   Wikipedia

  • Exit (operating system) — A computer process terminates its execution by making an exit system call. More generally, an exit in a multithreading environment means that a thread of execution has stopped running. The operating system reclaims resources (memory, files, etc.) …   Wikipedia

  • Overlay (operating system) — For the division of a single program to reduce memory requirements, see Overlay (programming). In operating systems, an overlay is when a process replaces itself with the code of another program. On Unix like systems, this is accomplished with… …   Wikipedia

  • Cygwin — Running Cygwin, including Cygwin/X, under Windows XP …   Wikipedia

  • OS-9 — For the Macintosh operating system, see Mac OS 9. OS 9 Company / developer Microware (now owned by RadiSys Corporation) Programmed in C, Assembly language OS family Unix like Working state Current …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”