Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site heurikon.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!uwvax!heurikon!jeff From: jeff@heurikon.UUCP (Jeffrey Mattox) Newsgroups: net.unix-wizards,net.sources Subject: When does ( alarm(1) == alarm(INFINITY) ) ? Message-ID: <132@heurikon.UUCP> Date: Wed, 6-Mar-85 02:19:59 EST Article-I.D.: heurikon.132 Posted: Wed Mar 6 02:19:59 1985 Date-Received: Thu, 7-Mar-85 05:19:16 EST Reply-To: jeff@heurikon.UUCP (Jeffrey Mattox) Organization: Heurikon Corp., Madison WI Lines: 132 Xref: watmath net.unix-wizards:12324 net.sources:2666 Answer: When you least expect it and, thanks to Murphy, when you least want it. In fact, there is a small, but non-zero, probability that: alarm(ANYTHING) == alarm(INFINITY). We came across this little kernel bug the other day when trying to figure out why certain programs were hanging on a simple "sleep(1);" statement. Although this happens on System V, I've been told this problem is common to most non-BSD UNIXes. sleep(arg) /* A simplified view of the sleep subroutine */ { ... alarm(arg); /* sets p_clktim=arg in proc table */ ... /* a critical time */ pause(); /* waits for SIGALRM: --pp->p_clktim == 0 */ } If an alarm(1) is executed *just* prior to a one second time tick, and if the time tick occurs before the pause(), then the pp->p_clktim value hits zero in clock() before the pause() is done, and the alarm signal will be missed by the pause. This results in an INFINITE sleep. If the process is suspended for more than one second prior to the pause, then alarms longer than one second could hang, too. We'd welcome suggestions on how to fix this problem; the simpler, the better (although the simpler, the-less-likely-it-will-fix-it). See for yourself. Here's an adaptive program which encourages a sleep(1) hang. You may need to manually adjust the initial loop count estimation algorithm, or enter an initial value as an argument. The ease with which this hangs is scary. ------------------------------cut here------------------------------- /* slphang.c */ #include#include /* This program is an adaptive sleep(1) tester. When called * without an argument, the logic first runs one loop over a one * second period to estimate the initial loop count value. If there * is an argument, it is taken to be a decimal value which is used * as the initial delay loop counter. On our system, a value of * 55000 is a good starting point. * * Sleep(1)'s are done and the loop count is increased or decreased * depending on whether or not the delay loop took more or less than * one full second to execute. The target is to loop until *just* * prior to a one second clock tick, hoping to set the alarm value * and have the clock tick occur before the sleep() does a pause(). * * While running, a SIGINT will print out the current loop delay value. * A "+" will print if the delay loop is being increased; a "-" if the * delay is being decreased. The program can be run in the background. * Ideally, the system should be "quiet" when executing this so the * adaption will work. Restart the program if it doesn't adapt (hang) * within one minute. * * Expect a bunch of "+" marks to start; if it starts with "-", it may * not converge. If you get alternating "+" and "-" you're close, or * your system is too busy for this to work. When you don't get * either (+ or -) for more than two seconds, it's hung. * * The ideal output should look something like this: * "++++++---+--+-+--++-+-" * * A SIGINT will cause the delay value to be printed and also will * "unhang" the sleep, so the program will continue printing "+" * and "-" until another hang. * * Use SIGQUIT to exit. */ int delay; short alrmflag,dummy; main(argc,argv) int argc; char *argv[]; { int onintr(); register int i; int delta; long time1,time2; short lastflag = 0; if ( argc > 1 ) delay = atoi(argv[1]); signal(SIGINT,onintr); signal(SIGALRM,onintr); sleep(1); /* synchronize to the clock, hope we don't hang here! */ if ( delay == 0 ) { /* argument? */ alarm(1); /* set alrmflag in one second */ for (i=0; i<999999 && !alrmflag ; i++); delay = i - i/3 - i/7; /* make a guess */ } delta = delay/12; /* initial adaptive +- increment */ time(&time2); /* init time2 */ while (1) { time1 = time2; /* last loop time */ for (i=0; i 1 ) { /* using too big a delay */ if ( lastflag == 1 ) { /* want two in a row */ delta = (delta/2) | 1; /* decrease delta */ delay -= delta; /* adapt */ lastflag = 0; } else { lastflag = 1; } putc('-',stderr); } else { /* using too small a delay */ delay += delta; /* adapt */ putc('+',stderr); lastflag = 0; } } } onintr(sig) { if ( sig == SIGALRM ) { alrmflag = 1; return; } signal(sig,onintr); printf("\ndly=%d\n",delay); } ---- /"""\ Jeffrey Mattox, Heurikon Corp, Madison, WI |O.O| {harpo, hao, philabs}!seismo!uwvax!heurikon!jeff (news & mail) \_=_/ ihnp4!heurikon!jeff (mail - best)