tags:

views:

94

answers:

2

Let's say I spawn a process PO through popen (READ ONLY) from a process PA. I then pclose() the pipe on PA's side.

On PO's side, how do I determine if stdout is still available without executing a write() ?

Note that I have tried catching SIGPIPE on PO's side to no avail.

UPDATED: I tried using fstat(1, &buf) without success.

UPDATED: The reason I need to detect this condition through PO I do not have access to PO's PID from PA (and hence can't kill it). Futhermore, I'd like for PO to be more robust in face failures of PA i.e. exiting by itself.

RESOLUTION: I went ahead and used socketpair, fork. Trying to control a process through popen turned out to be a nightmare (to me at least). A big thanks to everyone who contributed!

+2  A: 

Mmm... pclose() is supposed to wait for PO to finish before closing the pipe. In the meantime, PO can keep writing to its end of the pipe at least up to 4192 bytes (ulimit -p times 512), then it should simply block.

Perhaps you will have to switch for pipe()/fork()/dup2()/close() if you want more control. If this is what you want, let me know and I'll post some code.

Gonzalo
+1  A: 

PA is the consumer of information (Hence it does popen() , and pclose() ).

PO is the provider, and hence the server - in this case it only knows that it is writing to stdout, but cannot tell what stdout is bound to. So in this case PO is not supposed to know too much about stdout.

The EOF detection should happen at the PA program.

Could you post a few more details about why you need to do it in PO?

blispr