2015-05-07 56 views
0

我還沒有找到在線使用mkfifo()函數的完整示例。我能夠做的FIFO是這樣的:GNU Octave中的Mkfifo

mkfifo("file",777) 

但是,當我fopen()這個文件,倍頻只是掛起。從mkfifo對象創建,排隊和出隊字節的正確方法是什麼?

我想在Octave中創建一個內存中的fifo(在磁盤上也可以),並從同一個Octave腳本中讀寫。我的項目正在實時運行,所以我需要一個緩衝區,以便可以從同一個Octave腳本中進行填充和排空。我搜索了一個結果爲零的fifo庫。即使只是創建一個矢量和推送和彈出將適合我的需要。我自己嘗試過,但是我遇到了面向對象的編程設計問題,因爲Octave不允許通過引用或指針傳遞。

回答

0

該fifo類的作品,但只能達到一定的規模。 FIFO的最大字節大小可以通過運行找到:

​​

以下是我爲內存中的fifo編寫的代碼。這是相當簡陋的,但它運作良好:

1; % Prevent Octave from thinking that this is a function file 

global fifoCount fifoSamples fifoFiles fifoFids fifoDataType 
fifoSamples = zeros(0); 
fifoCount = 0; 
fifoFiles = cell(1); 
fifoFids = zeros(0); 
fifoDataType = 'single'; 
fifoDataTypeSize = 4; 
fifoMaxBytes = 1048576; % this is operating system enforced, changing here will not help 

function [] = o_fifo_write(index, data) 
    global fifoCount fifoSamples fifoFiles fifoFids fifoDataType 

    wrcount = fwrite(fifoFids(index), data, fifoDataType); 

    [sz,~] = size(data); 

    fifoSamples(index) = fifoSamples(index) + sz; 

    if(sz ~= wrcount) 
     disp(sprintf('o_fifo_write was given %d samples but wrote %d', sz, wrcount)); 
    end 

    if(~iscolumn(data)) 
     disp('data must be columnar in o_fifo_write'); 
    end 
end 

function [data] = o_fifo_read(index, count) 
    global fifoCount fifoSamples fifoFiles fifoFids fifoDataType 

    [data, rdcount] = fread(fifoFids(index), count, fifoDataType); 

    [sz,~] = size(data); 

    fifoSamples(index) = fifoSamples(index) - sz; 

    if(sz ~= rdcount || sz ~= count) 
     disp(sprintf('in o_fifo_read %d %d %d should all be the same', sz, rdcount, count)); 
    end 
end 

function [avail] = o_fifo_avail(index) 
    global fifoCount fifoSamples fifoFiles fifoFids fifoDataType 

    avail = fifoSamples(index); 
end 

function index = o_fifo_new() 
    global fifoCount fifoSamples fifoFiles fifoFids fifoDataType 

    fifoCount = fifoCount + 1; 
    index = fifoCount; 

    fifoSamples(index) = 0; 
    fifoFiles{index} = tempname; 
    [ERR, MSG] = mkfifo(fifoFiles{index}, base2dec('744',8)); 
    fifoFids(index) = fopen(fifoFiles{index}, 'a+'); 
%  fcntl(fifoFids(index), F_SETFL, O_NONBLOCK); % uncomment to avoid hangs when trying to overfill fifo 
end 


% ---- usage ----- 

txfifo = o_fifo_new(); 
disp(o_fifo_avail(txfifo)); 
o_fifo_write(txfifo, [1.243 pi 2*pi 4/3*pi]'); 
disp(o_fifo_avail(txfifo)); 
disp(o_fifo_read(txfifo, 4)); 
disp(o_fifo_avail(txfifo)); 
0

有兩個問題。第一:mkfifo預期模式爲以10爲底的整數,如果您在八進制中輸入「777」,則以8爲底數。第二:mkfifo使用umask修改權限(模式&〜umask)(參見man 3)

作爲例子:

fn=tempname 
[ERR, MSG] = mkfifo (fn, base2dec("744", 8)) 
stat(fn) 
fn = /tmp/oct-83UCBR 
ERR = 0 
MSG = 
ans = 

    scalar structure containing the fields: 

dev = 2053 
ino = 3408172 
mode = 4580 
modestr = prwxr--r-- 
nlink = 1 
uid = 1000 
gid = 1000 
rdev = 0 
size = 0 
atime = 1.4311e+09 
mtime = 1.4311e+09 
ctime = 1.4311e+09 
blksize = 4096 
blocks = 0 

正如你可以看到modestr現在prwxr - R--,你會從八進制744

現在你可以打開FIFO的一端期待:

fid = fopen (fn, "r") 

當然這會阻止直到fifo的另一端連接。

+0

感謝您的信息,但我如何讀寫FIFO? – portforwardpodcast

+0

@portforwardpodcast:「八度掛起」是fopen的預期阻止,直到另一端連接(因爲它沒有O_NONBLOCK而被打開) – Andy

+0

任何不清楚或丟失的東西? – Andy