1

I have a script that runs my application (c++) with STDIN redirected from a fifo and redirects STDOUT to a file. The following code runs just fine on ubuntu 18:

./my_app 0</home/user/pipe 1> log.txt &

I'm sending instructions to the fifo with printf: printf "ins\n" > /home/user/pipe

In the app reading is done std::getline in a try-catch block which works just fine on ubuntu 18, but on ubuntu 20 eofbit and failbit are always set after the first instruction is read.

The whole script looks like:

#!/bin/bash

./my_app 0</home/user/pipe 1> log.txt & sleep 1 printf "ins1\n" > /home/user/pipe sleep 5 printf "ins2\n" > /home/user/pipe . .

For the example above, i am able to read 'ins1' but nothing after

Later edit: Working now, with something to keep the pipe open. sleep infinity > /home/user/pipe & before starting the app and killing the sleep at the end

dma
  • 11

1 Answers1

1

I doubt that the same program application and script will work on a different version of Ubuntu (or Linux or UNIX in general).

The manual page for fifo refers to the manual page for pipe for the semantics of I/O.

If all file descriptors referring to the write end of a pipe have been closed, then an attempt to read(2) from the pipe will see end-of-file (read(2) will return 0). If all file descriptors referring to the read end of a pipe have been closed, then a write(2) will cause a SIGPIPE signal to be generated for the calling process. If the calling process is ignoring this signal, then write(2) fails with the error EPIPE.

I expect that commands like

printf "ins1\n" > /home/user/pipe

will open the pipe, write the data and close the pipe, so the receiving program should see an end-of-file condition.

The only way to prevent this is to keep the pipe open for writing. This is what you achieve with the sleep command shown in the question.

Bodo
  • 591