Why does UNIX command not respect the sigpipe signal

1 view (last 30 days)
Hello everyone,
In my work I often need to run some unix programs and process the output in MATLAB, which works for me.
now in the shell I can:
MyUnixProgram | head -n 20
which runs fine in shell, meaning, it returns 20 line of output after which MyUnixProgram terminates on SIGPIPE signal. (result of the head command completed and closed the pipe)
Strangely, this is not true when you do it with MATLABs unix command
[a,b] = unix('MyUnixProgram | head -n 20');
will run a long time. After some digging I've found that for some reason the MATLAB UNIX command is forcing MyUnixProgram to ignore the SIGPIPE signal. And it does so, even if it is wrapped inside the bash script.
I cant even use evalc since it also waits for completion.
Do somebody have same experience?
(To reproduce create a shell script with following
#!/bin/bash
for i in {1..20} ; do
sleep 1
echo $i
done
Now try to run ./foo.sh | head -n 2 in bash and in matlab Please note, that that short program does no trapping of the broken pipe and acctual program leaves even the MATLAB output only with the output of the head. But it takes the first command to finish.
So in shell it finishes in 2 seconds and it takes 20 in MATLAB unix call

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!