KAE
KAE

Reputation: 855

Should named pipes opened with mkfifo be closed and how?

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?

Upvotes: 19

Views: 26597

Answers (4)

EvgenKo423
EvgenKo423

Reputation: 3010

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).

Upvotes: 2

Jeffrey Portouw
Jeffrey Portouw

Reputation: 188

  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.

Upvotes: 11

willkil
willkil

Reputation: 1669

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.

Upvotes: 1

Arseny
Arseny

Reputation: 205

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.

Upvotes: 0

Related Questions