Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created February 17, 2018 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/044633aa7f112add7a236eb2696013d9 to your computer and use it in GitHub Desktop.
Save andrewrk/044633aa7f112add7a236eb2696013d9 to your computer and use it in GitHub Desktop.
test how epoll edge trigger works
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 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;
}
defer std.os.close(pipe[0]);
defer std.os.close(pipe[1]);
// put something in the write end
const args = try std.os.argsAlloc(std.debug.global_allocator);
if (args.len == 1) {
std.debug.warn("data in the pipe\n");
try std.os.posixWrite(pipe[1], "aoeoue");
} else {
std.debug.warn("no data in the pipe\n");
}
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;
}
var events: [10]std.os.linux.epoll_event = undefined;
const nfds = blk: {
const rc = std.os.linux.epoll_wait(epollfd, &events[0], u32(events.len), -1);
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]);
}
@andrewrk
Copy link
Author

output:

andy@xps ~/tmp> ./test
data in the pipe
andy@xps ~/tmp> ./test aoeu
no data in the pipe
^C⏎ 

so it will edge-trigger if there is already data.

@andrewrk
Copy link
Author

andrewrk commented Feb 17, 2018

andy@xps ~/tmp> strace ./test
execve("./test", ["./test"], [/* 62 vars */]) = 0
epoll_create1(EPOLL_CLOEXEC)            = 3
pipe2([4, 5], O_CLOEXEC)                = 0
write(2, "data in the pipe\n", 17data in the pipe
)      = 17
write(5, "aoeoue", 6)                   = 6
epoll_ctl(3, EPOLL_CTL_ADD, 4, {EPOLLIN|EPOLLET, {u32=0, u64=17179869184}}) = 0
epoll_wait(3, [{EPOLLIN, {u32=0, u64=17179869184}}], 10, -1) = 1
close(5)                                = 0
close(4)                                = 0
close(3)                                = 0
exit(0)                                 = ?
+++ exited with 0 +++
andy@xps ~/tmp> strace ./test aoeu
execve("./test", ["./test", "aoeu"], [/* 62 vars */]) = 0
epoll_create1(EPOLL_CLOEXEC)            = 3
pipe2([4, 5], O_CLOEXEC)                = 0
write(2, "no data in the pipe\n", 20no data in the pipe
)   = 20
epoll_ctl(3, EPOLL_CTL_ADD, 4, {EPOLLIN|EPOLLET, {u32=0, u64=17179869184}}) = 0
epoll_wait(3, ^Cstrace: Process 7153 detached
 <detached ...>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment