lkml.org 
[lkml]   [1997]   [May]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: Question on handling SIGALRM.
Date
...Richard B. Johnson says...:
> void handler(int sig)
> {
> global_variable= TRUE;
> }
>
> signal(SIGALRM, handler);
> global_variable = FALSE;
> alarm(timeout);
> if(global_variable == FALSE)
> status = read(fd, buffer, len);
> else
> ......

ahm... don't do this.
you only shorten the race.
If you must do this, Alan Cox sent a much better (and
correct solution) to this list that went something like

void handler(int sig)
{
siglongjump(stuff);
}

...
signal(SIGALRM, handler);
if (setlongjump(stuff))
return TIMEOUT;
alarm(timeout);
status=read(fd, buffer, len);
alarm(0);
................
There is still a race, but now it is between the return
from the read and the alarm(0), which may cause a false
error. You might be able to avoid it, but I don't know how.

... btw, the code fragment above is from memory, and almost
certainly misuses functions, but the idea should be right.
(use the man pages)

This should be in a faq somewhere...
-gordo

\
 
 \ /
  Last update: 2005-03-22 13:39    [W:0.039 / U:0.904 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site