多路转接之poll和select

本文介绍了使用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

 

转载于:https://www.cnblogs.com/takeaction/p/4376318.html

### epoll 的原理 epoll 是 Linux 提供的一种高效的 I/O 多路复用机制,特别适合处理大量并发连接。与传统的 select poll 不同,epoll 将添加/维护待检测任务阻塞进程/线程两个步骤分开,从而提高了效率。具体来说,epoll 使用 `epoll_ctl()` 来维护等待队列,并使用 `epoll_wait()` 来阻塞进程 [^3]。 epoll 的设计思路是通过红黑树来管理文件描述符,并通过事件驱动的方式通知应用程序哪些文件描述符已经准备好进行 I/O 操作。这种机制避免了每次调用时都需要传递整个文件描述符集合,从而显著减少了系统调用的开销。 ### epoll 的使用方法 要使用 epoll,首先需要创建一个 epoll 文件描述符,然后通过 `epoll_ctl()` 向 epoll 实例中添加或删除感兴趣的文件描述符。最后,通过 `epoll_wait()` 等待 I/O 事件的发生。 以下是一个简单的示例代码,展示了如何使用 epoll 监听管道读取数据: ```c #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/epoll.h> int main(int argc, const char* argv[]) { int fd[2]; pipe(fd); pid_t pid; pid = fork(); if (0 == pid) { // 子进程 close(fd[0]); char buf[5]; char ch = 'a'; while (1) { memset(buf, ch++, 5); write(fd[1], buf, 5); sleep(3); } } else { // 父进程 close(fd[1]); // 创建红黑树 int epfd = epoll_create(1); // 上树 struct epoll_event ev; ev.data.fd = fd[0]; ev.events = EPOLLIN; epoll_ctl(epfd, EPOLL_CTL_ADD, fd[0], &ev); // 监听 struct epoll_event evs[1]; // 接收从内核返回的有变化的文件描述符的数组。 while (1) { int n = epoll_wait(epfd, evs, 1, -1); if (1 == n) { char buf[64] = ""; n = read(fd[0], buf, 64); if (n <= 0) { printf("子进程关闭了写端"); close(fd[0]); epoll_ctl(epfd, EPOLL_CTL_DEL, fd[0], &ev); // 下树 break; } else { printf("读到子进程写的内容:%s\n", buf); } } } } return 0; } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值