usleep
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 <[email protected]>
Date: Wed, 11 Jun 2003 19:45:17 -0400 (EDT)
To: Lee Chin <[email protected]>
Subject: Re: usleep
Quoted 59 lines
Quoted 2 lines
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 0swapshmm, 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.)
Signature · 9 lines
__________________________________________________________ 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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.linux-learn.org/faqs
Quoted 1 line
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 <[email protected]> */
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;
}-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs