How do I unbuffer the standard output of a child process?
Lemieux
lemieux at ireq.hydro.qc.ca
Wed Mar 20 05:46:35 AEST 1991
My program forks a process and redirect the stdin and
stdout of the child to a set of named pipes.
Thus, I can send a command to the child process through
one pipe and read the result from the other.
One of the program's constraint is that I have to get the
result immediately (I can't wait for the output buffer
of the child process to be flushed).
Unfortunately, I did not find a way to unbuffer the standard
output of the child process (even with setbuf). How can I do that?
Can someone help me, please?
Thanks in advance.
Here's what I tried (no validation is done for clarity!).
#include <stdio.h>
#include <sys/stat.h>
#define PIPE_IN "/tmp/pipe_in"
#define PIPE_OUT "/tmp/pipe_out"
main(argc,argv)
int argc;
char *argv[];
{
int pid;
int fd_in,fd_out;
FILE *fp,*fp_in,*fp_out;
/* create pipes */
mknod(PIPE_IN ,S_IFIFO | 0777,0);
mknod(PIPE_OUT,S_IFIFO | 0777,0);
/* create child process */
if ((pid = fork()) == -1)
{
fprintf(stderr,"Can't fork child\n");
exit(1);
}
/* server */
if (pid > 0)
{
/* establish the communication medium */
fd_in = open(PIPE_IN ,O_WRONLY);
fd_out = open(PIPE_OUT,O_RDONLY);
fp_in = fdopen(fd_in ,"w");
fp_out = fdopen(fd_out,"r");
/* unbuffer the streams */
setbuf(fp_in,NULL);
setbuf(fp_out,NULL);
/* lots of stuff deleted for clarity */
...
}
/* client */
else
{
/* redirect stdin and stdout */
freopen(PIPE_IN,"r",stdin);
fp = freopen(PIPE_OUT,"w",stdout);
/*
* THIS COMMAND IS SUPPOSE TO UNBUFFER
* THE CHILD OUTPUT
*/
setbuf(fp,NULL);
/* execute the client program */
execl(argv[1],argv[2],0);
}
}
-----------------------------------------------------------------------------
Eric LEMIEUX | Internet: lemieux at ireq.hydro.qc.ca
Institut de Recherche d'Hydro-Quebec |
1800 Montee Sainte-Julie | TEL: (514) 652-8139
Varennes, Quebec, Canada | FAX: (514) 652-8309
-----------------------------------------------------------------------------
More information about the Comp.unix.questions
mailing list