国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩

當前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學習 > 講師博文 > linux進程間通信-FIFO,讓你全方位理解

linux進程間通信-FIFO,讓你全方位理解 時間:2018-06-20      來源:未知

有名管道(FIFO)

有名管道也被稱為FIFO文件,是一種特殊的文件。由于linux所有的事物都可以被視為文件,所以對有名管道的使用也就變得與文件操作非常統(tǒng)一。

(1)創(chuàng)建有名管道

用如下兩個函數(shù)中的其中一個,可以創(chuàng)建有名管道。

#include

#include

int mkfifo(const char *filename, mode_t mode);

filname是指文件名,而mode是指定文件的讀寫權(quán)限。

(2)打開有名管道

和打開其他文件一樣,可以用open來打開。通常有四種方法:

open(const char *path, O_RDONLY);

open(const char *path, O_RDONLY | O_NONBLOCK);

open(const char *path, O_WRONLY);

open(const char *path, O_WRONLY | O_NONBLOCK);

有三點要注意:

1就是程序不能以O(shè)_RDWR(讀寫)模式打開FIFO文件進行讀寫操作,而其行為也未明確定義,因為如一個管道以讀/寫方式打開,進程就會讀回自己的輸出,同時我們通常使用FIFO只是為了單向的數(shù)據(jù)傳遞。

2就是傳遞給open調(diào)用的是FIFO的路徑名,而不是正常的文件。(如:

const char *fifo_name = "/tmp/my_fifo";)

3第二個參數(shù)中的選項O_NONBLOCK,選項O_NONBLOCK表示非阻塞,加上這個選項后,表示open調(diào)用是非阻塞的,如果沒有這個選項,則表示open調(diào)用是阻塞的。

(3)阻塞問題

對于以只讀方式(O_RDONLY)打開的FIFO文件,如果open調(diào)用是阻塞的(即第二個參數(shù)為O_RDONLY),除非有一個進程以寫方式打開同一個FIFO,否則它不會返回;如果open調(diào)用是非阻塞的的(即第二個參數(shù)為O_RDONLY | O_NONBLOCK),則即使沒有其他進程以寫方式打開同一個FIFO文件,open調(diào)用將成功并立即返回。

對于以只寫方式(O_WRONLY)打開的FIFO文件,如果open調(diào)用是阻塞的(即第二個參數(shù)為O_WRONLY),open調(diào)用將被阻塞,直到有一個進程以只讀方式打開同一個FIFO文件為止;如果open調(diào)用是非阻塞的(即第二個參數(shù)為O_WRONLY | O_NONBLOCK),open總會立即返回,但如果沒有其他進程以只讀方式打開同一個FIFO文件,open調(diào)用將返回-1,并且FIFO也不會被打開。

(4)使用FIFO實現(xiàn)進程間的通信

管道的寫入端從一個文件讀出數(shù)據(jù),然后寫入寫管道。管道的讀取端從管道讀出后寫到文件中。

寫入端代碼:fifowrite.c

#include

#include

#include

#include

#include

#include

#include

#include

int main()

{

const char *fifo_name = "/tmp/my_fifo";

int pipe_fd = -1;

int data_fd = -1;

int res = 0;

const int open_mode = O_WRONLY;

int bytes_sent = 0;

char buffer[PIPE_BUF + 1];

int bytes_read = 0;

if(access(fifo_name, F_OK) == -1)

{

printf ("Create the fifo pipe.\n");

res = mkfifo(fifo_name, 0777);

if(res != 0)

{

fprintf(stderr, "Could not create fifo %s\n", fifo_name);

exit(EXIT_FAILURE);

}

}

printf("Process %d opening FIFO O_WRONLY\n", getpid());

pipe_fd = open(fifo_name, open_mode);

printf("Process %d result %d\n", getpid(), pipe_fd);

if(pipe_fd != -1)

{

bytes_read = 0;

data_fd = open("Data.txt", O_RDONLY);

if (data_fd == -1)

{

close(pipe_fd);

fprintf (stderr, "Open file[Data.txt] failed\n");

return -1;

}

bytes_read = read(data_fd, buffer, PIPE_BUF);

buffer[bytes_read] = '\0';

while(bytes_read > 0)

{

res = write(pipe_fd, buffer, bytes_read);

if(res == -1)

{

fprintf(stderr, "Write error on pipe\n");

exit(EXIT_FAILURE);

}

bytes_sent += res;

bytes_read = read(data_fd, buffer, PIPE_BUF);

buffer[bytes_read] = '\0';

}

close(pipe_fd);

close(data_fd);

}

else

exit(EXIT_FAILURE);

printf("Process %d finished\n", getpid());

exit(EXIT_SUCCESS);

}

管道讀取端: fiforead.c

#include

#include

#include

#include

#include

#include

#include

#include

int main()

{

const char *fifo_name = "/tmp/my_fifo";

int pipe_fd = -1;

int data_fd = -1;

int res = 0;

int open_mode = O_RDONLY;

char buffer[PIPE_BUF + 1];

int bytes_read = 0;

int bytes_write = 0;

memset(buffer, '\0', sizeof(buffer));

printf("Process %d opening FIFO O_RDONLY\n", getpid());

pipe_fd = open(fifo_name, open_mode);

data_fd = open("DataFormFIFO.txt", O_WRONLY|O_CREAT, 0644);

if (data_fd == -1)

{

fprintf(stderr, "Open file[DataFormFIFO.txt] failed\n");

close(pipe_fd);

return -1;

}

printf("Process %d result %d\n",getpid(), pipe_fd);

if(pipe_fd != -1)

{

do

{

res = read(pipe_fd, buffer, PIPE_BUF);

bytes_write = write(data_fd, buffer, res);

bytes_read += res;

}while(res > 0);

close(pipe_fd);

close(data_fd);

}

else

exit(EXIT_FAILURE);

printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);

exit(EXIT_SUCCESS);

}

(5)有名管道的安全問題

有一種情況是:一個FIFO文件,有多個進程同時向同一個FIFO文件寫數(shù)據(jù),而只有一個讀FIFO進程在同一個FIFO文件中讀取數(shù)據(jù)時,會發(fā)生數(shù)據(jù)塊的相互交錯。不同進程向一個FIFO讀進程發(fā)送數(shù)據(jù)是很普通的情況。這個問題的解決方法,就是讓寫操作的原子化。系統(tǒng)規(guī)定:在一個以O(shè)_WRONLY(即阻塞方式)打開的FIFO中, 如果寫入的數(shù)據(jù)長度小于等待PIPE_BUF,那么或者寫入全部字節(jié),或者一個字節(jié)都不寫入。如果所有的寫請求都是發(fā)往一個阻塞的FIFO的,并且每個寫記請求的數(shù)據(jù)長度小于等于PIPE_BUF字節(jié),系統(tǒng)就可以確保數(shù)據(jù)決不會交錯在一起。

上一篇:資深程序員告訴你串口配置的詳細流程,不容錯過

下一篇:imageview的基本屬性介紹,通俗易懂

熱點文章推薦
華清學員就業(yè)榜單
高薪學員經(jīng)驗分享
熱點新聞推薦
前臺專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權(quán)所有 ,京ICP備16055225號-5,京公海網(wǎng)安備11010802025203號

回到頂部