linux - How do I use epoll? -
i know interest in file descriptor registered epoll_ctl, can not find info on how use epoll.
if want read , write files asynchronously, how do it? use normal read , write calls? call these functions before or after calling epoll_wait, , mark file descriptors used nonblock?
you're mixing concepts. epoll(7)
not same thing asynchronous i/o. select(2)
, poll(2)
, epoll(7)
, other similar functions can block whether or not underlying file descriptors blocking or non-blocking; provide synchronous form of notification - system doesn't tell until explicitly ask it.
the point of select(2)
, poll(2)
, family can i/o multiplexing: single blocking call, can wait interesting events in given set of file descriptors. not same asynchronous i/o.
posix asynchronous i/o, on other hand, uses aio control blocks describe i/o operations (struct aiocb
), , performed aio_read(3)
, aio_write(3)
, aio_return(3)
, aio_suspend(3)
. unless have reason this, don't - complicates design quite lot.
to use epoll(7)
, there's not besides can read in man 7 epoll
:
- create
epoll
instanceepoll_create(2)
or more recent variantepoll_create1(2)
. - register interest in file descriptors
epoll_ctl(2)
. - call
epoll_wait(2)
wait i/o event, possibly blocking calling thread until event available
once epoll_wait(2)
returns, assuming successful, guaranteed @ least 1 of operations of interest registered epoll_ctl(2)
not block. events not block returned epoll_wait(2)
in buffer fed (the second argument epoll_wait(2)
).
iterate through buffer , figure out want do. example, if registered interest in reading file descriptor , returned epoll_wait(2)
(the events
field entry includes epollin
), can call read(2)
knowing won't block. same goes true write(2)
, if registered interest in knowing when write(2)
won't block, , event returned epoll_wait(2)
.
so, yes, call read(2)
/ write(2)
after epoll_wait(2)
, make sure perform operation identified being available, don't block.
oh, , remember epoll(7)
linux-specific.
Comments
Post a Comment