[vox-tech] Must a 300 microsecond delay keep the CPU busy?

Ted Deppner ted at psyber.com
Tue Apr 4 13:16:07 PDT 2006


On Tue, Apr 04, 2006 at 11:52:52AM -0700, Chris Jenks wrote:
>   I'm writing a C program on my Debian system to read from an interface 
> board through the parallel port. I need to wait at least 300 microseconds 
> before reading from the next channel, to give the A/D converter on the 
> board time to stabilize, but I don't want to wait much longer (e.g., 10 

Assuming you've got a somewhat standard kernel then... try this to enhance
your scheduling priority... this will increase the resolution of your
sleeps, maybe enough to do what you need.  Oh, and pay attention to what
you're doing.  When you have a high priority process running, if you did
anything wrong at all, you'll essentially lock the system up. A simple
"while(1);" becomes "ooops, must hit reset button".  Note, one call tells
the scheduler how important you are (and there are more options on the man
page, and the other call locks your program's memory so that your process
(or part of it) cannot be swapped out to disk.

Another trick is to read the CPU cycle counter, but I believe that is only
available on Pentium class CPUs.  http://faydoc.tripod.com/cpu/rdtsc.htm
seems to confirm that.

On last thought would be to do your normal sleep for 90% of the time you
need, and then do a more expensive time test for the last 10% in order to
increase your timing resolution only when it's most critical.


#include <stdio.h>
#include <time.h>
#include <sched.h>
#include <sys/mman.h>

/* TD July 24, 1999 or before */

int main()
{   
    struct sched_param sp;

    sp.sched_priority=90;
    if (sched_setscheduler(0,SCHED_FIFO, &sp)) {perror("setcheduler");exit(1);}
    if (mlockall(MCL_FUTURE)) { perror("mlockall"); exit(1); }

    printf("HIGH PRIORITY shell being launched\n");

    system("/bin/bash");

    exit(0);
}


-- 
Ted Deppner
http://www.deppner.us/


More information about the vox-tech mailing list