Linux Newbie help
 help / color / mirror / Atom feed
* usleep
@ 2003-06-11 22:55 Lee Chin
  2003-06-11 16:08 ` usleep Alan Bort
                   ` (5 more replies)
  0 siblings, 6 replies; 19+ messages in thread
From: Lee Chin @ 2003-06-11 22:55 UTC (permalink / raw)
  To: hahn, linux-newbie

I would think the following code would wait for 1 second each itteration before printing hello, but it waits way too long.  Replacing the for loop body with a 
usleep(1000000) works great... what am I missing here?

Thanks
Lee

#include <unistd.h>

int main()
{
    while (1)
    {
        int i;

        printf("hello\n");
        for(i = 0; i < 50; i++)
        {
            usleep(1000);
        }

    }

}

-- 
__________________________________________________________
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

^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: usleep
@ 2003-06-12  0:42 Lee Chin
  0 siblings, 0 replies; 19+ messages in thread
From: Lee Chin @ 2003-06-12  0:42 UTC (permalink / raw)
  To: 333101; +Cc: hahn, linux-newbie

I'm not sure what you are saying, but yes there are 1 million microseconds in a second.... 1000000 micro seconds that is.  So my loop seems correct.  Also, I am running this on a 2 GhZ processor, so I doubt the 1000 calls to usleep can add that much overhead.  I think something is wrong with the library or the kernel.
----- Original Message -----
From: Alan Bort <333101@personal.net.py>
Date: 11 Jun 2003 19:08:34 +0300
To: Lee Chin <leechin@mail.com>
Subject: Re: usleep

> for the #include statemen I assume you are using some sort of C
> languagbe (even C, C++ or C#) Well... my man of usleep says that it
> makes the system wait for any number of microseconds... make the maths.
> 
> Anyway... I really don't remebmer the value of a microsecond... I THINK
> it was something like "1 x 10 ^ -6" or something alike... (excuse the
> quotation aberration).
> 
> 
> El jue, 12-06-2003 a las 01:55, Lee Chin escribió:
> > I would think the following code would wait for 1 second each itteration before printing hello, but it waits way too long.  Replacing the for loop body with a 
> > usleep(1000000) works great... what am I missing here?
> > 
> > Thanks
> > Lee
> > 
> > #include <unistd.h>
> > 
> > int main()
> > {
> >     while (1)
> >     {
> >         int i;
> > 
> >         printf("hello\n");
> >         for(i = 0; i < 50; i++)
> >         {
> >             usleep(1000);
> >         }
> > 
> >     }
> > 
> > }
> -- 
> Alan Bort
> Linux Registered User 298277 -Country Manager- [http://counter.li.org]
> [ http://www.linuxquestions.org ] Username: Ciccio
> [ http://es.tldp.org ]
> Ciccio.-
> 

-- 
__________________________________________________________
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

^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: usleep
@ 2003-06-12  0:53 Lee Chin
  2003-06-12  1:02 ` usleep Mark Hahn
  0 siblings, 1 reply; 19+ messages in thread
From: Lee Chin @ 2003-06-12  0:53 UTC (permalink / raw)
  To: hahn; +Cc: linux-newbie

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

^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: usleep
@ 2003-06-12  1:04 Lee Chin
  0 siblings, 0 replies; 19+ messages in thread
From: Lee Chin @ 2003-06-12  1:04 UTC (permalink / raw)
  To: cat; +Cc: linux-newbie

Sorry everyone, I had initially posed my for loop wrong... I meant to say for(i = 0; i < 1000; i++).

Mark Hahn clarified the issue and usleep is not actually accurate (provably) and the man page actually has a disclaimer.  To me, it seems like usleep(2000) * 50 itterations actually gives a reasonably accurate 1 second delay on machines from 800 mgz to 2 ghz.

Mark Hahn actually gave me some more accurate code to do usleep too.  Thanks Mark!
Thanks
Lee
----- Original Message -----
From: CaT <cat@zip.com.au>
Date: Thu, 12 Jun 2003 09:21:46 +1000
To: Lee Chin <leechin@mail.com>
Subject: Re: usleep

> On Wed, Jun 11, 2003 at 05:55:02PM -0500, Lee Chin wrote:
> > I would think the following code would wait for 1 second each itteration
> > before printing hello, but it waits way too long.  Replacing the for
> > loop body with a usleep(1000000) works great... what am I missing here?
> 
> usleep = micro sleep = sleep in 1/1000000 second incrememnts.
> 
> -- 
> Martin's distress was in contrast to the bitter satisfaction of some
> of his fellow marines as they surveyed the scene. "The Iraqis are sick
> people and we are the chemotherapy," said Corporal Ryan Dupre. "I am
> starting to hate this country. Wait till I get hold of a friggin' Iraqi.
> No, I won't get hold of one. I'll just kill him."
> 	- http://www.informationclearinghouse.info/article2479.htm

-- 
__________________________________________________________
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

^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: usleep
@ 2003-06-12 20:43 Lee Chin
  0 siblings, 0 replies; 19+ messages in thread
From: Lee Chin @ 2003-06-12 20:43 UTC (permalink / raw)
  To: hahn; +Cc: linux-newbie

Mark,
Two questions

1) Do threads inherrit the schedule priority?  Or do I need to set this for every thread

2) If there is nothing else runnable in my system, will this help? or is it effectively the same?

Thanks
Lee
----- Original Message -----
From: Mark Hahn <hahn@physics.mcmaster.ca>
Date: Wed, 11 Jun 2003 21:02:17 -0400 (EDT)
To: Lee Chin <leechin@mail.com>
Subject: Re: usleep

> > Any way... what is setrealtime?  I dont have it on my linux machine
> 
> it's a simple tool I wrote a long time ago, code below.
> ***** N O T E *******
> it can effectively freeze your machine, since it gives you a process
> which will not be prempted!  it *is* useful, though, if you know
> what you're doing...
> ***** N O T E *******
> 
> #include <unistd.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <sched.h>
> /* setrealtime: run procs in realtime.
>    author: Mark Hahn <hahn@mcmaster.ca> */
> 
> int main(int argc, char *argv[]) {
>     static struct sched_param sched_parms;
>     int pid, wrapper=0;
> 
>     if (argc <= 1)
>         return 1;
> 
>     pid = atoi(argv[1]);
> 
>     if (!pid || argc != 2) {
>         wrapper = 1;
>         pid = getpid();
>     }
>     if (!pid)
>         return 1;
>  
>     sched_parms.sched_priority = sched_get_priority_min(SCHED_FIFO);
>     if (sched_setscheduler(pid, SCHED_FIFO, &sched_parms) == -1) {
>         perror("cannot set realtime scheduling policy");
>         return 1;
>     }
>     if (wrapper) {
>         setuid(getuid());
>         execvp(argv[1],&argv[1]);
>         perror("exec failed");
>         return 1;
>     }
>     return 0;
> }
> 

-- 
__________________________________________________________
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

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2003-06-13  1:23 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
2003-06-12  0:42 usleep Lee Chin
2003-06-12  0:53 usleep Lee Chin
2003-06-12  1:02 ` usleep Mark Hahn
2003-06-12  1:04 usleep Lee Chin
2003-06-12 20:43 usleep Lee Chin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox