Monday 7 October 2013

Creation of a child process using fork system call

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

int main()
{
    int p[2];
    pid_t pid;

    char inbuf[10],outbuf[10];

     if(pipe(p)==-1)
    {
               printf("pipe failed\n");
               return 1;
     }

    else
         printf(" pipe created\n");

if((pid=fork()))
{
    printf("In parent process\n");
    printf("type the data to be sent to child");
    scanf("%s",outbuf);
     write (p[1],outbuf,10);
      sleep(2);
      printf("after sleep in parent process\n");
}

else  
{
       printf("In child process\n");
       read(p[0],inbuf,10);
       printf("the data received by the child is %s\n",inbuf);
       sleep(2);
       printf("After sleep in child\n");
}
return 0;
}

Tags : OS LAB Program , Os programs , Creation of a child process using fork system call , OS fork program , fork system call , use of fork system call , fork() , working of fork() , fork() system call.