Language clean ups

This commit is contained in:
Michael Kerrisk 2007-06-14 21:11:22 +00:00
parent 8666f20911
commit fc15f317eb
1 changed files with 75 additions and 74 deletions

View File

@ -18,7 +18,7 @@
.\" .\"
.\" Davide Libenzi <davidel@xmailserver.org> .\" Davide Libenzi <davidel@xmailserver.org>
.\" .\"
.TH EPOLL 7 2002-10-23 "Linux" "Linux Programmer's Manual" .TH EPOLL 7 2007-06-22 "Linux" "Linux Programmer's Manual"
.SH NAME .SH NAME
epoll \- I/O event notification facility epoll \- I/O event notification facility
.SH SYNOPSIS .SH SYNOPSIS
@ -27,8 +27,8 @@ epoll \- I/O event notification facility
.B epoll .B epoll
is a variant of is a variant of
.BR poll (2) .BR poll (2)
that can be used either as Edge or Level Triggered interface and scales that can be used either as an edge-triggered or a level-triggered
well to large numbers of watched fds. interface and scales well to large numbers of watched file descriptors.
Three system calls are provided to Three system calls are provided to
set up and control an set up and control an
.B epoll .B epoll
@ -45,36 +45,36 @@ Interest for certain file descriptors is then registered via
.BR epoll_ctl (2). .BR epoll_ctl (2).
Finally, the actual wait is started by Finally, the actual wait is started by
.BR epoll_wait (2). .BR epoll_wait (2).
.SS Level Triggered and Edge Triggered .SS Level-Triggered and Edge-Triggered
The The
.B epoll .B epoll
event distribution interface is able to behave both as Edge Triggered event distribution interface is able to behave both as edge-triggered
( ET ) and Level Triggered ( LT ). (ET) and level-triggered (LT).
The difference between ET and LT The difference between ET and LT
event distribution mechanism can be described as follows. event distribution mechanism can be described as follows.
Suppose that Suppose that
this scenario happens : this scenario happens :
.TP .TP
.B 1 .B 1
The file descriptor that represents the read side of a pipe ( The file descriptor that represents the read side of a pipe
.B RFD .RI ( rfd )
) is added inside the is added inside the
.B epoll .B epoll
device. device.
.TP .TP
.B 2 .B 2
Pipe writer writes 2Kb of data on the write side of the pipe. A pipe writer writes 2Kb of data on the write side of the pipe.
.TP .TP
.B 3 .B 3
A call to A call to
.BR epoll_wait (2) .BR epoll_wait (2)
is done that will return is done that will return
.B RFD .I rfd
as ready file descriptor. as a ready file descriptor.
.TP .TP
.B 4 .B 4
The pipe reader reads 1Kb of data from The pipe reader reads 1Kb of data from
.BR RFD . .IR rfd .
.TP .TP
.B 5 .B 5
A call to A call to
@ -82,7 +82,7 @@ A call to
is done. is done.
.PP .PP
If the If the
.B RFD .I rfd
file descriptor has been added to the file descriptor has been added to the
.B epoll .B epoll
interface using the interface using the
@ -91,17 +91,18 @@ flag, the call to
.BR epoll_wait (2) .BR epoll_wait (2)
done in step done in step
.B 5 .B 5
will probably hang because of the available data still present in the file will probably hang despite the available data still present in the file
input buffers and the remote peer might be expecting a response based on the input buffer;
meanwhile the remote peer might be expecting a response based on the
data it already sent. data it already sent.
The reason for this is that Edge Triggered event The reason for this is that edge-triggered event
distribution delivers events only when events happens on the monitored file. distribution delivers events only when events happens on the monitored file.
So, in step So, in step
.B 5 .B 5
the caller might end up waiting for some data that is already present inside the caller might end up waiting for some data that is already present inside
the input buffer. the input buffer.
In the above example, an event on In the above example, an event on
.B RFD .I rfd
will be generated because of the write done in will be generated because of the write done in
.BR 2 .BR 2
and the event is consumed in and the event is consumed in
@ -112,19 +113,18 @@ does not consume the whole buffer data, the call to
.BR epoll_wait (2) .BR epoll_wait (2)
done in step done in step
.B 5 .B 5
might lock indefinitely. might block indefinitely.
The
.B epoll An application that employs the
interface, when used with the
.B EPOLLET .B EPOLLET
flag ( Edge Triggered ) flag (edge-triggered)
should use non-blocking file descriptors to avoid having a blocking should use non-blocking file descriptors to avoid having a blocking
read or write starve the task that is handling multiple file descriptors. read or write starve a task that is handling multiple file descriptors.
The suggested way to use The suggested way to use
.B epoll .B epoll
as an Edge Triggered as an edge-triggered
.RB ( EPOLLET ) .RB ( EPOLLET )
interface is below, and possible pitfalls to avoid follow. interface is as follows:
.RS .RS
.TP .TP
.B i .B i
@ -138,15 +138,16 @@ or
return EAGAIN return EAGAIN
.RE .RE
.PP .PP
On the contrary, when used as a Level Triggered interface, By contrast, when used as a level-triggered interface,
.B epoll .B epoll
is by all means a faster is simplay a faster
.BR poll (2), .BR poll (2),
and can be used wherever the latter is used since it shares the and can be used wherever the latter is used since it shares the
same semantics. same semantics.
Since even with the Edge Triggered
Since even with the edge-triggered
.B epoll .B epoll
multiple events can be generated up on receipt of multiple chunks of data, multiple events can be generated upon receipt of multiple chunks of data,
the caller has the option to specify the the caller has the option to specify the
.B EPOLLONESHOT .B EPOLLONESHOT
flag, to tell flag, to tell
@ -156,17 +157,17 @@ to disable the associated file descriptor after the receipt of an event with
When the When the
.B EPOLLONESHOT .B EPOLLONESHOT
flag is specified, flag is specified,
it is caller responsibility to rearm the file descriptor using it is the caller's responsibility to rearm the file descriptor using
.BR epoll_ctl (2) .BR epoll_ctl (2)
with with
.BR EPOLL_CTL_MOD . .BR EPOLL_CTL_MOD .
.SS Example for Suggested Usage .SS Example for Suggested Usage
While the usage of While the usage of
.B epoll .B epoll
when employed like a Level Triggered interface does have the same when employed as a level-triggered interface does have the same
semantics of semantics as
.BR poll (2), .BR poll (2),
an Edge Triggered usage requires more clarification to avoid stalls the edge-triggered usage requires more clarification to avoid stalls
in the application event loop. in the application event loop.
In this example, listener is a In this example, listener is a
non-blocking socket on which non-blocking socket on which
@ -177,7 +178,7 @@ file descriptor until EAGAIN is returned by either
.BR read (2) .BR read (2)
or or
.BR write (2). .BR write (2).
An event driven state machine application should, after having received An event-driven state machine application should, after having received
EAGAIN, record its current state so that at the next call to do_use_fd() EAGAIN, record its current state so that at the next call to do_use_fd()
it will continue to it will continue to
.BR read (2) .BR read (2)
@ -214,12 +215,11 @@ for(;;) {
} }
.fi .fi
When used as an Edge triggered interface, for performance reasons, it is When used as an edge-triggered interface, for performance reasons, it is
possible to add the file descriptor inside the epoll interface ( possible to add the file descriptor inside the epoll interface
.B EPOLL_CTL_ADD .RB ( EPOLL_CTL_ADD )
) once by specifying ( ) once by specifying
.BR EPOLLIN | EPOLLOUT .RB ( EPOLLIN | EPOLLOUT ).
).
This allows you to avoid This allows you to avoid
continuously switching between continuously switching between
.B EPOLLIN .B EPOLLIN
@ -232,31 +232,30 @@ with
.SS Questions and Answers .SS Questions and Answers
.TP .TP
.B Q1 .B Q1
What happens if you add the same fd to an epoll_set twice? What happens if you add the same file descriptor to an epoll_set twice?
.TP .TP
.B A1 .B A1
You will probably get EEXIST. You will probably get EEXIST.
However, it is possible that two However, it is possible that two
threads may add the same fd twice. threads may add the same file descriptor twice.
This is a harmless condition. This is a harmless condition.
.TP .TP
.B Q2 .B Q2
Can two Can two
.B epoll .B epoll
sets wait for the same fd? sets wait for the same file descriptor?
If so, are events reported to both If so, are events reported to both
.B epoll .B epoll
sets fds? file descriptors?
.TP .TP
.B A2 .B A2
Yes. Yes, and events would be reported to both.
However, it is not recommended. However, it is not recommended.
Yes it would be reported to both.
.TP .TP
.B Q3 .B Q3
Is the Is the
.B epoll .B epoll
fd itself poll/epoll/selectable? file descriptor itself poll/epoll/selectable?
.TP .TP
.B A3 .B A3
Yes. Yes.
@ -264,24 +263,24 @@ Yes.
.B Q4 .B Q4
What happens if the What happens if the
.B epoll .B epoll
fd is put into its own fd set? file descriptor is put into its own file descriptor set?
.TP .TP
.B A4 .B A4
It will fail. It will fail.
However, you can add an However, you can add an
.B epoll .B epoll
fd inside another epoll fd set. file descriptor inside another epoll file descriptor set.
.TP .TP
.B Q5 .B Q5
Can I send the Can I send the
.B epoll .B epoll
fd over a unix-socket to another process? file descriptor over a unix-socket to another process?
.TP .TP
.B A5 .B A5
No. No.
.TP .TP
.B Q6 .B Q6
Will the close of an fd cause it to be removed from all Will closing a file descriptor cause it to be removed from all
.B epoll .B epoll
sets automatically? sets automatically?
.TP .TP
@ -289,7 +288,7 @@ sets automatically?
Yes. Yes.
.TP .TP
.B Q7 .B Q7
If more than one event comes in between If more than one event occurs between
.BR epoll_wait (2) .BR epoll_wait (2)
calls, are they combined or reported separately? calls, are they combined or reported separately?
.TP .TP
@ -297,19 +296,20 @@ calls, are they combined or reported separately?
They will be combined. They will be combined.
.TP .TP
.B Q8 .B Q8
Does an operation on an fd affect the already collected but not yet reported Does an operation on a file descriptor affect the
events? already collected but not yet reported events?
.TP .TP
.B A8 .B A8
You can do two operations on an existing fd. You can do two operations on an existing file descriptor.
Remove would be meaningless for Remove would be meaningless for
this case. this case.
Modify will re-read available I/O. Modify will re-read available I/O.
.TP .TP
.B Q9 .B Q9
Do I need to continuously read/write an fd until EAGAIN when using the Do I need to continuously read/write a file descriptor
until EAGAIN when using the
.B EPOLLET .B EPOLLET
flag ( Edge Triggered behavior ) ? flag (edge-triggered behavior) ?
.TP .TP
.B A9 .B A9
No you don't. No you don't.
@ -322,26 +322,26 @@ next EAGAIN.
When and how you will use such file descriptor is entirely up When and how you will use such file descriptor is entirely up
to you. to you.
Also, the condition that the read/write I/O space is exhausted can 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 be detected by checking the amount of data read from / written to the target
file descriptor. file descriptor.
For example, if you call For example, if you call
.BR read (2) .BR read (2)
by asking to read a certain amount of data and by asking to read a certain amount of data and
.BR read (2) .BR read (2)
returns a lower number of bytes, you can be sure to have exhausted the read returns a lower number of bytes,
you can be sure of having exhausted the read
I/O space for such file descriptor. I/O space for such file descriptor.
Same is valid when writing using the The same is true when writing using the
.BR write (2) .BR write (2).
function.
.SS Possible Pitfalls and Ways to Avoid Them .SS Possible Pitfalls and Ways to Avoid Them
.TP .TP
.B o Starvation ( Edge Triggered ) .B o Starvation (edge-triggered)
.PP .PP
If there is a large amount of I/O space, If there is a large amount of I/O space,
it is possible that by trying to drain it is possible that by trying to drain
it the other files will not get processed causing starvation. it the other files will not get processed causing starvation.
This is not specific to (This problem is not specific to
.BR epoll . .BR epoll .)
.PP .PP
The solution is to maintain a ready list The solution is to maintain a ready list
and mark the file descriptor as ready and mark the file descriptor as ready
@ -349,32 +349,33 @@ in its associated data structure, thereby allowing the application to
remember which files need to be processed but still round robin amongst remember which files need to be processed but still round robin amongst
all the ready files. all the ready files.
This also supports ignoring subsequent events you This also supports ignoring subsequent events you
receive for fd's that are already ready. receive for file descriptors that are already ready.
.TP .TP
.B o If using an event cache... .B o If using an event cache...
.PP .PP
If you use an event cache or store all the fd's returned from If you use an event cache or store all the file descriptors returned from
.BR epoll_wait (2), .BR epoll_wait (2),
then make sure to provide a way to mark then make sure to provide a way to mark
its closure dynamically (ie- caused by its closure dynamically (i.e., caused by
a previous event's processing). a previous event's processing).
Suppose you receive 100 events from Suppose you receive 100 events from
.BR epoll_wait (2), .BR epoll_wait (2),
and in event #47 a condition causes event #13 to be closed. and in event #47 a condition causes event #13 to be closed.
If you remove the structure and If you remove the structure and
.BR close (2) .BR close (2)
the fd for event #13, then your the file descriptor for event #13, then your
event cache might still say there are events waiting for that fd causing event cache might still say there are events waiting for that
confusion. file descriptor causing confusion.
.PP .PP
One solution for this is to call, during the processing of event 47, One solution for this is to call, during the processing of event 47,
.BR epoll_ctl ( EPOLL_CTL_DEL ) .BR epoll_ctl ( EPOLL_CTL_DEL )
to delete fd 13 and to delete file descriptor 13 and
.BR close (2), .BR close (2),
then mark its associated then mark its associated
data structure as removed and link it to a cleanup list. data structure as removed and link it to a cleanup list.
If you find another If you find another
event for fd 13 in your batch processing, you will discover the fd had been event for file descriptor 13 in your batch processing,
you will discover the file descriptor had been
previously removed and there will be no confusion. previously removed and there will be no confusion.
.SH VERSIONS .SH VERSIONS
.BR epoll (7) .BR epoll (7)