Reference:
EINTR and What It Is Good For Martin Sústrik, zeromq
Before we dive, this concept is well mentioned in Richard Stevens's UNIX Network Programming - Ch.20.5, thus Martin Sústrik's blog post can be considered as a recap of EINTR error.
Then restart the blocking function.
Additionally, If you are implementing a blocking function yourself, take care to return EINTR when you encounter a signal.
EINTR and What It Is Good For Martin Sústrik, zeromq
Before we dive, this concept is well mentioned in Richard Stevens's UNIX Network Programming - Ch.20.5, thus Martin Sústrik's blog post can be considered as a recap of EINTR error.
Rule of thumb:
When handling EINTR error, check any conditions that may have been altered by signal handlers.Then restart the blocking function.
Additionally, If you are implementing a blocking function yourself, take care to return EINTR when you encounter a signal.
Beware those 2 POSIX functions which don't honor EINTR
- pthread_cond_wait
From the emit event side should honor EINTR. or use sem_wait - pthread_mutex_lock
Not usually a problem due to lock will be unlocked soon.
Consider this code:
Above is the reason POSIX has EINTR error.
Modify code to this:
noted that to make blocking functions like recv return EINTR you may have to use sigaction() with SA_RESTART set to zero instead of signal() on some operating systems.
But, this isn't a graceful shutdown.
We have to exhaust the incoming message before exit.
When you press Ctrl+C, program exits performing the clean-up beforehand.
The morale of this story is that common advice to just restart the blocking function when EINTR is returned doesn't quite work:
Even EINTR is not completely water-proof, check this code:
Ultimate solution
use pselect, which mask the signals before calling pselect, and allow signal to pass during the pselect(which if signal occurs, pselect returns).select
nfds should be n + 1 (exclusive bound), this optimizing the linear check of fds.
Be sure to check the definition under what conditions is a Descriptor ready for network FDs.
Notice that when an error occurs on a socket, both readable and writable is marked by select.
Although the timeval structure lets us specify a resolution in microseconds, the actual resolution supported by the kernel is often more coarse.
Many Unix kernels round the timeout value up to a multiple of 10ms. There is also a scheduling latency involved, meaning it takes some time after the timer expires before the kernel schedules this process to run.
pselect
example:
poll
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.