0

I have a C program to experiment with signals (./signal). It processes the standard input, exits when reading a 'q' and reacts to signals.

I would like to test it with a bash script using a named pipe (fifo). Currently I have this:

#!/bin/bash

rm mypipe
mkfifo mypipe

./signal <> mypipe &

echo f > mypipe
echo hello
echo f > mypipe
echo q > mypipe

rm mypipe

If I run this, then I can see my program's expected output, however, echo hello does not appear as it should between the program's two responses to the 'f' inputs.

Update

Sorry, I pasted an old version, the ampersand is here: ./signal <> mypipe &, I corrected the code above.

Update 2

I've added wait, -f and exit 0 as suggested by roaima but nothing changed. And also I tried it with ./signal < mypipe & but I get the same.

3
  • 1
    why the & at the end of mkfifo? That returns pretty much immediately. Your ./signal on the other hand does not. Commented Dec 9, 2021 at 11:20
  • There's no reason why echo hello "should" appear between two outputs of another process which runs independently from the current script. It can appear before, in between or after both of them, without any reliable way to determine it.
    – user313992
    Commented Dec 9, 2021 at 13:35
  • @UncleBilly, OK, so let's put a reason for this into the script.
    – z32a7ul
    Commented Dec 9, 2021 at 14:06

1 Answer 1

2

The & tells the shell to run the command in the background. Your script runs mkfifo mypipe in the background to create the pipe device, which seems somewhat pointless to me, but does not run ./signal <> mypipe in the background. Unless this C program forks and allows the parent to return immediately, it will block the script from continuing and the subsequent echo commands will not be reached until signal has exited

#!/bin/bash

# Create the pipe
rm -f mypipe
mkfifo mypipe

# Run the application in the background
./signal <> mypipe &

# Apply tests
echo f > mypipe
echo hello
echo f > mypipe
echo q > mypipe

# Wait for the application to terminate (a no-op if it's already gone)
wait

# Tidy up
rm -f mypipe
exit 0

Note that your program (and this modified one) never reads from the FIFO, so if your signal application writes to the pipe it will likely block.

If you find your test harness isn't working as expected, enable debugging

bash -x testscript    # run the script

I expect you'll find that one of the writes to the FIFO is blocking, which means your application isn't taking from the pipe

You must log in to answer this question.

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