I am trying to write a script to initiate a daemon which while pipe its output and input via two named pipes $FIFO.out
and $FIFO.in
respectively. What I wish to do is to give the client process the ability to talk with the daemon whenever it wants by using the named pipes.
Here is what I have already implemented: (The daemon is a qalc process)
#!/bin/sh
# Daemon script
FIFO="/tmp/qcalc"
mkfifo "$FIFO.out" "$FIFO.in"
exec qalc -t -u8 <"$FIFO.in" >"$FIFO.out"
#!/bin/sh
# Client script
FIFO="/tmp/qcalc"
talk() {
echo "$1" >"$FIFO.in"
# I'm not interested in the first two lines
read -r _ <"$FIFO.out"
read -r _ <"$FIFO.out"
read -r result <"$FIFO.out"
echo "$result"
}
talk 1
talk 2
The problem with this implementation is that I can only talk to the daemon once, and after that the client just freezes at the blocking read. Here is how it looks in the shell:
$ ./daemon &
[2] 57848
$ ./client
1
[2] + done ./daemon
^C./client: 13: cannot open /tmp/qcalc.out: Interrupted system call