popen ...

der Mouse mouse at thunder.mcrcim.mcgill.edu
Sun Jun 23 02:21:29 AEST 1991


In article <1991Jun20.004507.17814 at menudo.uh.edu>, svec5 at menudo.uh.edu (T.C. Zhao) writes:

> I have read the popen man page many times, but still do not quite
> understand how it should behave. Consider the following code:

>        if(!(fp = popen("more","w"))
>          fp = stdout;
>        lots_of_output_using_fprintf(fp...)_fputs(..,fp)_with_error_check
>        if(fp!=stdout) pclose(fp);

> everything seems working ok except when more quits prematurely(q),
> the program exits.

Actually, it receives a SIGPIPE signal, which, if uncaught, kills it.
If you block or ignore SIGPIPE, the write() call made by stdio will
return EPIPE, which will cause stdio to return errors - which you
probably aren't checking for, which may or may not be the right thing.
Or you can catch SIGPIPE, instead of blocking or ignoring it, and deal
with it however you want.

(The SIGPIPE signal arises because your program writes on a pipe that's
no longer connected to anything, 'cause the process it used to be
connected to is gone.)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.unix.programmer mailing list