1

I have two processes and one named pipe.

One process is actively reading from the pipe, and one process is sending data to the pipe continously.

The command that sends to the pipe is the following :

sudo iotop -b -P > diskfifo

The reader on the other hand is simply performing a loop read operation on the pipe

pipe=./diskfifo

while read line <$pipe
do
    if [[ "$line" == 'quit' ]]; then
        break
    fi
    echo $line
done

Problem : the iotop command sometimes stops after a certain number of iterations and I can't figure out why.

However, I noticed that if I reduce the amount of data sent the iotop command does not stop.

1 Answer 1

3

In each iteration of your loop, read will open the pipe for reading, read a line of data, and then close it.

If iotop decides to try to write to the pipe while read is not actively reading from it, it would receive a PIPE signal, as it tries to write to a pipe that nobody is reading from. The default action upon receiving the PIPE signal is for the process to terminate.

Instead of opening and closing the pipe for reading in the loop (and hoping that iotop won't write to the pipe while it's not being read from), redirect the input from the pipe to the loop itself:

pipe=./diskfifo

while read word; do
    [ "$word" = 'quit' ] && break
    printf '%s\n' "$word"
done <"$pipe"

This would keep the pipe open for the whole duration of the loop. Each invocation of read would inherit its standard input stream from the standard input of the loop, which is connected to the pipe.


A possible other formulation of the loop:

while read word && [ "$word" != 'quit' ]; do
    printf '%s\n' "$word"
done <"$pipe"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .