Processes hang on read Processes hang on read unix unix

Processes hang on read


The call to read() is blocking until more data shows up. From the man page for pipe

If a process attempts to read from an empty pipe, then read(2) will block until data is available. If a process attempts to write to a full pipe (see below), then write(2) blocks until sufficient data has been read from the pipe to allow the write to complete. Nonblocking I/O is possible by using the fcntl(2) F_SETFL operation to enable the O_NONBLOCK open file status flag.

After you open each file descriptor before you enter that while loop do this to each one:

fcntl(fd, F_SETFL, O_NONBLOCK);

However, you really should read up on blocking vs. non-blocking I/O, including reading the man pages for pipe, read, fcntl, etc.