From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 In-Reply-To: <99a6e3a7cd4a1a6da9d06b97ddff4e37@gmx.de> References: <959ad71b3d677ea98c0e6b2ef28db2dd@quanstro.net> <99a6e3a7cd4a1a6da9d06b97ddff4e37@gmx.de> Date: Sun, 22 Mar 2009 21:04:54 -0700 Message-ID: <13426df10903222104m424280d7ub9503f035eb708cb@mail.gmail.com> From: ron minnich To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [9fans] fossil periodic thread does zero sleep() Topicbox-Message-UUID: c210f0e6-ead4-11e9-9d60-3106f5b1d025 This is from mobile so I can not look at code much but if you are converting nanoseconds to milliseconds you multiply by 1e6 not 1e-6 I think. Ron On 3/22/09, cinap_lenrek@gmx.de wrote: > This is actualy very interesting. Riped the parts from periodic.c > to play a with the code to see how it reacts to some changes. > > The code below reproduces the problem: > > sleep()+0x7 /sys/src/libc/9syscall/sleep.s:5 > periodicThread(msec=0x3e8)+0xb2 /tmp/a.c:21 > ct=0x47a68e5b > t=0x47e50e4d > ts=0x0 > main()+0x10 /tmp/a.c:32 > _main+0x31 /sys/src/libc/386/main9.s:16 > > > The zerosleeps go away if one uncomments the foo print. It also > goes away if one makes the sleep one milli second longer by > changing ts to ts+1. > > I would love if anybody gives a good explaination of this bug > and how to fix it :-) > > #include > #include > > static void > periodicThread(int msec) > { > double t, ct, ts; > > ct = nsec()*1e-6; > t = ct + msec; > > for(;;){ > /* skip missed */ > while(t <= ct) > t += msec; > > ts = t - ct; > if(ts > 1000) > ts = 1000; > sleep(ts); > ct = nsec()*1e-6; > if(t <= ct){ > // print("foo!\n"); > t += msec; > } > } > } > > void > main(int argc, char *argv[]) > { > periodicThread(1000); > } > -- Sent from my mobile device