epoll 小记

【100%通过率】2023华为od机试C卷【电脑病毒感染】Java&Go 实现 一个局域网内有很多台电脑,分别标注为 0 ~ N-1 的数字。相连接的电脑距离不一样,所以感染时间不一样,感染时间用t 表示。其中网络内一台电脑被病毒感染,求其感染网络内所有的电脑最少需要多长时间。如果最后有电脑不会感染,则返回-1. 阅读详情

epoll

epoll - I/O event notification facility
The epoll API performs a similar task to poll(2): monitoring multiple file descriptors to see if I/O is possible on any of them. The epoll API can be used either as an edge-triggered or a level-triggered interface and scales well to large numbers of watched file descriptors.

  • 1
    epoll_create(2) creates an epoll instance and returns a file descriptor referring to that instance. (The more recent epoll_create1(2) extends the function‐ality of epoll_create(2).)

  • 2
    Interest in particular file descriptors is then registered via epoll_ctl(2). The set of file descriptors currently registered on an epoll instance is some‐times called an epoll set.

  • 3
    epoll_wait(2) waits for I/O events, blocking the calling thread if no events are currently available.

epoll_create
SYNOPSIS
       #include <sys/epoll.h>

       int epoll_create(int size);
       int epoll_create1(int flags);

DESCRIPTION
       epoll_create() creates an epoll(7) instance.  Since Linux 2.6.8, the size argument is ignored, but must be greater than zero; see NOTES below.

       epoll_create()  returns a file descriptor referring to the new epoll instance.  This file descriptor is used for all the subsequent calls to the epoll interface.  When no longer required, the
       file descriptor returned by epoll_create() should be closed by using close(2).  When all file descriptors referring to an epoll instance have been closed, the kernel destroys the instance and
       releases the associated resources for reuse.

   epoll_create1()
       If  flags  is  0,  then, other than the fact that the obsolete size argument is dropped, epoll_create1() is the same as epoll_create().  The following value can be included in flags to obtain
       different behavior:

       EPOLL_CLOEXEC
              Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.  See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.

RETURN VALUE
       On success, these system calls return a nonnegative file descriptor.  On error, -1 is returned, and errno is set to indicate the error.

ERRORS
       EINVAL size is not positive.

       EINVAL (epoll_create1()) Invalid value specified in flags.

       EMFILE The per-user limit on the number of epoll instances imposed by /proc/sys/fs/epoll/max_user_instances was encountered.  See epoll(7) for further details.

       EMFILE The per-process limit on the number of open file descriptors has been reached.

       ENFILE The system-wide limit on the total number of open files has been reached.

       ENOMEM There was insufficient memory to create the kernel object.

VERSIONS
       epoll_create() was added to the kernel in version 2.6.  Library support is provided in glibc starting with version 2.3.2.

       epoll_create1() was added to the kernel in version 2.6.27.  Library support is provided in glibc starting with version 2.9.

epoll_ctl
SYNOPSIS
       #include <sys/epoll.h>

       int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

DESCRIPTION
       This  system call performs control operations on the epoll(7) instance referred to by the file descriptor epfd.  It requests that the operation op be performed for the target file descriptor,
       fd.

       Valid values for the op argument are:

       EPOLL_CTL_ADD
              Register the target file descriptor fd on the epoll instance referred to by the file descriptor epfd and associate the event event with the internal file linked to fd.

       EPOLL_CTL_MOD
              Change the event event associated with the target file descriptor fd.

       EPOLL_CTL_DEL
              Remove (deregister) the target file descriptor fd from the epoll instance referred to by epfd.  The event is ignored and can be NULL (but see BUGS below).

       The event argument describes the object linked to the file descriptor fd.  The struct epoll_event is defined as:

           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 */
           };

       The events member is a bit mask composed using the following available event types:

       EPOLLIN
              The associated file is available for read(2) operations.

       EPOLLOUT
              The associated file is available for write(2) operations.

       EPOLLRDHUP (since Linux 2.6.17)
              Stream socket peer closed connection, or shut down writing half of connection.  (This flag is especially useful for writing simple code to detect peer shutdown when  using  Edge  Trig‐
              gered monitoring.)

       EPOLLPRI
              There is urgent data available for read(2) operations.

       EPOLLERR
              Error condition happened on the associated file descriptor.  epoll_wait(2) will always wait for this event; it is not necessary to set it in events.

       EPOLLHUP
              Hang up happened on the associated file descriptor.  epoll_wait(2) will always wait for this event; it is not necessary to set it in events.  Note that when reading from a channel such
              as a pipe or a stream socket, this event merely indicates that the peer closed its end of the channel.  Subsequent reads from the channel will return 0 (end of  file)  only  after  all
              outstanding data in the channel has been consumed.

       EPOLLET
              Sets  the  Edge  Triggered  behavior  for the associated file descriptor.  The default behavior for epoll is Level Triggered.  See epoll(7) for more detailed information about Edge and
              Level Triggered event distribution architectures.

       EPOLLONESHOT (since Linux 2.6.2)
              Sets the one-shot behavior for the associated file descriptor.  This means that after an event is pulled out with epoll_wait(2) the associated file descriptor  is  internally  disabled
              and no other events will be reported by the epoll interface.  The user must call epoll_ctl() with EPOLL_CTL_MOD to rearm the file descriptor with a new event mask.

       EPOLLWAKEUP (since Linux 3.5)
              If  EPOLLONESHOT  and EPOLLET are clear and the process has the CAP_BLOCK_SUSPEND capability, ensure that the system does not enter "suspend" or "hibernate" while this event is pending
              or being processed.  The event is considered as being "processed" from the time when it is returned by a call to epoll_wait(2) until the next call to epoll_wait(2) on the same epoll(7)
              file  descriptor,  the  closure  of that file descriptor, the removal of the event file descriptor with EPOLL_CTL_DEL, or the clearing of EPOLLWAKEUP for the event file descriptor with
              EPOLL_CTL_MOD.  See also BUGS.

RETURN VALUE
       When successful, epoll_ctl() returns zero.  When an error occurs, epoll_ctl() returns -1 and errno is set appropriately.

ERRORS
       EBADF  epfd or fd is not a valid file descriptor.

       EEXIST op was EPOLL_CTL_ADD, and the supplied file descriptor fd is already registered with this epoll instance.

       EINVAL epfd is not an epoll file descriptor, or fd is the same as epfd, or the requested operation op is not supported by this interface.

       ENOENT op was EPOLL_CTL_MOD or EPOLL_CTL_DEL, and fd is not registered with this epoll instance.

       ENOMEM There was insufficient memory to handle the requested op control operation.

       ENOSPC The limit imposed by /proc/sys/fs/epoll/max_user_watches was encountered while trying to register (EPOLL_CTL_ADD) a new file descriptor on an epoll instance.  See epoll(7) for  further
              details.

       EPERM  The target file fd does not support epoll.  This error can occur if fd refers to, for example, a regular file or a directory.

VERSIONS
       epoll_ctl() was added to the kernel in version 2.6.

CONFORMING TO
       epoll_ctl() is Linux-specific.  Library support is provided in glibc starting with version 2.3.2.
epoll_wait
SYNOPSIS
       #include <sys/epoll.h>

       int epoll_wait(int epfd, struct epoll_event *events,
                      int maxevents, int timeout);
       int epoll_pwait(int epfd, struct epoll_event *events,
                      int maxevents, int timeout,
                      const sigset_t *sigmask);

DESCRIPTION
       The  epoll_wait()  system  call  waits  for events on the epoll(7) instance referred to by the file descriptor epfd.  The memory area pointed to by events will contain the events that will be
       available for the caller.  Up to maxevents are returned by epoll_wait().  The maxevents argument must be greater than zero.

       The timeout argument specifies the number of milliseconds that epoll_wait() will block.  The call will block until either:

       *  a file descriptor delivers an event;

       *  the call is interrupted by a signal handler; or

       *  the timeout expires.

       Note that the timeout interval will be rounded up to the system clock granularity, and kernel scheduling delays mean that the blocking interval may overrun by a small  amount.   Specifying  a
       timeout of -1 causes epoll_wait() to block indefinitely, while specifying a timeout equal to zero cause epoll_wait() to return immediately, even if no events are available.

       The struct epoll_event is defined as:

           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 */
           };

       The  data  of  each  returned structure will contain the same data the user set with an epoll_ctl(2) (EPOLL_CTL_ADD, EPOLL_CTL_MOD) while the events member will contain the returned event bit
       field.

   epoll_pwait()
       The relationship between epoll_wait() and epoll_pwait() is analogous to the relationship between select(2) and pselect(2): like pselect(2), epoll_pwait() allows an application to safely  wait
       until either a file descriptor becomes ready or until a signal is caught.

       The following epoll_pwait() call:

           ready = epoll_pwait(epfd, &events, maxevents, timeout, &sigmask);

       is equivalent to atomically executing the following calls:

           sigset_t origmask;

           pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
           ready = epoll_wait(epfd, &events, maxevents, timeout);
           pthread_sigmask(SIG_SETMASK, &origmask, NULL);

       The sigmask argument may be specified as NULL, in which case epoll_pwait() is equivalent to epoll_wait().

RETURN VALUE
       When  successful,  epoll_wait() returns the number of file descriptors ready for the requested I/O, or zero if no file descriptor became ready during the requested timeout milliseconds.  When
       an error occurs, epoll_wait() returns -1 and errno is set appropriately.

ERRORS
       EBADF  epfd is not a valid file descriptor.

       EFAULT The memory area pointed to by events is not accessible with write permissions.

       EINTR  The call was interrupted by a signal handler before either (1) any of the requested events occurred or (2) the timeout expired; see signal(7).

       EINVAL epfd is not an epoll file descriptor, or maxevents is less than or equal to zero.

VERSIONS
       epoll_wait() was added to the kernel in version 2.6.  Library support is provided in glibc starting with version 2.3.2.

       epoll_pwait() was added to Linux in kernel 2.6.19.  Library support is provided in glibc starting with version 2.6.

example
#include <sys/epoll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 5
#define MAX_BUF_SIZE 128
/**
 * 
 * 
 * 
*/

int main() {
    struct epoll_event ev, events[MAX_SIZE];
    char buf[MAX_BUF_SIZE];
    int epoll_fd = epoll_create1(0);
    if (epoll_fd == -1) {
        printf("epoll create fail");
        return -1;
    }

    ev.events = EPOLLIN;
    ev.data.fd = 0; // stdin
    
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &ev) == -1) {
        perror("epoll_ctl: listen_sock");
        exit(EXIT_FAILURE);
    }

    while(1) {
        /***
         *        Note that the timeout interval will be rounded up to the system clock granularity, and kernel scheduling delays mean that the blocking interval may overrun by a small  amount.   Specifying  a
       timeout of -1 causes epoll_wait() to block indefinitely, while specifying a timeout equal to zero cause epoll_wait() to return immediately, even if no events are available.
       */
        int nfds = epoll_wait(epoll_fd, &events, MAX_SIZE, -1);
        if (nfds == -1) {
            perror("epoll_wait");
            exit(EXIT_FAILURE);
        }
        
        for (int n = 0; n < nfds; ++n) {
            if (events[n].data.fd == 0) {
                memset(buf, 0, MAX_BUF_SIZE);
                int len = read(0, buf, MAX_BUF_SIZE);
                if (strncmp("exit", buf, 4) == 0) {
                    printf("will exit!");
                    exit(EXIT_FAILURE);
                } else {
                    printf("receive : len[%d]  %s\n", len, buf);
                }
            }
        }
    }
    return 0;
}
自注意力机制(SelfAttention Mechanism)原理与代码实战案例讲解 自注意力机制(Self-Attention Mechanism)原理与代码实战案例讲解 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming 1. 背景介绍 阅读详情

相关推荐

RabbitMQ的使用(JAVA)

Java快速上手的MQ上线!

.............. 1万+

GitHub:基于epoll机制的高并发聊天室,c语言实现

https://github.com/jwzh222/epoll后期填坑。

五山口老法师 2804

小红书UID泄露风险揭秘:如何防止你的手机号被社工库关联(附防护指南)

本文揭示了小红书UID泄露的严重风险,详细解析了UID与手机号关联后被社工库利用的完整链条。通过分析黑产的数据整合技术,提供从基础到进阶的防护策略,包括密码管理、账号隔离和应急响应措施,帮助用户有效防范个人信息泄露风险。

ol789012345的博客 1501

彻底学会使用epoll(二)——ET和LT的触发方式

看了篇文章,受益匪浅 原文链接: http://blog.chinaunix.net/uid-28541347-id-4285054.html 分析了ET和LT的实现方式,那么分析他们的触发方式就容易多了。我们通过实现分析知道LT模式下epoll_wait被唤醒可以通过两种方式(图中红线和蓝线),而ET模式只能通过一种方式(图中红线)。所以ET模式下能被唤醒的情况,LT模式下一定也能被唤醒。我...

蜂蜜味的哈士奇 975

MATLAB实战:5步搞定铣削稳定性叶瓣图(附完整代码解析)

本文提供了一份基于MATLAB的铣削稳定性叶瓣图实战指南。通过5个清晰步骤,从动力学原理出发,详细解析了利用半离散法构建稳定性判据、模块化代码设计、高效参数扫描及高级可视化的完整流程,并附有完整代码,帮助工程师快速预测和规避加工颤振,优化铣削工艺参数。

weixin_29197051的博客 193

HBM PHY 架构解析:从主控模块到DWORD设计的深度探索

本文深入解析了HBM PHY的完整架构,从作为系统大脑的PUB主控模块出发,详细阐述了其CSR控制、初始化与训练引擎的核心功能。进而剖析了负责命令传输的AWORDX2模块与作为数据高速公路的DWORD模块的设计奥秘,包括其双向数据流、精密时钟对齐与延迟线校准机制。最后,探讨了顶层MASTER PHY的整合作用及从DFI到DRAM的完整数据通路,为理解这一高性能内存接口的关键物理层设计提供了全面视角。

weixin_29306011的博客 291

epoll 相关问题简单说明

阻塞、非阻塞、异步 I/O 阻塞: 以 read 为例,当 fd 中无数据,read 将进行等待,直到有数据为止,否则 read 将用于阻塞。 “低速”系统调用。 非阻塞: 当 fd 中无数据可读,那么 read 不进行等待而直接返回,并且返回相应错误码(EAGAIN)。 此时,怎么办呢?调用者可以以轮询的方式进行查看。如果不想轮询呢?系统提供了一套机制,帮助我们进行“轮询查看”工作,...

frodocheng的博客 1936

LVGL v9的lv_scale控件实战:5分钟打造动态仪表盘(附完整代码)

本文详细介绍了LVGL v9中lv_scale控件的实战应用,通过5个关键步骤快速打造动态仪表盘。文章涵盖核心特性解析、代码实现、样式定制及性能优化,适用于工业HMI、智能家居等场景,并附完整代码示例。

gin88的博客 261

epoll源码探秘(epoll_create)

epoll的源码浅谈

蔡曜镫专栏 5287

中国高精度年度平均气温栅格数据集(1901-2024)

本文基于中国月度平均气温栅格数据集,计算生成1901-2024年1km分辨率的年度平均气温数据集,并按中国省市行政区划进行裁剪。该数据集融合CRU全球数据和WorldClim高分辨率数据,经496个站点验证,具有高时空精度和长时间序列特点,覆盖全国(含港澳台)逾百年气温记录,为气候变化研究、生态水文分析及农业生产评估等提供重要数据支撑。数据格式为Tif,单位为0.1℃,可获取全国、各省及各市三个尺度的气温栅格数据。

PPdata_Academy的博客 1241

【服务器电源架构与关键技术发展趋势】AI数据中心迅猛发展,服务器电源如何演变?

功率级跃升:机柜功率从 100kW 向 1MW 级发展,供电架构采用 HVDC + 固态变压器(SST),省去传统 UPS、PFC 环节; 母线电压升级:从 48V 向 400V→800V 演进,配套 AI 电弧检测、智能运维解决高压安全与维护难题; 能效(PUE)提升:目标 PUE<1.3,通过液冷 CDU 替代传统风冷,可将 PUE 降至 1.16(传统 AI 集群 PUE 达 1.6); 拓扑控制复杂化:功率拓扑从单相→三相、单级→多交错结构,适配高功率需求; 安全维度扩展:新增网络安全防护需求。

昔时扬尘处 1449

epoll

来源:https://linux.die.net/man/7/epoll 来源: https://blog.youkuaiyun.com/hdutigerkin/article/details/7517390 Description(描述) The epoll API performs a similar task to poll(2): monitoring multiple file descriptor...

疯狮子的博客 762

Detector-Free SfM 项目复现完整指南:从环境搭建到代码解析与可视化

本文提供了Detector-Free SfM项目的完整复现指南,涵盖技术背景、环境配置、代码解析和可视化方案。重点包括: 技术背景:Detector-Free SfM通过两阶段策略(粗重建+迭代精化)解决了传统SfM在弱纹理区域的瓶颈问题,采用深度学习匹配器替代传统特征点检测。 环境配置: 详细硬件要求与推荐配置(GPU/CPU/内存) 完整的Ubuntu系统设置步骤 Conda环境创建与依赖安装流程 关键组件安装指南(PyTorch、COLMAP等) 常见错误解决方案 项目特点: 支持多种Detector

huanghm88的专栏 120

linux IO复用(epoll)小记

一、epoll简介 epoll是Linux内核为处理大批量文件描述符而作了改进的poll, 是Linux下多路复用IO接口select/poll的增强版本, 它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。另一点原因就是获取事件的时候, 它无须遍历整个被侦听的描述符集, 只要遍历那些被内核IO事件异步唤醒而加入Ready队列的描述符集合就行了。二、e...

weixin_34114823的博客 111

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

关于eventfd,epoll,线程间通信小记

先介绍eventfd 1 #include<sys/eventfd.h> 2 int eventfd(unsigned int initval, int flags); 使用这个函数来创建一个事件对象,linux线程间通信为了提高效率,大多使用异步通信,采用事件监听和回调函数的方式来实现高效的任务处理方式(虽然会将逻辑变得复杂)。 linux内核会为这个事件对象维护一个...

weixin_30660027的博客 403

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

http小记

http粘包 现象 如:客户端连续3次send 1k数据,并且每次发送间隔没有很明确(可以理解为每次发送间隔很短很短),会让服务器误以为客户端一次发送了3k的数据。因此服务器只会recv一次并且send一次返回给客户端,导致客户端只recv一次,与其send的次数不匹配。 解决办法 1、解析http数据,用\r\n\r\n进行区分(head中每一行都有/r/n,而head与body之间有一个空行,因此会有两个/r/n) 2、在http的head中定义包的长度(content-length) sendfile

asdaqqwc的博客 332

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

小记

搜索文件中含有“evutil_make_listen_socket_reuseable”,递归查找子目录 grep evutil_make_listen_socket_reuseable * -r

rongxiaojun的专栏 1928

Redis小记

Redis-benchmark测试redis Redis是单进程:单进程模型来处理客户端的请求,对读写等事件的响应是通过epoll函数的包装来做到的。 epoll是Linux内核为处理大批量文件描述符而作了改进的poll(具体我也不清楚) Redis有16个默认的数据库, 可以用  select NUM 来选择数据库(索引从0开始) 例如 select 7 是选择第8个数据库 Db

扁鹊的博客 468

NIO核心依赖多路复用小记

NIO允许一个线程同时处理多个连接,而不会因为一个连接的阻塞而导致其他连接被阻塞。核心是依赖操作系统的多路复用机制。

摸魚散人的博客 1282

【Java】知识点小记

之前在刷博客的时候,发现一些写得比较好的博客都会默默收藏起来。最近在查阅补漏,有的知识点比较重要的,但是在之前的博客中还没有写到,于是趁着闲整理一下。 文本的知识点: Integer常量池 TCP拆包粘包 select、poll、epoll简单区别 jdk1.6以后对Synchronize锁优化 Java内存模型 本文力求简单讲清每个知识点,希望大家看完能有所收获。 一、神奇的Int...

猿人启示录 448

事件上报处理流程小记(二)InputReader监听获取数据

【代码】InputReader监听获取数据。

qq_34841985的博客 333

std::asio小记

个人理解 asio是一个异步库,提供了支持函数异步执行的能力。 注: asio是proactor模式,在linux上没有现成的io复用可以支持asio。所以asio在Linux上是通过在epoll外封装一层来实现相关功能。 在Linux上的实现原理 asio本质上,是通过把函数进行封装,然后放入内部维护的队列中,然后在通过run方法从队列中取值,来达到异步的效果。当然内部也是对于多线程进行了支持,在多线程下,安全的访问内部队列。 ...

three_trees的blog 840

redis详解小记

String是最简单的数据类型,一般用于复杂的计数功能的缓存:微博数,粉丝数等。底层实现方式:动态字符串sds 或者 long。String的内部存储结构一般是sds(Simple Dynamic String),但是如果一个String类型的value的值是数字,那么Redis内部会把它转成long类型来存储,从而减少内存的使用。使用场景:缓存对象、常规计数、分布式锁、共享 session 信息等。

qq_38549201的博客 241

ceph:消息通信机制小记--研读

目录 Messenger的创建 创建Worker和Epoll句柄 启动线程 加入监听事件 等待并处理事件 服务端bind 服务端listen 处理连接 AsyncConnection的状态转换 AsyncMessenger启动接收线程 AsyncMessenger类图 Send IO Receive IO 原文:https:...

小鱼菜鸟的博客 404

工作小记: sendto失败 errno 22 / SO_REUSEADDR SO_REUSEPROT

sentto

饭真好吃的博客 2930

Nginx相关小记

netty小记

runningo的博客 202
上一篇: Android SELinux 小结
下一篇: CC2640R2 UART 配置
leoStack
博客等级 码龄11年 16粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值