我正在使用命名管道捕獲另一個程序(Matlab)中的外部程序(wgrib2)的輸出。 Matlab代碼如下,並且system
訪問命令行來製作管道。這裏是我的問題:應該關閉使用mkfifo打開的命名管道,以及如何操作?
- 我使用它後必須關閉命名管道
myfifo
嗎?代碼運行後似乎仍然存在。 - 如果需要關閉
myfifo
,關閉它的命令是什麼? - 我將在下面運行代碼示例多次(> 1000),如果我重新使用命名管道並且直到最後才關閉它,那麼是否可以?
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)
這個答案是錯誤的。 1:程序確實需要關閉管道。如果壞的程序不關閉他們的文件,即使工作站也可能用完打開的文件。 2:你**不要**通過刪除命名管道來關閉它。關閉和刪除是不同的操作。 – willkil
你是對的。我已經更新了後代的答案。 –