LolaTheriotWhat is a fork in computer programming?

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 succesfull 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.

Tags: , , , , , ,