From: mauch AT uni-duisburg DOT de (Michael Mauch) Newsgroups: comp.os.msdos.djgpp Subject: Re: usleep() and 95dos problems Date: Fri, 25 Jul 1997 10:34:37 +0200 Organization: Home, sweet home (via Gesamthochschule Duisburg) Lines: 56 Message-ID: <5r9oet$q1d$1@news-hrz.uni-duisburg.de> References: NNTP-Posting-Host: ppp66.uni-duisburg.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On 24 Jul 1997 07:07:03 GMT, jmarin AT pyy DOT jmp DOT fi (Jukka Marin) wrote: > I'm new to djgpp (and dos programming in general :-) I'm trying to write > a packet protocol which requires small, but accurate time delays. I was > happy to find the usleep() call - and unhappy to find out it doesn't > work properly. Not on my hardware, at least. > > If I have a loop like this: > > while(1) { > printf("huu\n"); > for (i = 0; i < 1000; i++) usleep(1000); > } > > The printf() is being run as fast as it can, it seems - ie. the usleep() > call doesn't delay the execution at all. If I change the usleep argument > to 100000, I get a delay - but I need 1 ms delays, not 100 ms ones. usleep() does not provide 1 ms delay, because it is based on clock() which in turn has a resolution of 55 ms. You can use a homebrew version of usleep(), e.g.: #include #include "puclock.h" unsigned int pusleep(unsigned int _useconds) { uclock_t stop_time = puclock() + _useconds * (uclock_t)UCLOCKS_PER_SEC / 1e6; while (puclock() < stop_time) { __dpmi_yield(); } return 0; } This code uses the puclock() function posted here a few weeks ago. See this list's archive at www.delorie.com or drop me a line and I'll send it to you (ca. 3KB). uclock() uses the timer chip and does not work inside a Windows/Win95 DOS box, because Windows fiddles with the timer chip. puclock() overcomes this problem because it uses Virtual Timer Device if it runs inside a Windows DOS box. Regards... Michael -- Spammers: ask secretreports AT answerme DOT com for free info about how to explode your business. hoefner AT ddv DOT de wants to learn more about spam and UCE. Please help him.