Plk
Plk

Reputation: 57

How to know when to exit program when using multiple pipes?

Let's say we have 3 pipes, 3 children that send data through them, 1 parent that reads this data (eg. printing some sent string). All the children are the same: they send data to pipe, let's say 4 times, and then they close their side of pipe and exit the program.

How can you know (in parent) that all the pipes have been closed?

  1. When we're simply iterating through pipes, with some sleep time, and checking if there's some data to be read.
  2. When we're using poll to read data (multiplexing).

(I'm not sure, but I think in the first case my program was returning with some pipe error, after all the write sides of pipes have been closed?)

I guess we could iterate through the write side of pipes and if all of them are -1?? (not sure to what value file descriptor is set when we close it, or if it's set at all, if not maybe we could set it by ourselves in child) then we can exit parent (data reading) program?

Upvotes: 0

Views: 32

Answers (0)

Related Questions