Reputation: 855
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:
myfifo
after I use it? It seems to persist after the code is run.myfifo
needs to be closed, what is the command to close it?Upvotes: 19
Views: 26597
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.
- 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.
- 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.
- 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:
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;
Upvotes: 2
Reputation: 188
Edited to reflect below comment, which is correct. Deleting != closing.
fclose(mFifo)
As mentioned in the accepted answer, closing will not delete the fifo. You may need to do that separately.
Upvotes: 11
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
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