Re: use alarm() to achieve timeout.



On Dec 16, 7:27 pm, hellog...@xxxxxxxxx wrote:

Thanks for your reply. So you mean what I concern is right and the
implementation of timeout using alarm() from
UNP(<<unix network programming ,3rd, by Richard stevens>>)
is not perfect(the code I wrote above is from UNP)

You are correct. That code has a race condition.

 just because of the small window between alarm() and recvfrom().
then could you please show me a correct solution to implement
timeout using alarm() when doing socket i/o?

I don't have the code handy, but the basic idea is to have the signal
handler call longjmp. You only call the function you want to timeout
if 'setjmp' returns zero. Since the signal handler calls 'longjmp',
the signal handler will ensure 'setjmp' does not return zero. So there
is no way you can call 'recvfrom' (or whatever) after the signal has
fired.

DS
.



Relevant Pages

  • Re: use alarm() to achieve timeout.
    ... implementation of timeout using alarm() from ... the signal handler will ensure 'setjmp' does not return zero. ...
    (comp.os.linux.networking)
  • Re: use alarm() to achieve timeout.
    ... How do you know that will interrupt the 'recvfrom'? ... The correct fix is for the signal handler to *HANDLE* the signal, ... implementation of timeout using alarm() from ...
    (comp.os.linux.networking)
  • Re: Time out question
    ... There are quite a lot of problems with alarm() ... from time_limit import time_limit, TimeOut ... raise TimeOut() ... # Install new handler remembering old ...
    (comp.lang.python)
  • Re: Bash: command output to variable
    ... ReadMode "normal"; ... # reads key with a timeout of 5 seconds without echo ... alarm $timeout; ... # if the Perl syntax error message string exists and is not ...
    (comp.unix.shell)
  • Re: Sockets Redirection problem
    ... setup a signal to break it, but signal handler is ... There is exactly one simple way to implement such a timeout without ... void alrm_handler ... 'timeout processing' will be done unless the running timer is ...
    (comp.os.linux.development.apps)