Take the general scenario of a parent creating a child process. Parent process declares a file pointer *fp.
then it points it to a file say 'new1' by
fp = fopen("new1","w+"). Opening it for both reading and writing.
Now the parent process creates a child process. While parent process waits for the child, child uses
execlp("./child_new",0); calling a c file child_new.
In child_new we have same File pointer declared. Now can we use that file pointer to make changes in same file which we did in parent??
Can processes share a file pointer?
I read somewhere that we can but I tried and it is showing some segmentation fault.
******Parent************************
Code:
#include<stdio.h>
#include<fcntl.h>
main()
{
FILE *fp;
fp = fopen("new1","w+");
if(fork()==0)
{
printf("\nIn child process\n");
execlp("./child_new",0);
exit(1);
}
else {
wait();
printf("In parent");
exit(1);
}
}
***********child****************
Code:
#include<stdio.h>
#include<fcntl.h>
main()
{
FILE *fp;
printf("In child file");
fseek(fp,0L,0);
}