fork System call and Threads

Content: 

Content covered: 

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

Responses

thank you

 

great video Sir

 

prasanta's picture

Great....

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

main(){
  p = fork();
  if(p > 0){
    fork();
    printf("techtud");
  }
  printf("techtud");
}

 


Responses

More Comments
shiveshroy's picture

Thanks a lot pritam Prasun sir for such an awesome video, It basically cleared all the co-concepts related to fork() system call. I really appriciate your effort.

for the first fork() call i m getting the below warning, can you pls explain why this warning is coming.

"fork.c:33:4: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]"

sumitverma's picture

Working fine. Check:
https://ideone.com/Tx6Ozj

please add this line to your code. #include  <sys/types.h>

sumitverma's picture

Thanks for your suggestion. But it is just a part of code to explain the fork concept, So main function is sufficient.

Consider the following pseudo-code

main()
{
    int t1 = 0, t2 = 0, t3 = 0;
    t1 = fork();
    t2 = fork();
    if (t1 != 0) {
        t3 = fork();
        print("0");
    }
}

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

  • Explanation

    If you are not familiar how the fork() system call works, please refer to this link : http://www.techtud.com/resource-share/fork-system-call-very-good-notes-e...

    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.

    Children:

    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 C2.here we count 2 processes C2 and C21.

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

Responses

rtiwari95's picture

6 processes will be created here.

nice one 

 

ajithsupramani's picture

nice

Why are we counting the parent process it is already given to us?

 

Consider the following piece of code
main()
{
int i,n;
for(i=1;i<=n;++i)
   fork();
}
How many times child process will created?

  • Explanation

    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:
1.
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.

Faster

Slower

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

Awesome, you did good to complete todays course.

To continue your preparation while we publish tomorrow’s topic,

please subscribe to our youtube channel.

 

Responses

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

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

karmjitjoshi's picture

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?? 

sumitverma's picture

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.

amit17's picture

sir please increase the speed of uploading the lectures. thank you.

karmjitjoshi's picture

When the next video will be uploaded?

lovelyverma's picture

yes plz tell sir?

Can anyone explain question 5 of quiz.

 

chetnawadhwa's picture

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

rahulkumarsh's picture

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

Enter your search keyword:

Search form

Wait!

Here is a chance to join biggest community of technical Students,
Tutors with FREE learning resources and so much more.
It takes less then 60 seconds.