techtud's picture

fork System call and Threads

Understanding fork() system call
Content covered: 

What id fork(), How it works, An example

More Less

How many times the following code will print "techtud" ?

  p = fork();
  if(p > 0){


Illustration of Fork() system call

Consider the following pseudo-code

    int t1 = 0, t2 = 0, t3 = 0;
    t1 = fork();
    t2 = fork();
    if (t1 != 0) {
        t3 = fork();

What is the total number of processes created by execution of this above program?

If you are not familiar how the fork() system call works, please refer to this link :

Lets clarify each thing carefully. I will be enumerating fork calls as 1st ,2nd and 3rd in order. 1st I will tell u execution of parent only.

Parents: When the parent excutes 1st fork, parent's t1 will receive PID1 which is non zero, whereas Child C1's t1 will be having 0 and all children of C1 will use t1=0.

When parent executes 2nd fork,parent's t2=PID2, child C2 will be created and it will have value of t2=0.parent's t1 will be PID1 which will be passed to C2.

Now parent's t1 =PID1 which is non zero ,so it will execute 3rd fork and create process C3. t3 of parent will be PID3 and non zeros,where as C3's t3 will be Zero.


Now turning to children's fork execution part,C1 will execute fork and create process C11. C1 's t2 will be PID11 and non zero whereas C11's t2 will be zero.

It is worth remembering that C1 and C11 will be using value of t1 of C1 ,which is zero, so neither of them will be able to execute 3rd fork which is inside "if" clause. they will not call neither fork nor print and they terminate. Here we count 2 processes. C1 and C11 C2 is having t1=PID1 which is non zero, whereas its t2=0.

Now C2 will be able to execute 3rd fork and create process C21. C2's t3 = PID21 non zero,whereas C21's t3 =0. Now C21 will call print function and terminate. same with we count 2 processes C2 and C21.

Total process , Parent, C1,C2,C3, C11,C21 = 6.

Fork System call

Consider the following piece of code
int i,n;
How many times child process will created?

For n=1, 1 child process.
For n=2, 3 child processes.
For n, total 2n-1 child process will be created.


Thread: Thread is a lightweight process which provides a way to improve application performance through parallelism. It provides concurrency within a process.Thread is an execution unit which consists of its own program counter, a stack, and a set of registers.
Types of Thread:
User Level Thread
2. Kernel Level Thread

User Level Thread

Kernel Level Thread

Implemented by thread library in user mode.

Implemented by O.S.  in Kernel mode.



Generic and can run on any O.S.

Kernel- level thread is specific to the O.S.

Multithreading Models:
In a specific implementation, the user threads must be mapped to kernel threads, using one of the following strategies:
1. Many to one relationship // many user-level threads are all mapped onto a single kernel thread.
2. One to one relationship // a separate kernel thread to handle each user thread.
3. Many to many relationship // multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models.


  • This quiz contains 5 questions on the topic Fork and Threads
  • Lean well before you attempt the quiz
  • You can attempt the quiz unlimited number of times.

Difficulty Level:  basic
No. of Questions:  5
Gourav Jain gouravjain 11 Jan 2017 12:38 am

Please, explain the 4th question of the quiz??

Rahul rahul55523 11 Jan 2017 01:06 pm

how the address of parent and child process are same in question 4th

karmjit joshi karmjitjoshi 11 Jan 2017 04:21 pm

Consider the following code fragment:

if (fork() == 0)

{ a = a + 5; printf("%d,%d\n", a, &a); }

else { a = a –5; printf("%d, %d\n", a, &a); } 

Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?


u = x + 10 and v = y


u = x + 10 and v != y


u + 10 = x and v = y


u + 10 = x and v != y

Please explain this?? 

Sumit Verma sumitverma 11 Jan 2017 05:51 pm

Let's go line by line:

1.if (fork() == 0)

2. { a = a + 5; printf("%d,%d\n", a, &a); }

3. else { a = a –5; printf("%d, %d\n", a, &a); } 

Process P will execute line1. fork() will cause creation of a new child process say C1
Now Both P and C1 will start execution from the next instruction in line 1 which is the comparision of the returned value of fork() with zero.
For parent process P, this comparision is false as value returned by fork will be greater than 0(Process id of child process). So P will execute the else block.
For child process C1, Condition fork()==0 is true. So Cwill execute the if block.
Now u, v be the values printed by the parent process, and x, y be the values printed by the child process.
u= a-5, v=address of a (in parent process)
x= a+5, y=address of a (in child process)
Note one thing here: In virtual memory environment, all the processes start execution from the virtual address zero and hence both the process will have same logical address of the variable 'a' (physical address will be different).
Hence u + 10 = x and v = y is the correct option.

AMIT CHAUDHARY amit17 11 Jan 2017 07:05 pm
sir please increase the speed of uploading the lectures. thank you.
karmjit joshi karmjitjoshi 12 Jan 2017 12:59 pm

When the next video will be uploaded?

Rani Raja lovelyverma 12 Jan 2017 01:12 pm

yes plz tell sir?

Amit Jindal amitj 13 Jan 2017 12:40 am

Can anyone explain question 5 of quiz.


Chetna Wadhwa chetnawadhwa 14 Jan 2017 02:44 am

what about other days..after this..? haven't they been uploaded or are not accessible to me tell sum1

rahul sharma rahulkumarsh 14 Jan 2017 07:49 pm

Sir please upload next lectures and assignments . thanks a lot for the previous ones.