“sys/epoll.h”代码

推荐阅读“sys/epoll.h”代码,有助于理解epoll函数的用法。
Browse the source code of glibc/sysdeps/unix/sysv/linux/sys/epoll.h

/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */
#ifndef        _SYS_EPOLL_H
#define        _SYS_EPOLL_H        1
#include <stdint.h>
#include <sys/types.h>
#include <bits/types/sigset_t.h>
/* Get the platform-dependent flags.  */
#include <bits/epoll.h>
#ifndef __EPOLL_PACKED
# define __EPOLL_PACKED
#endif
enum EPOLL_EVENTS
  {
    EPOLLIN = 0x001,
#define EPOLLIN EPOLLIN
    EPOLLPRI = 0x002,
#define EPOLLPRI EPOLLPRI
    EPOLLOUT = 0x004,
#define EPOLLOUT EPOLLOUT
    EPOLLRDNORM = 0x040,
#define EPOLLRDNORM EPOLLRDNORM
    EPOLLRDBAND = 0x080,
#define EPOLLRDBAND EPOLLRDBAND
    EPOLLWRNORM = 0x100,
#define EPOLLWRNORM EPOLLWRNORM
    EPOLLWRBAND = 0x200,
#define EPOLLWRBAND EPOLLWRBAND
    EPOLLMSG = 0x400,
#define EPOLLMSG EPOLLMSG
    EPOLLERR = 0x008,
#define EPOLLERR EPOLLERR
    EPOLLHUP = 0x010,
#define EPOLLHUP EPOLLHUP
    EPOLLRDHUP = 0x2000,
#define EPOLLRDHUP EPOLLRDHUP
    EPOLLEXCLUSIVE = 1u << 28,
#define EPOLLEXCLUSIVE EPOLLEXCLUSIVE
    EPOLLWAKEUP = 1u << 29,
#define EPOLLWAKEUP EPOLLWAKEUP
    EPOLLONESHOT = 1u << 30,
#define EPOLLONESHOT EPOLLONESHOT
    EPOLLET = 1u << 31
#define EPOLLET EPOLLET
  };
/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl().  */
#define EPOLL_CTL_ADD 1        /* Add a file descriptor to the interface.  */
#define EPOLL_CTL_DEL 2        /* Remove a file descriptor from the interface.  */
#define EPOLL_CTL_MOD 3        /* Change file descriptor epoll_event structure.  */
typedef union epoll_data
{
  void *ptr;
  int fd;
  uint32_t u32;
  uint64_t u64;
} epoll_data_t;
struct epoll_event
{
  uint32_t events;        /* Epoll events */
  epoll_data_t data;        /* User data variable */
} __EPOLL_PACKED;
__BEGIN_DECLS
/* Creates an epoll instance.  Returns an fd for the new instance.
   The "size" parameter is a hint specifying the number of file
   descriptors to be associated with the new instance.  The fd
   returned by epoll_create() should be closed with close().  */
extern int epoll_create (int __size) __THROW;
/* Same as epoll_create but with an FLAGS parameter.  The unused SIZE
   parameter has been dropped.  */
extern int epoll_create1 (int __flags) __THROW;
/* Manipulate an epoll instance "epfd". Returns 0 in case of success,
   -1 in case of error ( the "errno" variable will contain the
   specific error code ) The "op" parameter is one of the EPOLL_CTL_*
   constants defined above. The "fd" parameter is the target of the
   operation. The "event" parameter describes which events the caller
   is interested in and any associated user data.  */
extern int epoll_ctl (int __epfd, int __op, int __fd,
                      struct epoll_event *__event) __THROW;
/* Wait for events on an epoll instance "epfd". Returns the number of
   triggered events returned in "events" buffer. Or -1 in case of
   error with the "errno" variable set to the specific error code. The
   "events" parameter is a buffer that will contain triggered
   events. The "maxevents" is the maximum number of events to be
   returned ( usually size of "events" ). The "timeout" parameter
   specifies the maximum wait time in milliseconds (-1 == infinite).
   This function is a cancellation point and therefore not marked with
   __THROW.  */
extern int epoll_wait (int __epfd, struct epoll_event *__events,
                       int __maxevents, int __timeout);
/* Same as epoll_wait, but the thread's signal mask is temporarily
   and atomically replaced with the one provided as parameter.
   This function is a cancellation point and therefore not marked with
   __THROW.  */
extern int epoll_pwait (int __epfd, struct epoll_event *__events,
                        int __maxevents, int __timeout,
                        const __sigset_t *__ss);
__END_DECLS
#endif /* sys/epoll.h */

注:上述代码的出处:https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/sys/epoll.h.html

附录

linux内核的eventepoll.c文件所在的目录是“linux/fs/eventpoll.c”。GitHub地址如下:
https://github.com/lozhibo/linux/blob/master/fs/eventpoll.c
如有兴趣,可参考网上剖析epoll源代码的文章学习。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用epoll将程序改为非阻塞接收的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/netlink.h> #include <sys/socket.h> #include <sys/epoll.h> #define UEVENT_BUFFER_SIZE 2048 #define MAX_EVENTS 10 int main(int argc, char **argv) { int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); if (fd < 0) { perror("Failed to create socket"); return EXIT_FAILURE; } struct sockaddr_nl addr; memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = NETLINK_KOBJECT_UEVENT; if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Failed to bind socket"); close(fd); return EXIT_FAILURE; } // 创建 epoll 实例 int epfd = epoll_create1(0); if (epfd < 0) { perror("Failed to create epoll instance"); close(fd); return EXIT_FAILURE; } // 将 fd 添加到 epoll 实例 struct epoll_event event; event.data.fd = fd; event.events = EPOLLIN | EPOLLET; // 设置为边沿触发模式 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0) { perror("Failed to add fd to epoll"); close(epfd); close(fd); return EXIT_FAILURE; } char buf[UEVENT_BUFFER_SIZE]; while (1) { struct epoll_event events[MAX_EVENTS]; int n = epoll_wait(epfd, events, MAX_EVENTS, -1); if (n < 0) { perror("Failed to wait for events"); break; } for (int i = 0; i < n; i++) { if (events[i].events & EPOLLIN) { ssize_t n = recv(fd, buf, sizeof(buf), 0); if (n < 0) { perror("Failed to receive data"); break; } char *p = buf; while (p < buf + n) { printf("%s\n", p); p += strlen(p) + 1; } } } } close(epfd); close(fd); return EXIT_SUCCESS; } ``` 主要的改动如下: 1. 创建了一个 epoll 实例,并将 socket fd 添加到 epoll 实例中; 2. 将 while 循环改为 epoll_wait 调用,用于等待事件; 3. 在接收到 EPOLLIN 事件时,调用 recv 函数接收数据。 注意,我们将 epoll 实例设置为边沿触发模式(EPOLLET),这意味着一旦有数据到达,就会触发一次 EPOLLIN 事件,而不是像水平触发模式(EPOLLIN)那样在数据到达时持续触发 EPOLLIN 事件。这样可以避免 epoll_wait 函数一直返回可读事件,从而提高程序的效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值