I'm trying to run a C code with matlab, developed by others. In c, it's written to stdout, then in matlab a pipe is created and read:

unix('rm -f /tmp/matdata.in;mkfifo /tmp/matdata.in');
unix(['./',file,' > /tmp/matdata.in &']);

Now, I want the data directed to the pipe also be written into a file for later analysis. I know ''tee'' is good for this, but while I try both of below commands, the pipe and the file are always size 0

unix(['./',file,' > /tmp/matdata.in | tee /tmp/data &']);
unix(['./',file,' | tee /tmp/matdata.in /tmp/data &']);
  • Your second attempt should write to the FIFO (/tmp/matdata.in), the regular file (/tmp/data) and also to standard output. How can you tell that it has "size 0" (it's a FIFO, so I'd expect eg. ls to report 0)? What happens if you attempt to read from it (with a fopen, like in the original code)? – JigglyNaga Oct 11 '18 at 14:32
  • Yes, I meant that ''ls'' report 0. Sorry, I did not know one'd expect 0 for FIFO. But how about the regular file? Is it surprising to you that ''ls'' also report 0 for a regular file? Thanks a lot! – Bowen Zhao Oct 11 '18 at 18:41
  • tee attempts to write the same data to all destination files, as well as stdout, simultaneously, so if the pipe isn't being read, nothing will go to the file either. Are you reading the pipe after running tee, like in the original code? – JigglyNaga Oct 12 '18 at 1:25
  • Yes. The code is reading the pipe with fopen and run properly. But the file size is 0 even after the run. Thanks! – Bowen Zhao Oct 12 '18 at 14:51

The problem could be that you are directing stdout to a file with > /tmp/matdata.in, which leaves tee with no output to write. Reorganizing the command as ' | tee /tmp/data /tmp/matdata.in &' could have fixed the issue, and I can use that on my computer to write to two files, but I'm not entirely certain as your bottom command didn't work. Maybe it's something with matlab. You should try it with the pipe at the end like in my command. Alternatively, you could try 'stdbuf -i0 -o0 -e0 ./',file,' | tee /tmp/data /tmp/matdata.in &' (or './',file,' | stdbuf -i0 -o0 -e0 tee /tmp/data /tmp/matdata.in &') to remove buffering that might limit the rate at which you get your data, if the writer of the c code didn't do a good job at flushing the output ever so often

  • Thanks! I tried stdbuf but that does not change the result. When I try to read the pipe with fopen after the run, it seems to hang? – Bowen Zhao Oct 12 '18 at 15:22
  • Could it be that the C code is not working properly? – jkan5855 Oct 13 '18 at 9:46

When writing to stdout using stdio, the output to files and pipes is block buffered.

You need to wait until 4k or 8k (depending on your local stdio) has been received from stdin and written to stdout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.