Alternative I/O Models 1361
described in Section 44.7.) In the other window, we run instances of cat(1) that
write data to these FIFOs.
Terminal window 1 Terminal window 2
$ mkfifo p q
$ ./epoll_input p q
$ cat > p
Opened "p" on fd 4
Type Control-Z to suspend cat
[1]+ Stopped cat >p
$ cat > q
Opened "q" on fd 5
About to epoll_wait()
Type Control-Z to suspend the epoll_input program
[1]+ Stopped ./epoll_input p q
Above, we suspended our monitoring program so that we can now generate input
on both FIFOs, and close the write end of one of them:
qqq
Type Control-D to terminate “cat > q”
$ fg %1
cat >p
ppp
Now we resume our monitoring program by bringing it into the foreground, at
which point epoll_wait() returns two events:
$ fg
./epoll_input p q
About to epoll_wait()
Ready: 2
fd=4; events: EPOLLIN
read 4 bytes: ppp
fd=5; events: EPOLLIN EPOLLHUP
read 4 bytes: qqq
closing fd 5
About to epoll_wait()
The two blank lines in the above output are the newlines that were read by the
instances of cat, written to the FIFOs, and then read and echoed by our monitoring
program.
Now we type Control-D in the second terminal window in order to terminate
the remaining instance of cat, which causes epoll_wait() to once more return, this
time with a single event:
Type Control-D to terminate “cat >p”
Ready: 1
fd=4; events: EPOLLHUP
closing fd 4
All file descriptors closed; bye