17

I am using a named pipe to capture the output of an external program (wgrib2) within another program (MATLAB). The MATLAB code is below, and system() accesses the command line to make the pipe:

system('mkfifo myfifo');     % Make a named pipe myfifo
% Call the external program wgrib2 and dump its output to the named pipe myfifo
system('wgrib2.exe multi_1.glo_30m.hs.201212.grb2 -ij 1 165 -ij 1 166 > myfifo &');
fid = fopen('myfifo', 'r');  % Open the named pipe
a = fscanf(fid, '%c');       % Read the output as character
fclose(fid);                 % Close the "file" (myfifo still exists afterward)

Here are my questions:

  1. Do I have to close the named pipe myfifo after I use it? It seems to persist after the code is run.
  2. If myfifo needs to be closed, what is the command to close it?
  3. I will be running the code sample above many times (>1000), so is it OK if I reuse the named pipe and do not close it until the end?
EvgenKo423
  • 2,256
  • 2
  • 16
  • 23
KAE
  • 815
  • 1
  • 13
  • 32

4 Answers4

11
  1. No. Unix treats everything like a file. Named pipes are not different. If you’re done using it, you probably want to close it so you don’t clutter up your machine with named pipes, but you don’t need to close it.

Edited to reflect below comment, which is correct. Deleting != closing.

  1. You close the named pipe the same way you close any file:
    fclose(mFifo)

As mentioned in the accepted answer, closing will not delete the fifo. You may need to do that separately.

  1. There’s nothing wrong with re-using a named pipe. It’s up to you, however, to know when you’re done reading/writing to it for each iteration. Once all the data has been read out of the pipe, you’re free to use it again as many times as you want.
  • 4
    This answer is wrong. 1: Programs indeed do *need* to close pipes. Even workstations can run out of open files if bad programs don't close their files. 2: You **do not** close a named pipe by deleting it. Closing and deleting are different operations. – willkil Mar 15 '17 at 16:00
  • 1
    You are correct. I have updated the answer for posterity. – Jeffrey Portouw Mar 24 '17 at 15:09
2

You probably confused closing with the opposite of mkfifo command.

An accepted answer is definitely the best solution for MATLAB users, but I'd like to clear things out for those who came here for named pipes.


On Unix-likes named pipe (FIFO) is a special type of file with no content. The mkfifo command creates the pipe on a file system (assigns a name to it), but doesn't open it. You need to open and close it separately like any other file.

  1. Do I have to close the named pipe myfifo after I use it? It seems to persist after the code is run.

It's generally a good idea to close/delete/free things up as soon as you don't need them anymore.
The pipe itself (and its content) gets destroyed when all descriptors to it are closed. What you see left is just a name.

  1. If myfifo needs to be closed, what is the command to close it?

Named pipe can be closed with fclose() function. To make the pipe anonymous and unavailable under the given name (can be done when the pipe is still open) you could use the MATLAB's delete function or the rm console command.

  1. I will be running the code sample above many times (>1000), so is it OK if I reuse the named pipe and do not close it until the end?

It's OK to reuse a named pipe as long as each iteration starts with an empty pipe (according to MATLAB documentation, fscanf() function will do this for you).

A named pipe can be reused in two ways:

  • By reusing the pipe itself (without closing it):
    system('mkfifo myfifo');
    tmp = fopen('myfifo', 'r+'); % Open the pipe in both ways (otherwise it will block)
    fid = fopen('myfifo', 'r');  % Open the pipe for reading (otherwise `fscanf` will block)
    fclose(tmp);                 % Close the auxiliary descriptor
    
    % Use the pipe multiple times...
    system('wgrib2.exe multi_1.glo_30m.hs.201212.grb2 -ij 1 165 -ij 1 166 > myfifo');
    a = fscanf(fid, '%c');
    ...
    
    % Close and delete the pipe
    fclose(fid);
    delete myfifo;
    
  • By reusing the name to open a pipe (the way you were using it).
EvgenKo423
  • 2,256
  • 2
  • 16
  • 23
1

You probably shouldn't use a named pipe for capturing system output in MATLAB. Per MATLAB's system() documentation, you can capture output directly in the system() call:

[status, cmdout] = system('wgrib2.exe multi_1.glo_30m.hs.201212.grb2 -ij 1 165 -ij 1 166');
a = cmdout

However, if you insist on using a named pipe, then Yes, you should close it. You should always close resources you open. But closing a named pipe does not delete it.

willkil
  • 1,619
  • 1
  • 21
  • 33
0

Cannot agree. You close fifo by closing it. The system closes fifo when it is finished (the system() call above). When the fifo is closed the other side knows that there's no more data (EOF condition, there is no explicit EOF check in the example code but it is very probable in the real one) and finishes.

Arseny
  • 207
  • 2
  • 6
  • 1
    'Cannot agree' with what? – user207421 Jul 28 '16 at 03:06
  • 1
    I actually came to this site because don't know much about fifos. But op expects deletion of fifo after closing it, while the system works obviosly other way. Just like with any other file one may open and close the fifo or decide to remove it. Here's the example: – Arseny Sep 05 '16 at 01:27
  • 1
    In one terminal: `mkfifo fifo` `cat - >fifo` in the other terminal: `cat fifo` Second terminal receives data from the first one until user press Ctrl-D closing the fifo. – Arseny Sep 05 '16 at 01:35