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