File handler
files are the means to communicate between processes files are shared between them. when a read is made in one of the processes it moves the file posiion handler.here the read in the child process moves the file position pointer ten positions. when the read is made from the parent process the file is read from the eleventh position hence different things are displayed in the screen.
// header files
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
//main func
int main(int argc, char *argv[])
{
int fp,pid;
char buff[11];
fp=open("baby",O_RDONLY);
pid = fork();
if(pid ==0)
{
printf("child begins\n");
read(fp,buff,10);
buff[10]='\0';
printf("child read\n");
puts(buff);
printf("the file handle is :%d\n",lseek(fp,0,1));
printf("child ends\n");
}
else
{
wait(0);
read(fp,buff,10);
buff[10]='\0';
printf("parent read\n");
puts(buff);
printf("the file handle is :%d\n",lseek(fp,0,1));
printf("parent exiting\n");
}
return 0;
}
Comments
Post a Comment