星期五, 3月 10, 2017

[ C ] fork() 利用pipe傳值


PIPE是linux IPC的一種溝通方式。

1. pipe所傳遞的資料只能向一個方向流動,不能雙方互相傳送。

2. 只能用於父子的process,或是兄弟間的pcocess。

3. 每次寫入的內容都是加在pipe buffer的後面,而讀出是從buffer的最前面讀出。

使用pipe時需要先宣告描述檔。(file descriptors)

int pi[2];

之後要用這描述檔丟給pipe去建立連線,

int pipe( int fd[2] );

RETURNS: 0 on success                                                       
        -1 on error: errno = EMFILE (no free descriptors)                  
                             EMFILE (system file table is full)            
                             EFAULT (fd array is not valid)


而fd[0]只能用於讀,fd[1]只能用於寫,
如果在寫時,可以先把fd[0]關掉,
在讀時,可以先把fd[1]關掉。


SourceCode

Ref
Ref_2

沒有留言: