epoll机制代码核对

epoll工作机理

       epoll_wait检查epoll句柄中的rdllist不为空退出循环

       遍历所有的epitem,把各种ready的fd收集起来返回

谁来影响rdllist不为空,一般的说法是文件的fd的状态发生改变,就能有某个方法将fd加入到rdllist中,并通知调用epoll_wait的进程

        在epoll_add的时候,会注册回调函数,在fd准备好的时候,执行这个回调函数更新rdllist

那么这个过程是怎么样的呢?

       ep_poll_callback会完成rdllist的状态更新

回调函数的调用链

       tcp_v4_do_rcv(net/ipv4/tcp_ipv4.c)

       tcp_rcv_established(net/ipv4/tcp_input.c)

       sock_def_readable(net/core/sock.c)

       wake_up_interruptiable_sync_poll(wait.h)

       __wake_up_sync_key(kernel/sched.c)

       __wake_up_common(kernel/sched.c)

   static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,int nr_exclusive, int wake_flags, void *key)
      {
                 wait_queue_t *curr, *next;
                list_for_each_entry_safe(curr, next, &q->task_list, task_list) 
               {
                   unsigned flags = curr->flags;
                   if (curr->func(curr, mode, wake_flags, key) && 
                        (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
                    break;
              }
      }

      func就是ep_poll_callback,从而完成了rdllist中的变更

回调函数是如何注册进去的呢?

      ep_insert=>sock_poll=>sock->ops->poll(tcp_poll)=>sock_poll_wait-=>poll_wait=>ep_ptable_queue_proc-=>init_waitqueue_func_entry

   ep_insert(struct eventpoll *ep, struct epoll_event *event,struct file *tfile, int fd)
   {  
     ....
     revents = tfile->f_op->poll(tfile, &epq.pt);
     ....
   }
   static const struct file_operations socket_file_ops = {
        .owner =THIS_MODULE,
        .poll =sock_poll,
   };
   
   static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
   {
            if (p && wait_address)
            p->qproc(filp, wait_address, p);
                //qproc就是ep_ptable_queue_proc
  }
   static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,poll_table *pt)
  {
        ......
        init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
        .....
   }
   static inline void init_waitqueue_func_entry(wait_queue_t *q,wait_queue_func_t func)
   {
      q->flags = 0;
      q->private = NULL;
      q->func = func; //ep_poll_callback
   }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Linux C epoll机制示例代码,用于监听文件描述符的可读事件: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/epoll.h> #define MAX_EVENTS 10 int main() { int fd, sockfd, n, epollfd; char buffer[256]; struct sockaddr_in serveraddr, clientaddr; socklen_t clientlen = sizeof(clientaddr); struct epoll_event ev, events[MAX_EVENTS]; // 创建socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } // 绑定地址 bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = INADDR_ANY; serveraddr.sin_port = htons(5000); if (bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) { perror("ERROR on binding"); exit(1); } // 监听 listen(sockfd, 5); // 创建epoll实例 epollfd = epoll_create1(0); if (epollfd == -1) { perror("epoll_create1"); exit(EXIT_FAILURE); } // 添加sockfd到epoll实例中 ev.events = EPOLLIN; ev.data.fd = sockfd; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1) { perror("epoll_ctl: sockfd"); exit(EXIT_FAILURE); } // 等待事件发生 while (1) { n = epoll_wait(epollfd, events, MAX_EVENTS, -1); for (int i = 0; i < n; i++) { // 如果是sockfd可读事件,则接收客户端连接 if (events[i].data.fd == sockfd) { fd = accept(sockfd, (struct sockaddr *) &clientaddr, &clientlen); if (fd < 0) { perror("ERROR on accept"); exit(1); } printf("Client connected\n"); // 将新的连接fd添加到epoll实例中 ev.events = EPOLLIN; ev.data.fd = fd; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) { perror("epoll_ctl: fd"); exit(EXIT_FAILURE); } } // 如果是其他fd可读事件,则接收数据 else { bzero(buffer, 256); if (read(events[i].data.fd, buffer, 255) < 0) { perror("ERROR reading from socket"); exit(1); } printf("Message received: %s\n", buffer); } } } // 关闭socket和epoll实例 close(sockfd); close(epollfd); return 0; } ``` 以上代码监听端口5000,接收客户端连接并接收数据。通过epoll机制,可以同时监控多个文件描述符的可读事件,从而提高程序的效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值