Re: use alarm() to achieve timeout.



On Dec 16, 12:52 pm, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Dec 15, 7:57 pm, guanghui <hellog...@xxxxxxx> wrote:





alarm(5); // its signal handler is just used to interrupt recvfrom( )
if((n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL))<0){
   if(errno==EINTR)
       fprintf(stderr,"socket time out\n");
   else
         err_sys("recvfrom error");

}
what I'm confused is , because alarm and recvfrom is not atmoic, so
there is a window between them. When the system is really heavyloaded,
it's possible the following things would happen:
       1 alarm(5)
       2 other processed got executed
       3 our program is executed again, and recvfrom is going to be
called, but
          exactly before recvfrom is called, the SIGALRM is generated
and its signal
          handler is called, but the handler doesn't interrupt
recvfrom because
          recvfrom hasn't yet been called. the recvfrom got called and
because the
          alarm has expired so possibly we would block forever.

Is this something we couldn't avoid in case the system is heavyloaded,
or there is a way to solve this?

Your alarm handler should have handled the alarm, preventing the
'recvfrom' from ever getting called. If your alarm handler allowed the
control flow to go and call 'recvfrom', then it failed to handle the
problem.

DS- Hide quoted text -

- Show quoted text -

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() */
}
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( ).
.



Relevant Pages

  • Re: sntp hangs if network not available
    ... >setting up alarm for 3 secs ... >Before recvfrom ... Skip the bars and set-ups and start using Yahoo! ... Personals for free ...
    (comp.protocols.time.ntp)
  • 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)
  • use alarm() to achieve timeout.
    ... I am now reading UNP(unix network programming). ... from the book talking about achieving timeout using alarm() when doing ... // its signal handler is just used to interrupt recvfrom() ...
    (comp.os.linux.networking)
  • Re: timing out slow operations
    ... alarm 10; # schedule alarm in 10 seconds ... but that is how one makes code bomb-proof: ... expire. ... I suppose you should set up an alarm handler before going into the ...
    (comp.lang.perl.misc)
  • Re: use alarm() to achieve timeout.
    ... there is a window between them. ... Your alarm handler should have handled the alarm, ... 'recvfrom' from ever getting called. ... control flow to go and call 'recvfrom', then it failed to handle the ...
    (comp.os.linux.networking)