linux - 当 fd 关闭时,我会收到 epoll 的通知吗?

标签 linux epoll

我目前正在构建一些使用epoll的东西。它工作得非常好,但是当底层 fd 关闭时,当文件描述符从 epoll 中删除时,最好能收到通知。

有没有办法在 fd 关闭时立即从 epoll 获取通知?

最佳答案

没有。这是 Zig程序演示:

const std = @import("std");

pub fn main() !void {
    const epollfd = blk: {
        const rc = std.os.linux.epoll_create1(std.os.linux.EPOLL_CLOEXEC);
        const err = std.os.linux.getErrno(rc);
        if (err != 0) return error.EpollCreateFailed;
        break :blk @intCast(i32, rc);
    };
    defer std.os.close(epollfd);

    var pipe: [2]i32 = undefined;
    {
        const rc = std.os.linux.pipe2(&pipe, std.os.linux.O_CLOEXEC);
        const err = std.os.linux.getErrno(rc);
        if (err != 0) return error.PipeCreateFailed;
    }

    var ev: std.os.linux.epoll_event = undefined;

    ev.events = std.os.linux.EPOLLIN | std.os.linux.EPOLLET;
    ev.data.fd = pipe[0];
    {
        const rc = std.os.linux.epoll_ctl(epollfd, std.os.linux.EPOLL_CTL_ADD, pipe[0], &ev);
        const err = std.os.linux.getErrno(rc);
        if (err != 0) return error.EpollCtlFailed;
    }

    const thread = try std.os.spawnThread(pipe[0], threadStart);

    var events: [10]std.os.linux.epoll_event = undefined;
    const nfds = blk: {
        std.debug.warn("epoll wait\n");
        const rc = std.os.linux.epoll_wait(epollfd, &events, @intCast(u32, events.len), -1);
        std.debug.warn("epoll wakeup\n");
        const err = std.os.linux.getErrno(rc);
        if (err != 0) return error.EpollWaitFailed;
        break :blk rc;
    };
    if (nfds != 1) return error.NotExactly1FileDescriptor;
    std.debug.assert(events[0].data.fd == pipe[0]);
    thread.wait();
}

fn threadStart(fd: i32) void {
    std.os.time.sleep(1, 0);
    std.debug.warn("close fd\n");
    std.os.close(fd);
}

它输出:

epoll wait
close fd
^C

(我不得不用 Ctrl+C 杀死它,因为它再也没有醒来。)

这是 strace 输出:

execve("./test", ["./test"], 0x7fff311ff208 /* 122 vars */) = 0
epoll_create1(EPOLL_CLOEXEC)            = 3
pipe2([4, 5], O_CLOEXEC)                = 0
epoll_ctl(3, EPOLL_CTL_ADD, 4, {EPOLLIN|EPOLLET, {u32=4, u64=4}}) = 0
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0) = 0x7fd082ed9000
clone(child_stack=0x7fd0836d8fd8, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID|0x400000, parent_tidptr=0x7fd0836d8fe4, child_tidptr=0x7fd0836d8fe4) = 13547
strace: Process 13547 attached
[pid 13546] write(2, "epoll wait\n", 11epoll wait
<unfinished ...>
[pid 13547] nanosleep({tv_sec=1, tv_nsec=0},  <unfinished ...>
[pid 13546] <... write resumed> )       = 11
[pid 13546] epoll_wait(3,  <unfinished ...>
[pid 13547] <... nanosleep resumed> 0x7fd0836d8f20) = 0
[pid 13547] write(2, "close fd\n", 9close fd
)   = 9
[pid 13547] close(4)                    = 0
[pid 13547] exit(0)                     = ?
[pid 13547] +++ exited with 0 +++
^Cstrace: Process 13546 detached

关于linux - 当 fd 关闭时,我会收到 epoll 的通知吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43034535/

相关文章:

redis - 如何降低 epoll_wait 调用的 finish_task_switch() 的 CPU 使用率?

c - Linux Evdev 投票滞后

c - 结构成员名称中的前缀

Linux:为什么 sed -e '1,244d' 不删除

c++ - gsoap wsdl2h 在 wcf wsdl 文件上失败

android - 如何处理这种情况 : andorid epoll_wait return -1 and errno=4 used ndk

linux - 在 centos 7 上同步确认后的 socket 鳍

linux - 在 Linux 上使用 Node 0.12.7 获取 "Unexpected reserved word"

linux - 如何使用curl检查文件是否下载

linux - UDP 数据包丢弃 - INErrors 与 .RcvbufErrors