Re: use alarm() to achieve timeout.



On Dec 17, 12:18 am, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Dec 15, 9:13 pm, guanghui <hellog...@xxxxxxx> wrote:

sorry, I think I forgot something.
the signal handler is registered with sa_flags |=SA_INTERRUPT;
and the following is the hander:
void sig_alrm(int signo)
{
return; /* just interrupt the recvfrom() */}

There's your bug. How do you know that will interrupt the 'recvfrom'?

and when data arrived before SIGALRM is generated , we simply
turn off the alarm using alarm(0);

what I really mean is "alarm( ) and recvfrom( )" should be an
atomic operation, when SIGALRM is generated and caught,
the program is blocked inside the recvfrom system call so
the handler would interrupt recvfrom( ).

You are correct, they should be atomic. However, your code doesn't
make them behave atomically, so your code is broken.

The correct fix is for the signal handler to *HANDLE* the signal, not
just ignore it. Handling the signal requires ensuring that the program
control flow does not go the wrong way.

DS

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)
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?
.



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.
    ... implementation of timeout using alarm() from ... the signal handler will ensure 'setjmp' does not return zero. ...
    (comp.os.linux.networking)
  • Re: Controlling PCF8583 clock chip with a PIC
    ... >> chip is working fine. ... no matter what type of alarm I instruct the chip to enable ... >> by checking the interrupt output pin of the chip with a scope. ...
    (comp.arch.embedded)
  • Re: How to maintain time in a C program
    ... I'd recommend you use them. ... period of time using setitimer or alarm. ... interrupt it after the specified interval. ...
    (comp.lang.c)
  • Re: use alarm() to achieve timeout.
    ... How do you know that will interrupt the 'recvfrom'? ... turn off the alarm using   alarm; ... the program is blocked inside the recvfrom system call so ... The correct fix is for the signal handler to *HANDLE* the signal, ...
    (comp.os.linux.networking)