Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!thunder.mcrcim.mcgill.edu!bonnie.concordia.ca!s3!lemieux From: lemieux@ireq.hydro.qc.ca (Lemieux) Newsgroups: comp.unix.questions Subject: How do I unbuffer the standard output of a child process? Keywords: fork, pipe, redirection, buffering Message-ID: <6119@s3.ireq.hydro.qc.ca> Date: 19 Mar 91 19:46:35 GMT Sender: root@s3.ireq.hydro.qc.ca Organization: Institut de Recherche d'Hydro-Quebec Lines: 84 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 #include #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@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 -----------------------------------------------------------------------------