I want to use alarm()
to set the timeout of recvfrom
. But found that when use signal()
to register a handler for SIGALRM
, and a SIGALRM
has been captured and then invoked the signal handler. But after returned from handler, the recvfrom()
still blocks while there is no data coming and there is no EINTR
error. Why? Does the signal()
set the SA_RESTART
flag automatically?
Here is the code:
signal(SIGALRM, sig_handler);
while(1)
{
alarm(5);
n = recvfrom(sock, buf, BUF_MAX, 0, (struct sockaddr*)&addr, &len);
if(n < 0)
{
if(errno == EINTR)
{
printf("recvfrom timeoutn");
continue;
}
else
{
printf("recvfrom errorn");
}
}
else
{
printf("data: %sn", buf);
alarm(0);
}
}
void sig_handler(int signo)
{
return;
}
Aucun commentaire:
Enregistrer un commentaire