点击(此处)折叠或打开
#include<poll.h> int poll(struct pollfd *fds,nfds_t nfds,int timeout) 1)fds参数是一个pollfd结构类型的数组,它指定所有我们感兴趣的文件描述符上发生的可读,可写和异常等事件。pollfd结构体定义如下: struct pollfd { int fd;//文件描述符 short events;//注册的事件 short revents;//实际发生的事件,由内核填充 }; poll事件类型 POLLIN POLLOUT POLLERR POLLRDHUP等。 2)nfds参数指定被监听事件集合fds的大小。 typedef unsigned long nfds_t; 3)timeout参数指定poll的超时值,单位是毫秒。timeout为-1,poll调用将永远阻塞,直到某个事件发生;当timeout为0时,poll 调用将立即返回。 返回值含义与select相同。 poll的一个简单应用:在/root/pro/fd1 /root/pro/fd2中分别有内容,
1234
5678
和
1122
3344
点击(此处)折叠或打开
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <sys/types.h> #include <stropts.h> #include <sys/poll.h> #include <sys/stropts.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h> #include <poll.h> #define BUFSIZE 1024 int main(int argc, char *argv[]) { char buf[BUFSIZE]; int bytes; struct pollfd *pollfd; int i=0; int nummonitor=0; int numready; int errno; char *str; if(argc != 3) { fprintf(stderr,"Usage:the argc num error\n"); exit(1); } if((pollfd = (struct pollfd*)calloc(2, sizeof(struct pollfd))) == NULL) //为struct pollfd分配空间 exit(1); for(i; i<2; i++) //初始化化struct pollfd结构 { str = (char*)malloc(14*sizeof(char)); memcpy(str,"/root/pro/",14); strcat(str,argv[i+1]);//注意,需要把路劲信息放到str中,否则opne("/root/pro/argv[i]",O_RDONLY)会出错 printf("str=%s\n",str);//原因在于,在” “之中的argv[i]是字符串,不会用变量代替argv[i]. (pollfd+i)->fd = open(str,O_RDONLY); if((pollfd+i)->fd >= 0) fprintf(stderr, "open (pollfd+%d)->fd:%s\n", i, argv[i+1]); nummonitor++; (pollfd+i)->events = POLLIN;//与下面的pollfd->revent对应 } printf("nummonitor=%d\n",nummonitor); while(nummonitor > 0) { numready = poll(pollfd, 2, -1); if ((numready == -1) && (errno == EINTR)) continue; //被信号中断,继续等待 else if (numready == -1) break; //poll真正错误,推出 printf("numready=%d\n",numready); for (i=0;nummonitor>0 && numready>0; i++) { if((pollfd+i)->revents & POLLIN) { bytes = read(pollfd[i].fd, buf, BUFSIZE); numready--; printf("pollfd[%d]->fd read buf:\n%s \n", i, buf); nummonitor--; } } } for(i=0; i<nummonitor; i++) close(pollfd[i].fd); free(pollfd); return 0; } 运行结果如下: epoll是linux特有的I/O复用函数。它在实现上和使用上与select,poll有很大区别。首先epoll用一组函数来完成任务,而不是单个函数,其次epoll把用户关心的文件描述符上的事件放在内核里的一个事件表中。 epoll需要使用一个额外的文件描述符,来唯一标识内核中的这个事件表。这个文件描述符使用如下函数创建:点击(此处)折叠或打开
#include<sys/epoll.h> int epoll_create(int size); size参数只是给内核一个提示,告诉它事件表有多大。 下面的函数用来操作epoll的内核事件表点击(此处)折叠或打开
#include<sys/epoll.h> int epoll_ctl(int epfd,int op,int fd,struct epoll_event *event); fd参数是要操作的文件描述符,op参数指定操作类型,event参数指定事件。 epoll_event定义如下: struct epoll_event { __uint32_t events; /*epoll事件*/ epoll_data_t data; //用户数据 }; typedef union epoll_data { void *ptr; int fd;//使用做多的 uint32_t u32; uint64_t u64; }epoll_data_t; epoll_ctl成功时返回0,失败则返回-1并设置errno。 epoll系统调用的主要接口是epoll_wait函数,它在一段超时时间内等待一组文件描述符上的事件,其原型如下:点击(此处)折叠或打开
#icnldue<sys/epoll.h> int epoll_event(int epfd,struct epoll_event *events,int maxevents,int timeout); 成功时返回就绪的文件描述符个数,失败时返回-1并设置errno的值。 maxevents参数指定最多监听多少个事件。 epoll_wait函数如果检测到事件,就将所有的就绪事件从内核事件表中复制到它的第二个参数events指向的数组中。这个数组只用于输出epoll检测到的就绪事件。 <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 阅读(188) | 评论(0) | 转发(0) | 1上一篇:elf文件格式与动态链接库(非常之好)-----不可不看
下一篇:嵌入汇编
相关热门文章 linux 常见服务端口xmanager 2.0 for linux配置【ROOTFS搭建】busybox的httpd...openwrt中luci学习笔记Linux里如何查找文件内容... 给主人留下些什么吧!~~ 评论热议