What is a fork in computer programming?

Answer

fork() is a concept that originated in the UNIX world. Running programs are called processes, and the only way to execute a new program is for a current program to fork() itself, thereby creating a seperate process. The process that called fork() is known as the parent process, and the newly created process is known as the child process.

The child process, which begins its life as a copy of the parent process, can be "replaced".

In UNIX, the first program that is executed after booting finishes is the init process. This process will fork() and load the getty program over the child process, thereby creating a process which will prompt the user for a password.

Since a call to fork() creates a child process which is in essence the same as the parent process, it is necesarry to determine (from within the process) which process is the child, and which is the parent.

Upon sucesfull execution, the fork() call returns the child's Process ID in the parent process, and a 0 in the child process.

You can then use the child to execute a system call, such as running another program (or whatever you needed a second process for).

If you are using pipes (for communication between the child and parent process), make sure you have the parent wait for it.

 

Improve Answer Discuss the question "What is a fork in computer programming?" Watch Question

First answer by ID1162077129. Last edit by RuntuGen. Contributor trust: 99 [recommend contributor]. Question popularity: 76 [recommend question]


Research your answer:

Can you answer other questions about programming?

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > What is a fork in computer programming?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
What is fork?  Fork vs clone?  Fork in computer programming?  Stress in computer programming?  What is fork in computer language?  Why do you interest computer programming?