Re: use alarm() to achieve timeout.
- From: guanghui <helloghui@xxxxxxx>
- Date: Mon, 15 Dec 2008 21:13:52 -0800 (PST)
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( ).
.
- Follow-Ups:
- Re: use alarm() to achieve timeout.
- From: David Schwartz
- Re: use alarm() to achieve timeout.
- References:
- use alarm() to achieve timeout.
- From: guanghui
- Re: use alarm() to achieve timeout.
- From: David Schwartz
- use alarm() to achieve timeout.
- Prev by Date: Re: use alarm() to achieve timeout.
- Next by Date: Apache-Proxyblock.conf -Need to block iGoogle without blocking Google
- Previous by thread: Re: use alarm() to achieve timeout.
- Next by thread: Re: use alarm() to achieve timeout.
- Index(es):
Relevant Pages
|