C Program to show an example of Fork System Call


 

C Program to show an example of Fork System Call
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main(void)
{
        pid_t pid=fork();
        if(pid==0)
        {
                int j;
                for(j=0;j<10;j++)
                {
                        printf("Child:%d\n",j);
                        sleep(1);
                }
                exit(0);
        }
        else if(pid>0)
        {
                int i;
                for(i=0;i<10;i++)
                {
                        printf("Parent:%d\n",i);
                        sleep(1);
                }
        }
        else
        {
                printf("Couldn't fork");
                exit(1);
        }
}

Post a Comment

Previous Post Next Post