|     本文關鍵字: 有名管道,linux有名管道     1.實驗目(mu)的     通(tong)過(guo)編寫有(you)名管(guan)(guan)道(dao)多(duo)路通(tong)信(xin)實(shi)驗,進一(yi)步掌握管(guan)(guan)道(dao)的創建、讀寫等操(cao)作,同時復習(xi)使用select()函數實(shi)現管(guan)(guan)道(dao)的通(tong)信(xin)。     2.實(shi)驗內容     這(zhe)里采(cai)用(yong)管(guan)道函(han)數(shu)(shu)創(chuang)建有名管(guan)道(并(bing)不(bu)是在控制(zhi)臺下輸入命令),而(er)且使用(yong)select()函(han)數(shu)(shu)替(ti)代poll()函(han)數(shu)(shu)實現多路復(fu)用(yong)(使用(yong)select()函(han)數(shu)(shu)是出于以演示為目(mu)的)。     3.實驗步(bu)驟     (1)畫出流程(cheng)圖。該實(shi)驗(yan)流程(cheng)圖如(ru)圖1所示。  圖1  實驗流程圖
     (2)編寫(xie)代碼(ma)(ma)。該實驗源(yuan)代碼(ma)(ma)如下:     /* pipe_select.c */#include <fcntl.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <errno.h>
 
 #define FIFO1				"in1"
 #define FIFO2				"in2"
 #define MAX_BUFFER_SIZE		1024			/* 緩沖區大小 */
 #define IN_FILES			3				/* 多路復用輸入文件數目 */
 #define TIME_DELAY			60			/* 超時值秒數 */
 #define MAX(a, b)			((a > b)?(a):(b))
 
 int main(void)
 {
 int fds[IN_FILES];
 char buf[MAX_BUFFER_SIZE];
 int i, res, real_read, maxfd;
 struct timeval tv;
 fd_set inset,tmp_inset;
 
 fds[0] = 0;
 
 /* 創建兩個有名管道 */
 if (access(FIFO1, F_OK) == -1)
 {
 if ((mkfifo(FIFO1, 0666) < 0) && (errno != EEXIST))
 {
 printf("Cannot create fifo file\n");
 exit(1);
 }
 }
 if (access(FIFO2, F_OK) == -1)
 {
 if ((mkfifo(FIFO2, 0666) < 0) && (errno != EEXIST))
 {
 printf("Cannot create fifo file\n");
 exit(1);
 }
 }
 
 /* 以只讀非阻塞方式打開兩個管道文件 */
 if((fds[1] = open (FIFO1, O_RDONLY|O_NONBLOCK)) < 0)
 {
 printf("Open in1 error\n");
 return 1;
 }
 if((fds[2] = open (FIFO2, O_RDONLY|O_NONBLOCK)) < 0)
 {
 printf("Open in2 error\n");
 return 1;
 }
 
 /* 取出兩個文件描述符中的較大者 */
 maxfd = MAX(MAX(fds[0], fds[1]), fds[2]);
 /* 初始化讀集inset,并在讀文件描述符集中加入相應的描述集 */
 FD_ZERO(&inset);
 for (i = 0; i < IN_FILES; i++)
 {
 FD_SET(fds[i], &inset);
 }
 FD_SET(0, &inset);
 
 tv.tv_sec = TIME_DELAY;
 tv.tv_usec = 0;
 /* 循環測試該文件描述符是否準備就緒,并調用select()函數對相關文件描述符做相應操作* /
 while(FD_ISSET(fds[0],&inset) || FD_ISSET(fds[1],&inset) || FD_ISSET(fds[2],
 &inset))
 {
 /* 文件描述符集的備份,以免每次都進行初始化 */
 tmp_inset = inset;
 res = select(maxfd + 1, &tmp_inset, NULL, NULL, &tv);
 switch(res)
 {
 case -1:
 {
 printf("Select error\n");
 return 1;
 }
 break;
 case 0: /* Timeout */
 {
 printf("Time out\n");
 return 1;
 }
 break;
 default:
 {
 for (i = 0; i < IN_FILES; i++)
 {
 if (FD_ISSET(fds[i], &tmp_inset))
 {
 memset(buf, 0, MAX_BUFFER_SIZE);
 real_read = read(fds[i], buf, MAX_BUFFER_SIZE);
 if (real_read < 0)
 {
 if (errno != EAGAIN)
 {
 return 1;
 }
 }
 else if (!real_read)
 {
 close(fds[i]);
 FD_CLR(fds[i], &inset);
 }
 else
 {
 if (i == 0)
 {  /* 主程序終端控制 */
 if ((buf[0] == 'q') || (buf[0] == 'Q'))
 {
 return 1;
 }
 }
 else
 {  /* 顯示管道輸入字符串 */
 buf[real_read] = '\0';
 printf("%s", buf);
 }
 }
 } /* end of if */
 } /* end of for */
 }
 break;
 } /* end of switch */
 } /* end of while */
 return 0;
 }
     (3)編譯并運(yun)行該程序。     (4)另(ling)外打開兩個虛擬終端,分別輸(shu)入“cat > in1”和(he)“cat > in2”,接著在該管道中輸(shu)入相(xiang)關內容,并觀(guan)察實驗(yan)結果。     4.實驗結果     實(shi)驗運行結果(guo)如下:     $ ./pipe_select (必須先運行主程序) SELECT CALL
 select call
 TEST PROGRAMME
 test programme
 END
 end
 q /* 在終端上輸入“q”或“Q”立刻結束程序運行 */
 
 $ cat > in1
 SELECT CALL
 TEST PROGRAMME
 END
 
 $ cat > in2
 select call
 test programme
 end
     本文選自華清遠見嵌入式培訓教材《從實踐中學嵌入式Linux應用程序開發》    熱(re)點鏈接: 
         1、有名管道(FIFO)2、linux 消息隊列
 3、linux 共享內存
 4、linux下的信號量
 5、linux下的信號處理實例
 
 更多新聞>>  |