linux進程間(jian)通信-FIFO,讓你(ni)全方位理解(jie)
時(shi)間:2018-06-20 來源:未(wei)知
有名管道(FIFO)
有(you)(you)名(ming)管道(dao)也被稱為FIFO文(wen)(wen)件(jian)(jian),是(shi)一(yi)種特殊的文(wen)(wen)件(jian)(jian)。由于linux所有(you)(you)的事物(wu)都可(ke)以(yi)被視為文(wen)(wen)件(jian)(jian),所以(yi)對有(you)(you)名(ming)管道(dao)的使用也就變得與文(wen)(wen)件(jian)(jian)操作(zuo)非常統一(yi)。
(1)創建有名(ming)管道(dao)
用如下兩個函數中的其中一(yi)個,可(ke)以(yi)創(chuang)建(jian)有名管道。
#include
#include
int mkfifo(const char *filename, mode_t mode);
filname是(shi)指文(wen)件名,而mode是(shi)指定文(wen)件的(de)讀(du)寫權限(xian)。
(2)打開有名管(guan)道
和打開其他文(wen)件一(yi)樣,可(ke)以用open來打開。通(tong)常有四種方(fang)法:
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);
有(you)三點要注意:
1就(jiu)是(shi)程序不能以O_RDWR(讀(du)寫(xie))模式(shi)打(da)開FIFO文件進行讀(du)寫(xie)操作(zuo),而其行為也未(wei)明確(que)定義,因為如一個(ge)管道以讀(du)/寫(xie)方式(shi)打(da)開,進程就(jiu)會讀(du)回(hui)自(zi)己的(de)(de)輸出(chu),同時我們通(tong)常使用FIFO只是(shi)為了單向(xiang)的(de)(de)數據(ju)傳遞。
2就是(shi)傳(chuan)遞給open調用的是(shi)FIFO的路(lu)徑名,而不是(shi)正常的文件。(如:
const char *fifo_name = "/tmp/my_fifo";)
3第二個(ge)參數中(zhong)的(de)(de)選項O_NONBLOCK,選項O_NONBLOCK表(biao)示非(fei)阻塞,加上這(zhe)個(ge)選項后(hou),表(biao)示open調用是(shi)非(fei)阻塞的(de)(de),如果(guo)沒有這(zhe)個(ge)選項,則表(biao)示open調用是(shi)阻塞的(de)(de)。
(3)阻塞問(wen)題
對于以(yi)只(zhi)讀方(fang)式(shi)(O_RDONLY)打(da)(da)開(kai)的(de)FIFO文件,如(ru)果(guo)open調用是阻塞的(de)(即第二(er)個(ge)參數(shu)(shu)為(wei)O_RDONLY),除非有一(yi)個(ge)進程(cheng)以(yi)寫(xie)方(fang)式(shi)打(da)(da)開(kai)同一(yi)個(ge)FIFO,否則它不會返回(hui);如(ru)果(guo)open調用是非阻塞的(de)的(de)(即第二(er)個(ge)參數(shu)(shu)為(wei)O_RDONLY | O_NONBLOCK),則即使(shi)沒(mei)有其他進程(cheng)以(yi)寫(xie)方(fang)式(shi)打(da)(da)開(kai)同一(yi)個(ge)FIFO文件,open調用將成功(gong)并立即返回(hui)。
對于以只寫方式(O_WRONLY)打開的(de)FIFO文件(jian),如(ru)果open調(diao)用(yong)(yong)是阻(zu)塞的(de)(即(ji)第(di)二(er)個參數(shu)為O_WRONLY),open調(diao)用(yong)(yong)將被阻(zu)塞,直到有(you)(you)一(yi)個進程以只讀方式打開同一(yi)個FIFO文件(jian)為止;如(ru)果open調(diao)用(yong)(yong)是非(fei)阻(zu)塞的(de)(即(ji)第(di)二(er)個參數(shu)為O_WRONLY | O_NONBLOCK),open總會立(li)即(ji)返(fan)回(hui),但如(ru)果沒有(you)(you)其他進程以只讀方式打開同一(yi)個FIFO文件(jian),open調(diao)用(yong)(yong)將返(fan)回(hui)-1,并(bing)且FIFO也不會被打開。
(4)使用FIFO實現進程間的通信
管(guan)道的寫(xie)入(ru)端(duan)從(cong)一(yi)個文(wen)(wen)件(jian)讀(du)出數據,然(ran)后寫(xie)入(ru)寫(xie)管(guan)道。管(guan)道的讀(du)取端(duan)從(cong)管(guan)道讀(du)出后寫(xie)到文(wen)(wen)件(jian)中(zhong)。
寫(xie)入端代碼: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);
}
管道讀取(qu)端(duan): 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)有名管(guan)道(dao)的安全問題
有(you)一(yi)種情況是(shi)(shi):一(yi)個(ge)FIFO文件,有(you)多(duo)個(ge)進(jin)(jin)程同(tong)時向同(tong)一(yi)個(ge)FIFO文件寫(xie)(xie)數(shu)(shu)據(ju),而只(zhi)有(you)一(yi)個(ge)讀(du)FIFO進(jin)(jin)程在同(tong)一(yi)個(ge)FIFO文件中讀(du)取數(shu)(shu)據(ju)時,會(hui)發生(sheng)數(shu)(shu)據(ju)塊的(de)(de)相互交錯。不同(tong)進(jin)(jin)程向一(yi)個(ge)FIFO讀(du)進(jin)(jin)程發送數(shu)(shu)據(ju)是(shi)(shi)很普(pu)通的(de)(de)情況。這(zhe)個(ge)問題的(de)(de)解決(jue)方法,就(jiu)是(shi)(shi)讓寫(xie)(xie)操(cao)作的(de)(de)原子化。系統規定:在一(yi)個(ge)以O_WRONLY(即阻塞方式)打(da)開的(de)(de)FIFO中, 如(ru)果(guo)寫(xie)(xie)入的(de)(de)數(shu)(shu)據(ju)長度小于(yu)等待PIPE_BUF,那么(me)或者(zhe)寫(xie)(xie)入全(quan)部字(zi)節,或者(zhe)一(yi)個(ge)字(zi)節都不寫(xie)(xie)入。如(ru)果(guo)所(suo)有(you)的(de)(de)寫(xie)(xie)請(qing)求都是(shi)(shi)發往(wang)一(yi)個(ge)阻塞的(de)(de)FIFO的(de)(de),并且每個(ge)寫(xie)(xie)記請(qing)求的(de)(de)數(shu)(shu)據(ju)長度小于(yu)等于(yu)PIPE_BUF字(zi)節,系統就(jiu)可(ke)以確(que)保數(shu)(shu)據(ju)決(jue)不會(hui)交錯在一(yi)起。