background process in java listening to stdin background process in java listening to stdin shell shell

background process in java listening to stdin


Well, in fact the fifo files seems not behave exactly as you imagine.

The read() command is not blocking on a pipe. You should loop until you get data:

    try {        bis = new BufferedInputStream(System.in);        while (true) {            byte[] buffer = new byte[4096];            if (bis.available() > 0) {                bis.read(buffer, 0, bis.available());                // do some clever thing            } else {                try {                    Thread.sleep(50);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    } catch (IOException e) {        // it failed...    }

This will try to read and if there is nothing, then it will wait for 50 ms.

M.


Since you have no data in the pipe yet, read() sees the end of the stream and because of this it returns -1. Call available() before reading from the stream to be sure that there is information in it.


If you use set -m at the top of your shell script (hence enabling forcefully job control), you should see a consistent behavior.