From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Lee Chin" Subject: Re: usleep Date: Wed, 11 Jun 2003 19:53:24 -0500 Sender: linux-newbie-owner@vger.kernel.org Message-ID: <20030612005324.51946.qmail@mail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Return-path: Content-Disposition: inline List-Id: Content-Type: text/plain; charset="us-ascii" To: hahn@physics.mcmaster.ca Cc: linux-newbie@vger.kernel.org Thanks... that helpes (I initially posed my code wrong... my loop I intended to post was a for(i = 0; i < 1000; i++) Any way... what is setrealtime? I dont have it on my linux machine Thanks Lee ----- Original Message ----- From: Mark Hahn Date: Wed, 11 Jun 2003 19:45:17 -0400 (EDT) To: Lee Chin Subject: Re: usleep > > I would think the following code would wait for 1 second each itteration > > before printing hello, > > well, usleep takes microseconds, so as you've written it, > you should expect it to take 1ms per usleep or 50ms between hello's. > further, the man page doesn't claim that it'll actually sleep > for exactly the specified microseconds. > > here: > #include > #include > int main() { > int c = 20; > while (c--) { > int i; > printf("hello\n"); > for(i = 0; i < 50; i++) { > usleep(1000); > } > } > return 0; > } > > (not that a correct program needs both the stdio and a return from main.) > > | [hahn@hahn hahn]$ /usr/bin/time ./chin > | hello > ... > | hello > | 0.00user 0.00system 0:19.99elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k > | 0inputs+0outputs (71major+10minor)pagefaults 0swaps > | [hahn@hahn hahn]$ setrealtime time ./chin > | hello > ... > | hello > | 0.13user 0.87system 0:01.00elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k > | 0inputs+0outputs (71major+10minor)pagefaults 0swaps > > hmm, so on its own, 20 iterations of your loop take 20 seconds, > one second per iteration, and the usleep is actually lasting 20ms. > this is the traditional behavior (and correct, since the man page > permits this kind of fuzziness). > > if run in realtime mode, 20*50 usleep(1000)'s take 1.00 seconds, cool. > > also, if I add: > #include > > void myusleep(unsigned us) { > struct timeval tv; > tv.tv_sec = 0; > tv.tv_usec = us; > select(0,0,0,0,&tv); > } > > then it behaves pretty much exactly as expected. (good code would handle > the case where us>1000000 and where select is interrupted by a signal.) > > -- __________________________________________________________ Sign-up for your own FREE Personalized E-mail at Mail.com http://www.mail.com/?sr=signup - To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.linux-learn.org/faqs