IO复用

xiaoxiao2021-02-28  111

select系统调用: select系统调用的用途是:在一段时间内,监听用户感兴趣的文件描述符上的可读,可写和异常等事件。 select系统调用的原型如下: int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout); (1)nfds指定被监控的文件描述符的总数。 (2)readfds,writefds,exceptfds参数分别指向可读,可写和异常等时间对应的文件描述符集合。 (3)timeout参数用来设置select函数的超时时间. select成功时返回就绪(可读,可写和异常)文件描述符的总数。若果在超时时间内没用任何文件描述符就绪,select将返回0,select失败时返回-1并设置errno。如果在 select等待时间内,程序接受到信号,则select立即-1,并设置errno为EINTER。 fd_set结构的定义如下: #include #define __FD_SETSIZE 1024 #include #define FD_SETSIZE __FD_SETSIZE typedef long int __fd_mask; #undef  __NFDBITS #dfine NFDBITS (8 * (int)sizeof(__fd_mask) typedef sruct  {     #ifdef __use_xopen            __fd_mask fds_bits[__FD_SETSIZE /__NFDBITS];      #define __FDS_BITS(set)    ((set)->fds_bits)     #else          __fd_mask    __fds_bits[__FD_SETSIZE/__NFDBITS]      #define __FDS_BITS(set)     ((set)->__fds_bits);     #endif } fd_set; 用以上定义可见,fd_set结构体仅包含一个整形数组, 该数组的每个元素的每一位标记一个文件描述符。fd_set能容纳的文件描述符数量由FD_SETSIZE指定,这就限制了select能同时处理的文件描述符的总量。 poll系统调用: poll系统调用和select类似,也是在指定时间内轮询一定数量的文件描述符,以测试其中是否有就绪者。poll的原型如下:

点击(此处)折叠或打开

#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里如何查找文件内容... 给主人留下些什么吧!~~ 评论热议
转载请注明原文地址: https://www.6miu.com/read-57791.html

最新回复(0)