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?Closing a FIFOThe parent closes the FIFO after writing all the data. The child had previously opened the FIFO in READ ONLY mode (and no other processes have the FIFO open for WRITING).
A traditional pipe is “unnamed” and lasts only as long as the process. A named pipe, however, can last as long as the system is up, beyond the life of the process. It can be deleted if no longer used. Usually a named pipe appears as a file and generally processes attach to it for inter-process communication.
Yes, it is inherently thread safe, because it doesn't use a handle, a buffer, or anything else on the client side that the two (or more) threads might try to access simultaneously. Each time you call CallNamedPipe it opens a new instance of the named pipe, sends the message, and closes the handle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With