In the program below, I’m trying to measure the time elapse between a call
to usleep(10); I’m always getting 2 millisecond, until I increase the usleep
to 1000. Then I get 3 millisecond. I have made the program at high priority
(63). It is running on a pentium 1.3Ghz with almost nothing else running.
Why doesn’t I get a elapse time more close to the usleep call i’m setting.
I know I can create a timer. But I’m porting a program that use this method
to wait for small period of time.
Thanks
Rejean Senecal
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <signal.h>
#include <math.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>
// Time diff in millisecond
#define OC_DEBUG_DIFF_TIME(END,START)
((double(END.tv_sec-START.tv_sec)*1000.0)+(double(END.tv_nsec-START.tv_nsec)
/1000000.0))
using namespace std;
int main(int argc, char** argv) {
std::cout << “Hello, realtime!” << std::endl;
//--------------------------------------------------------------------
// Check priority and make ourself realtime via sched_fifo.
//--------------------------------------------------------------------
sched_param schedulerParams;
schedulerParams.sched_priority=-1;
if (sched_getparam(getpid(), &schedulerParams)!=0){
perror("Cannot Get scheduler parameters ");
}
schedulerParams.sched_priority=63;
if(sched_setscheduler(getpid(), SCHED_FIFO, &schedulerParams))
{
perror("Cannot Set scheduler parameters ");
exit(-1);
}
//--------------------------------------------------------------------
struct timespec before;
struct timespec end;
if (clock_gettime(CLOCK_REALTIME,&before)){
perror(“clock_gettime”);
}
usleep(10);
clock_gettime(CLOCK_REALTIME,&end);
cout << " time " << OC_DEBUG_DIFF_TIME(end,before) << endl << flush;
}