![]() |
|
#1
|
|||
|
|||
Need advice to provide accurate time delay in CHi,
Can I ask if anyone can advise me on the command to provide accurate timing delay in C which which I can use for my multithreading application using pthread? I've tried generating a "for" loop which is not ideal in my case because the busy loop just takes up the processor and stops other processes from running in parallel. Also I've tried the "usleep" command but usleep(1) (presumably time delay of 1 microsec) is too slow. Can I ask what usleep is exactly doing - is it a kind of busy loop as well? If not, can I check if there is a command call "nanosleep"? Please kindly advise if there is a nice way to generate this delay which does not require any processing (so that I can use it for the multithreading) and fast enough (preferably in nanosecs). Thanks for your help in advance. Peck |
|
#2
|
|||
|
|||
Re: Need advice to provide accurate time delay in CQuote:
What operating system? Embedded or hosted? Regards, Dave |
|
#3
|
|||
|
|||
Re: Need advice to provide accurate time delay in CI'm using embedded Linux for my application. Any suggestions?
Thanks. Peck |
|
#4
|
|||
|
|||
Re: Need advice to provide accurate time delay in CQuote:
I'll give the bottom line first. Bottom line: If you really need (software-based) delays with microsecond accuracy, you can't count on it with Linux. For embedded systems, you might be able to access hardware counter/timers in the CPU (or in other parts of the design) that you can use to effect fine-grained delays. In reality, regardless of the fact that the arguments to usleep and nanosleep let you specify a delay in microseconds or nanoseconds, the actual resolution is limited to a value set up by the kernel configuration. For my Centos Linux system, the best resoution that it can attain is 10 milliseconds. That is to say that the minimum delay is 10 milliseconds, and if I specify a delay of 10 milliseconds plus one nanosecond, the delay will be at least 20 milliseconds. Here's the way the functions like usleep, nanosleep, setitimer, etc., usually work these days: When the function is invoked, a "wake-up" call is scheduled with the kernal at the time specified by the delay argument. Then the current process is suspended. The kernel goes on to its next scheduled process. When the specified time has elapsed, the kernel can activate the original process again. So, with these functions, the only guarantee is that the delay will be "at least as much as" the time specified in their arguments. The finest resolution is determined by a configuration parameter when the kernel was compiled. With an embedded system you may be able to get resolutions as fine as a millisecond, but I seriously doubt that you can get better than that. (And there are some pretty good reasons why most applications wouldn't want to do that anyhow.) Now older kernels (non-preemptive) would sometimes implement delays of less than a couple of milliseconds by using a delay loop, but if you have observed that it doesn't work on your system, that is probably no help at all. If you are doing development for your embedded application on a Linux workstation, then you can try things like man nanosleep, man usleep, man setitimer, man 7 time. Of course, those man pages are for your workstation, not for the target system, and, depending on kernel versions, the descriptions may not apply. But they could give you some ideas for further investigation. See Footnote. Keep in mind that all of these functions are not standard C library functions. In fact some embedded systems may have some other functions (maybe defined down at the "asm" level) that support the kind of timing that you want. Consult the documentation for whatever distribution you are using. Since, I don't think you can do what you need with software timers like used with nanosleep, etc., consider the following: You may be able to access hardware counter/timers in your target system (either with vendor-supplied functions and/or device drivers or by writing your own hardware-accessing functions). That's just about the only way to get microsecond-resolution "real time" action from a Linux system, embedded or not. Regards, Dave Footnote: If you want to see more details about what you are up against, you can check out things like the following: Linux Process Scheduling Concepts Look for other stuff like this on the web. There may be information more useful than what I have given here, and in fact there may be ways that I don't know about or haven't thought of. Last edited by davekw7x : 29-Apr-2008 at 08:55.
|
|
#5
|
|||
|
|||
Re: Need advice to provide accurate time delay in CQuote:
If you are really going to "get into" this stuff, and if your embedded distribution is more recent, the O'reilly book "Understanding the Linux Kernel, 3rd Edition" covers kernel 2.6, and has chapters on scheduling, timing, and just about everything else. There are numerous web references that may help. For example: I/O port HOWTO: HIgh-Resolution Timing Regards, Dave |
|
#6
|
|||
|
|||
Re: Need advice to provide accurate time delay in CThanks, Dave. I'll have a read on these useful stuff and will modify my codes accordingly once I understand the mechanisms.
Peck |
|
#7
|
|||
|
|||
Re: Need advice to provide accurate time delay in CAfter modified, please post your code here and let other benefits.
__________________
Linux is the best OS in the world. |
Recent GIDBlog
Toyota - 2008 July Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unusual time delay (VC++ project) | enjoy | MS Visual C++ / MFC Forum | 1 | 27-Jun-2007 21:04 |
| Asynchronous transfer question | crystalattice | Miscellaneous Programming Forum | 2 | 24-Jan-2007 20:39 |
| constructors/classes | mapes479 | CPP / C++ Forum | 3 | 19-Nov-2006 17:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The