All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lee Chin" <leechin@mail.com>
To: hahn@physics.mcmaster.ca
Cc: linux-newbie@vger.kernel.org
Subject: Re: usleep
Date: Wed, 11 Jun 2003 19:53:24 -0500	[thread overview]
Message-ID: <20030612005324.51946.qmail@mail.com> (raw)

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 <hahn@physics.mcmaster.ca>
Date: Wed, 11 Jun 2003 19:45:17 -0400 (EDT)
To: Lee Chin <leechin@mail.com>
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 <unistd.h>
> #include <stdio.h>
> 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 <sys/time.h>
> 
> 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

             reply	other threads:[~2003-06-12  0:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-12  0:53 Lee Chin [this message]
2003-06-12  1:02 ` usleep Mark Hahn
  -- strict thread matches above, loose matches on Subject: below --
2003-06-12 20:43 usleep Lee Chin
2003-06-12  1:04 usleep Lee Chin
2003-06-12  0:42 usleep Lee Chin
2003-06-11 22:55 usleep Lee Chin
2003-06-11 16:08 ` usleep Alan Bort
2003-06-11 23:21 ` usleep CaT
2003-06-11 23:45 ` usleep Mark Hahn
2003-06-11 23:55 ` usleep Ray Olszewski
2003-06-12  0:17   ` usleep Mark Hahn
     [not found]   ` <Pine.LNX.4.44.0306112015380.20310-100000@coffee.psychology .mcmaster.ca>
2003-06-12  0:38     ` usleep Ray Olszewski
2003-06-11 18:45       ` usleep Alan Bort
2003-06-12  4:14       ` usleep Riley Williams
2003-06-12  7:46         ` usleep Ray Olszewski
2003-06-13  1:23   ` usleep Stephen Samuel
2003-06-12 11:47 ` usleep Steven Smith
2003-06-13  1:16 ` usleep Stephen Samuel
2003-06-13  1:22   ` usleep Mark Hahn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030612005324.51946.qmail@mail.com \
    --to=leechin@mail.com \
    --cc=hahn@physics.mcmaster.ca \
    --cc=linux-newbie@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.