当前位置: 技术问答>linux和unix
socket关闭是是否需要从epoll里面移出
来源: 互联网 发布时间:2016-04-11
本文导语: 将socket添加到epoll中,关闭socket时是否需要从epoll中移出。epool是否会将其主动移出?对socket close后不从epoll中移出是否会有影响? | 你的问题是Q6,答案是A6 QUESTIONS AND ANSWERS (from linux-kernel) ...
将socket添加到epoll中,关闭socket时是否需要从epoll中移出。epool是否会将其主动移出?对socket close后不从epoll中移出是否会有影响?
|
你的问题是Q6,答案是A6
QUESTIONS AND ANSWERS (from linux-kernel)
Q1 What happens if you add the same fd to an epoll_set twice?
A1 You will probably get EEXIST. However, it is possible that two threads may add the same fd twice. This
is a harmless condition.
Q2 Can two epoll sets wait for the same fd? If so, are events reported to both epoll sets fds?
A2 Yes. However, it is not recommended. Yes it would be reported to both.
Q3 Is the epoll fd itself poll/epoll/selectable?
A3 Yes.
Q4 What happens if the epoll fd is put into its own fd set?
A4 It will fail. However, you can add an epoll fd inside another epoll fd set.
Q5 Can I send the epoll fd over a unix-socket to another process?
A5 No.
Q6 Will the close of an fd cause it to be removed from all epoll sets automatically?
A6 Yes.
Q7 If more than one event comes in between epoll_wait(2) calls, are they combined or reported separately?
A7 They will be combined.
Q8 Does an operation on an fd affect the already collected but not yet reported events?
A8 You can do two operations on an existing fd. Remove would be meaningless for this case. Modify will
re-read available I/O.
Q9 Do I need to continuously read/write an fd until EAGAIN when using the EPOLLET flag (
Edge-Triggered behaviour ) ?
A9 No you donu2019t. Receiving an event from epoll_wait(2) should suggest to you that such file
descriptor is ready for the requested I/O operation. You have simply to consider it ready until you
will receive the next EAGAIN. When and how you will use such file descriptor is entirely up to you.
Also, the condition that the read/write I/O space is exhausted can be detected by checking the
amount of data read/write from/to the target file descriptor. For example, if you call read(2) by asking
to read a certain amount of data and read(2) returns a lower number of bytes, you can be sure to have
exhausted the read I/O space for such file descriptor. Same is valid when writing using the write(2)
function.
QUESTIONS AND ANSWERS (from linux-kernel)
Q1 What happens if you add the same fd to an epoll_set twice?
A1 You will probably get EEXIST. However, it is possible that two threads may add the same fd twice. This
is a harmless condition.
Q2 Can two epoll sets wait for the same fd? If so, are events reported to both epoll sets fds?
A2 Yes. However, it is not recommended. Yes it would be reported to both.
Q3 Is the epoll fd itself poll/epoll/selectable?
A3 Yes.
Q4 What happens if the epoll fd is put into its own fd set?
A4 It will fail. However, you can add an epoll fd inside another epoll fd set.
Q5 Can I send the epoll fd over a unix-socket to another process?
A5 No.
Q6 Will the close of an fd cause it to be removed from all epoll sets automatically?
A6 Yes.
Q7 If more than one event comes in between epoll_wait(2) calls, are they combined or reported separately?
A7 They will be combined.
Q8 Does an operation on an fd affect the already collected but not yet reported events?
A8 You can do two operations on an existing fd. Remove would be meaningless for this case. Modify will
re-read available I/O.
Q9 Do I need to continuously read/write an fd until EAGAIN when using the EPOLLET flag (
Edge-Triggered behaviour ) ?
A9 No you donu2019t. Receiving an event from epoll_wait(2) should suggest to you that such file
descriptor is ready for the requested I/O operation. You have simply to consider it ready until you
will receive the next EAGAIN. When and how you will use such file descriptor is entirely up to you.
Also, the condition that the read/write I/O space is exhausted can be detected by checking the
amount of data read/write from/to the target file descriptor. For example, if you call read(2) by asking
to read a certain amount of data and read(2) returns a lower number of bytes, you can be sure to have
exhausted the read I/O space for such file descriptor. Same is valid when writing using the write(2)
function.