#include<stdio.h>
FILE * fp;
main()
{
pid_t pid;
fp=fopen("..........");
// now i am transfering the control to a new process(p2.c).
pid=fork();
if(pid==0)
.....
execl(" p2".....);
....
}
//p2.c
extern FILE *fp;
main()
{
//here i want to write to a file using the file pointer which i declared
// in p1.c
fprintf(fp,"%s",.....);
.....
.....
}
how can i write this...
(or)
Actually , i want a file to be opened in a process\(probably in an infinite
loop),and using that file pointer i want to perform another task in a diffrent
process i.e acess that file pointer in a different process and do a job.
First off: execl will clobber EVERYTHING including the open file descriptors from the parent process.
Maybe if you explained exactly what you are trying to do - not in terms of code - we could tell you how to do it. Even if you could pass descriptors down to other programs using execl() -- which you cannot -- I do not see what you gain.
Open file descriptors should, by default, remain open. They will be closed only if the FD_CLOEXEC flag was set, which is generally rare. Having file descriptors open across an exec() is how the shell implements stuff like:
program < inputfile > outputfile
i know this sounds kinda layman-ish...
u can write fp to file and read from it in p2. u can decide the name of the file before executing the progs, like "test" or something...