国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 操作系統 > 正文

多路轉接之poll和select

2024-06-28 13:21:23
字體:
來源:轉載
供稿:網友
多路轉接之poll和select

先看poll():

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/stat.h> 5 #include <sys/types.h> 6 #include <fcntl.h> 7 #include <poll.h> 8  9 #define oops(x, n) { perror(x); exit(n); }10 11 int main()12 {13     mkfifo("fifo1", 0644);    14     mkfifo("fifo2", 0644);    15     16     int fd1 = open("fifo1", O_RDWR);17     int fd2 = open("fifo2", O_RDWR);18     char buf[1000];19     for(;;)20     {21         struct pollfd pfd[] = {{0, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};22         int ret = poll(pfd, 3, 1);23         if(ret > 0)24         {25             if(pfd[0].revents&POLLIN)26             {27                 scanf(" %[^/n]", buf);    28                 PRintf("keypad:%s/n", buf);29                 if(strcmp(buf, "quit") == 0) break;    30             }31             if(pfd[1].revents&POLLIN)32             {33                 int n = read(fd1, buf, sizeof(buf));    34                 buf[n] = '/0';35                 printf("fifo1:%s", buf);36             }37             if(pfd[2].revents&POLLIN)38             {39                 int n = read(fd2, buf, sizeof(buf));    40                 buf[n] = '/0';41                 printf("fifo2:%s", buf);42             }43         }44 45     }46     close(fd1);47     close(fd2);48     unlink("fifo1");49     unlink("fifo2");50 51     return 0;52 }
View Code

再來說說select():

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/stat.h> 5 #include <sys/types.h> 6 #include <fcntl.h> 7 #include <poll.h> 8  9 #define oops(x, n) { perror(x); exit(n); }10 11 int main()12 {13     mkfifo("fifo1", 0644);    14     mkfifo("fifo2", 0644);    15     16     int fd1 = open("fifo1", O_RDWR);17     int fd2 = open("fifo2", O_RDWR);18     char buf[1000];19     for(;;)20     {21         struct pollfd pfd[] = {{0, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};22         int ret = poll(pfd, 3, 1);23         if(ret > 0)24         {25             if(pfd[0].revents&POLLIN)26             {27                 scanf(" %[^/n]", buf);    28                 printf("keypad:%s/n", buf);29                 if(strcmp(buf, "quit") == 0) break;    30             }31             if(pfd[1].revents&POLLIN)32             {33                 int n = read(fd1, buf, sizeof(buf));    34                 buf[n] = '/0';35                 printf("fifo1:%s", buf);36             }37             if(pfd[2].revents&POLLIN)38             {39                 int n = read(fd2, buf, sizeof(buf));    40                 buf[n] = '/0';41                 printf("fifo2:%s", buf);42             }43         }44 45     }46     close(fd1);47     close(fd2);48     unlink("fifo1");49     unlink("fifo2");50 51     return 0;52 }
View Code


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米泉市| 章丘市| 文昌市| 石棉县| 昆山市| 秦安县| 汉沽区| 田阳县| 丰城市| 大荔县| 宝鸡市| 沅陵县| 大邑县| 甘洛县| 岱山县| 井冈山市| 青浦区| 通辽市| 乌审旗| 牡丹江市| 刚察县| 合川市| 凤庆县| 宜阳县| 涞水县| 偃师市| 新沂市| 溧水县| 建宁县| 辽宁省| 施甸县| 阳城县| 乌拉特后旗| 邵阳市| 汉源县| 西青区| 黄冈市| 潼关县| 天祝| 蒙山县| 独山县|