Every running instance of a program is known as a process. The concept of processes is fundamental to the UNIX / Linux operating systems. A process has its own identity in form of a PID or a process ID. This PID for each process is unique across the whole operating system. Also, each process has its own process address space where memory segments like code segment, data segment, stack segment etc are placed. The concept of process is very vast and can be broadly classified into process creation, process execution and process termination.
Linux Processes Series: part 1, part 2, part 3, part 4 (this article).
In this article we will concentrate on the process creation aspect from programming point of view. We will focus on the fork() function and understand how it works.
The fork() Function
The fork() function is used to create a new process by duplicating the existing process from which it is called. The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it.
- The child has a unique PID like any other process running in the operating system.
- The child has a parent process ID which is same as the PID of the process that created it.
- Resource utilization and CPU time counters are reset to zero in child process.
- Set of pending signals in child is empty.
- Child does not inherit any timers from its parent
Note that the above list is not exhaustive. There are a whole lot of points mentioned in the man page of fork(). I’d strongly recommend readers of this article to go through those points in the man page of fork() function.
The Return Type
Fork() has an interesting behavior while returning to the calling method. If the fork() function is successful then it returns twice. Once it returns in the child process with return value ‘0’ and then it returns in the parent process with child’s PID as return value. This behavior is because of the fact that once the fork is called, child process is created and since the child process shares the text segment with parent process and continues execution from the next statement in the same text segment so fork returns twice (once in parent and once in child).
C Fork Example
Lets take an example to illustrate the use of fork function.
#include <unistd.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <sys/wait.h> #include <stdlib.h> int var_glb; /* A global variable*/ int main(void) { pid_t childPID; int var_lcl = 0; childPID = fork(); if(childPID >= 0) // fork was successful { if(childPID == 0) // child process { var_lcl++; var_glb++; printf("\n Child Process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb); } else //Parent process { var_lcl = 10; var_glb = 20; printf("\n Parent process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb); } } else // fork failed { printf("\n Fork failed, quitting!!!!!!\n"); return 1; } return 0; }
In the code above :
- A local and a global variable (var_lcl and var_glb ) declared and initialized with value ‘0’
- Next the fork() function is called and its return value is saved in variable childPID.
- Now, the value of childPID is checked to make sure that the fork() function passed.
- Next on the basis of the value of childPID, the code for parent and child is executed.
- One thing to note here is why the variables var_lcl and var_glb are used?
- They are used to show that both child and parent process work on separate copies of these variables.
- Separate values of these variables are used to show the above stated fact.
- In linux, a copy-on-write mechanism is used where-in both the child and the parent keep working on the same copy of variable until one of them tries to change its value.
- After fork, whether the child will run first or parent depends upon the scheduler.
Now, when the above code is compiled and run :
$ ./fork Parent process :: var_lcl = [10], var_glb[20] Child Process :: var_lcl = [1], var_glb[1]
We see that in the output above, both the child and parent process got executed and logs show separate values of var_lcl and var_glb. This concludes that both parent and child had their own copy of var_lcl and var_glb.
NOTE: A function getpid() can be used to fetch the process ID of the calling process and the function getppid() can be used to fetch the PID of the parent process.
Comments on this entry are closed.
Hi,
Thanks a lot..
Very nice and practical article….
I keep following you! This blog is a must for CS students and enthusiasts!
I like your articles very much and a great FAN of TGS.
very nice one
Good article thanks for the post!
First and foremost i like this blog!!, However, I have two questions about the child parent topic.
int main(void) {
int i;
fork();
fork();
fork();
printf(“process…\n”);
return 0;
}
i have such a program. whenever i run it, i get 8 process… es. why? how many children are here. and how many parents do we have? can i have them as a tree diagram?
Thanks in advance
You have 8 processes because fork creates an additional process for the parent and child. The program starts with 1. You call fork() now you have 2. Call fork() again and you have 4. Call fork() a third time and you have 8. Here’s the best diagram i can do here on this text editor. The children are below the parent on each level of the hierarchy:
1
2 4 5
3 6 7
8
As you can see the left side will always be bigger because the 2nd process was created there first.
okay it didn’t keep my spaces and the formatting of my tree did not show up, but just step through it and i’m sure you’ll be able to figure it out.
perfect, I never understand fork() until read you explain, function return twice is so new for me.
Thanks alot
Hello,
Nice article.
I have one doubt about what I did here in my code:
if(childPID == 0) // child process
{
var_lcl++;
var_glb++;
printf(“\n Child Process :: var_lcl = [%d], var_glb[%d]\n”, var_lcl, var_glb);
printf(“child\n”);
int pid = getpid();
int parent_pid = getppid();
printf(“My real pid is %d\n”, pid);
printf(“My parent pid is %d\n”, parent_pid);
}
else //Parent process
{
var_lcl = 10;
var_glb = 20;
printf(“\n Parent process :: var_lcl = [%d], var_glb[%d]\n”, var_lcl, var_glb);
printf(“parent\n”);
printf(“o childPID eh: %d, “,childPID);
int pid = getpid();
int parent_pid = getppid();
printf(“My real pid is %d\n”, pid);
printf(“My parent pid is %d\n\n\n\n”, parent_pid);
}
My output started with:
Parent process :: var_lcl = …….
Child Process :: var_lcl = …….
My expected output:
Parent process :: var_lcl = …….
AOA
thanks a lot . I really clarify my concept after reading it
Hi ..
Can you tel me how many processes will be created for below program and explanation will be appreciated.
main()
{
for(i=1;i<n;i++)
fork();
}
@Danish
I am not a Linux programer, neither havent tested yet the code that you proposed, but it kinda gives me the impression that the code is going to create an infinite set of processes (I am just taking a guess here). The cause of my assumption is because supposing fork initilized all identifiers, then, the identifier “i” in the “for” loop would be initialized to 1 each time a new process is created, thus, creating either a inifinite set of processes or making the system to throw an out of memory exception (which would be a nice feature in such case). But I am just guessing since I havent tested the code and because it seems kinda dangerous.
still now i am not able to understand that how many childs are created when we call fork()
main()
{
printf(“the process id is %d”,&getpid());
fork();
printf(“the child id is %d”,getpid());
}
i want to know whats d output???????explain it…….
plz explain me this answer
for(i=0;i<4;i++)
{
fork();
}
num of child processes created
plz let me know answer
reply to your answer
when fork function executes it creates the copy of all process(to understand the concept you may think that it create the copy of process which are below the calling function) and continue its execution from the process which are copied.
below here is the working
when 1st fork execute it creates copy of 2 fork function and 1 printf function(say we name it as page 1st) and control will transfer to page 1st
then when 2nd fork executes from page 1st, it creates copies of 1fork function and 1 printf function (say name it as page 2nd) and control will transfer to page 2nd.
when 3rd fork function executes from page 2nd,it creates copies of 1 printf function and create a page 3rd and control will transfer to page 3rd.
So from here(i.e page 3rd) it executes 1st printf function and return it control to page 2nd.In page 2,3rd fork function executes then it executes 2nd printf function and control will transfer to page 1st.
In page 1st 2nd fork executes completely,then it comes to 3rd fork function (i.e it create 4th page) and executes 3rd printf function and comes to page 1st and executes 4th printf function and then return to main
then 2nd fork executes…..same proces is done
it create 5th page which contain 1 fork and 1 printf function and control come to 1st fork function of page 5 and executes it then it create 6 th page from which it executes printf function( which is 5th) and return to page 5th and executes printf function(which is in 6th number) and control will return to main
Then executes 3rd fork function which create 7th page from which it have 1 printf function and executes it (which is of 7th) and return to main
then it printf last printf function which is of 8th in number…….
i like this, thank you
int main()
{
fork();
for();
fork();
printf(“hello world……”);
while(1);
return 0;
}
pls can any one tell what is the output for this??
A total of how many processes would be created in the function fork test () given below, excluding the main process that called fork test()? How many times will “Whatsup” be printed. Briefly explain your answer.
void fork_test( )
{
if (fork() != 0)
{
if (fork() != 0)
fork();
else
printf(“Hello 1\n”);
}
else
{
if (fork() != 0)
{
printf(“Hello\n”);
}
else
{
fork();
}
}
printf(“Whatsup\n”);
}
A total of how many processes would be created in the function fork test () given below, excluding the main process that called fork test()? How many times will “Whatsup” be printed. Briefly explain your answer.
void fork_test( )
{
if (fork() != 0)
{
if (fork() != 0)
fork();
else
printf(“Hello 1\n”);
}
else
{
if (fork() != 0)
{
printf(“Hello\n”);
}
else
{
fork();
}
}
printf(“Whatsup\n”);
}
Thanks for these information…helpful for answering viva questions.
Hi,
You have written:
“After fork, whether the child will run first or parent depends upon the scheduler.”
So does this mean that the parent and child processes are not being executed parallely?
Is there any way to run two processes parallel? I have tried multithreading, but since my debian-based linux platform has a single core processor, it seems to be splitting the time and switching between threads. That is, say thread1 is executed a couple of times and then thread2 a couple of times and again thread1. It seems to be executing in this manner.
Disclaimer: The two threads each has an infinite loop to run.