What is fork()?
fork() is a command available on Unix systems (e.g. Linux, OS X). It is used to create a child process with a copy of memory contents the calling process is using. In most demonstration, C/C++ is generally used and the library <unistd.h> is needed to be included. fork() return to the caller a value of type “pid_t” which contains the process ID of the newly created one.
Consider the following code:
int a = 6;
pid_t pid;
pid = fork();
printf(”Hello world.”);
At first, you have only one process executing (call it “parent process”). After it reaches the command fork(), another process (call it “child process”) is created and the entire memory spaces of its parent are copied. Now, you will have seen 2 processes executing; which have “a = 6″. The parent process gets as the return value from fork() the process ID of its child, but the child process gets 0 as the return value. And then, the two processes (parent and child) continue their execution until the end. Note that the child does not execute from the beginning of the program code.
What concerns process creation?
When the operating system fork a child, there are many possibilities to do. One probably give advantages over another in some certain points of view, but give disadvantages in other points of view.
Consider the following questions:
- Should the child process execute concurrently with its parent? A programmer simply wants the child to do a particular job and return a desired value to the parent. This sounds reasonable to let the child complete its task and let the parent wait for the return value. However, another programmer might not want the parent to wait for. She probably wants the child process to help its parent executing some tasks. Think of your web browser. When you click a link to a new page, you would not like to stop the caller process. In fact, web browsers uses other techniques, like threads, rather than child process creation. I just give as an exmaple.
- A newly created process might use the same physical address, needless to copy memory contents from its parent. This would save the transfer time but the executions of the child and the parent are dependent. For example, the value of integer ‘a’ used by the parent might be changed by its child, as a result, the parent execution result gets distorted. Nonetheless, the shared spaces can be used to implement process synchronization easier (not taking into account about critical sections).
- Process management using child-and-parent system build up a large tree of processes. On Linux, the top most process (called the root) of the tree is “init”. When a process in the tree is killed, a few issues, again, arise. All the processes in the subtree shall be terminated if each of them is executing a job for its parent, its grand parent and so for that killed process. However, some processes in the subtree might not have any concerns with their parent. Recall that “init” is the root of all the processes on Linux, containing all the process in the system. If it is killed, the subtree should not be all terminated.
How does Linux handle process management?
With the 3 aspects that have been introduced namely: concurrent execution, address space sharing, and process termination, the example code below will answer all the questions.
#include <unistd.h>
#include <ostream.h>int main() {
int a = 0;
pid_t pid = fork();
if(pid > 0){
cout << “—– Parent Process —–” << endl;
cout << “ child process id = ” << pid << endl;
for(int i = 0; i < 6; i++)
cout << “ a = ” << a++ << “, &a = ” << &a << endl;
cout << “—– Waiting to be killed —–” << endl;
while(1);
} else {
cout << “—– Child Process —–” << endl;
for(int i = 0; i < 6; i++)
cout << “ a = ” << a– << “, &a = ” << &a << endl;
cout << “—– Waiting to be killed —–” << endl;
while(1);
}
return 0;
}
This program is intended, after forked, to have the parent keep counting from 0 to 5, and the child keep counting from - to -5. And then both parent and child get into infinite loops waiting for experimenter killing the parent manually.
The output from executing this program is:

The output from killing the parent process is:

What can be concluded? (Ananlysis)
“a = -5, &a = 0xbf9da920″ is a remarkable line. This was printed out from the child process since it is a negative number from counting down. The child process first executed and paused, the parent then executed, and the child finally executed the rest. The result obviously indicates that Linux allow concurrent execution between parent and child process.
Looking at “&a = 0xbf9da920″, both parent and child refered to the address of “a” at the same value. This would contradict the fact that the numbers counted by the parent and child were not interfered. If it had really been the same address, the counts would have some garbage. That is, the real address of “a” in the parent process and the child process must not be the same. The printed out addresses were just logical memory address given by the operating system. The conclusion is that they do not share the memory space. The “a” in the child has the same logical address as in its parent because it is an identical copy from its parent. The entire area is mapped identically.
Terminating the parent process using the command “kill” gives an obvious result. The child does survive. Then, where would the child be attached? Any child process which has lost its parent will be attach to the top most node (i.e. “init” process) This probably causes a great trouble if too many of processes lose its parent.
* “cout” is used instead of “printf” because of some technical problems.
** this program code is runnable only on Unix systems.
if you are using Windows “CreateProcess( … )” is used instead of fork().
*** equivalent commands for “ps” and “kill” on Windows are, “tasklist” and “taskkill“.

idiotao: You have made such a great tragedy during the experiment.
d0m3z: Ah .. Why?
idiotao: Someone dies. Some children have lost their parents.
March 19th, 2008 at 9:07 pm
what the fork() ??
March 20th, 2008 at 4:02 am
Gag: fork()!!! not fork()!
March 20th, 2008 at 12:59 pm
เฮ้ย กูกับเต๋ากำลังปรึกษากันว่าทำยังไงให้สาว ๆ มาทิ้งคอมเมนต์ในบล็อกบ้าง เหมือนอย่างของโดม สุดฮอต
spread tewson !
spread idiotao !
March 20th, 2008 at 8:56 pm
อย่าสงสัยว่าทำไม post นี้ ผมนึกยังงัยถึงเขียนเกี่ยวกับ fork() พอดีว่ามันเป็นการบ้านวิชา Operating System หนะครับ ให้ออกแบบการทดลอง และทำการทดลองอะไรบางอย่าง ก็เลยหยิบยกเรื่องนี้มาทำการทดลอง และเผยแพร่เพื่อเป็นการศึกษาแก่ผู้สนใจต่อไป เย้ ในที่สุดก็ได้ทำการปิดเทอมอย่างเป็นทางการซะที วันนี้ไปดู “ปิดเทอมใหญ่ หัวใจว้าวุ่น” มาแล้วด้วย หนังรัก 4 รูปแบบที่วัยรุ่นหน้าละอ่อนอย่างพวกเรา (แหวะ) ตั้งหน้าตั้งตาคอยมานาน เดี๋ยวรอให้คนไปดูกันมากหน่อย รอภาพฉากสวยๆ จะเอามาโพสมั่ง คิดถึงทุกๆ คนครับ
March 25th, 2008 at 10:30 pm
เนิร์ดนะ คราวนี้
ว่าแต่ idiotao นี่คือใครหว่า
March 26th, 2008 at 2:59 pm
impressive
February 16th, 2010 at 9:36 pm
ทำเป็นไทยไม่ได้หรอ งง มาก