All about programming in GNU/LINUX

Race conditions in fork system call , difference b/w fork and vfork system calls in GNU/LINUX

We say that the RACE conditions conditions exist in a program if the sequence in which the output is produced is unpredictable because of the order in which the programs are executed . After forking(executing fork system call), if the child and parent process have sequence of instructions to perform , the execution control will be toggling b/w Child and parent due the internal scheduling algorithm of the kernel , and the order of execution of the instruction is unpredictable .The following code illustrates this concept .

Image

#include<unistd.h>
#include<stdlib.h>
int main()
{
    int i,j;
    i=fork();
    if(i==0)//child process
    {
        for(j=1;j<=10;j++)
         printf(“\nChild here\n”);
        exit(0);
    }
    else //parent process
    {
        for(j=1;j<=10;j++)
         printf(“\nParent here\n”);

    }
   exit(0);
}
As you can clearly see the output is intermixed because of the switch in control of execution b/w child and the parent process .This is unavoidable .The possible solutions be either to make the parent sleep this the child completes execution or use vfork() system call

vfork() works similar to fork(), but vfork() doesnt begin the execution the of the parent process till either the child execs(executes any of the exec() family of calls) or the child exits.Unlike fork() the child process from vfork() doesnt copy the address space of the parent .It is usually used when the child process is created with a purpose of executing the exec() system call.Since vfork() system call doesnt start the execution of parent process and waits till the child execs or exits ,thus race condition is avoided.The sample code below illustrates this concept.

Image

 

#include<unistd.h>
#include<stdlib.h>
int main()
{
    int i,j;
    i=vfork();
    if(i==0)//child process
    {
        for(j=1;j<=10;j++)
         printf(“\nChild here”);
        exit(0);
    }
    else //parent process
    {
        for(j=1;j<=10;j++)
         printf(“\nParent here”);

    }
   exit(0);
}
This program is similar to the earlier one ,but instead of fork(), vfork() is used .You can clearly see that the output is not intermixed as in case of fork() , since vfork() doesnt start the execution of the parent till the child finishes executing the instructions it execs or exits.

 

Advertisements

4 responses

  1. thank you.. this cleared the bug in my mind..excellent example .props for putting up the screenshots it really helped me…thanks again:)

    October 17, 2013 at 10:37 pm

    • I’m glad this post of mine helped you . Feels good being able to contribute back to the community from where i gained all my knowledge 🙂

      October 22, 2013 at 2:14 am

  2. in this blog i found nice programs to understand those concepts
    Nice blog ,,Keep it up !

    August 22, 2014 at 8:31 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s