LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to implement delay (microseconds) with real timer ticking at 100ms. (https://www.linuxquestions.org/questions/programming-9/how-to-implement-delay-microseconds-with-real-timer-ticking-at-100ms-777858/)

mayankparasher 12-24-2009 05:57 AM

how to implement delay (microseconds) with real timer ticking at 100ms.
 
hi all,
In my application i implemented one timer(ITIMER_REAL) ticking @ 100ms. Code given below:
struct timeval my_value={0,100000};
struct timeval my_interval={0,100000};
struct itimerval my_timer={my_interval,my_value};
setitimer(ITIMER_REAL, &my_timer, NULL);
signal(SIGALRM,TimerHandler );
TimerHandler is the event executing @ every 100ms.
It works absolutly fine.

The problem is, i already implemented various usleep in application, which gets terminated because of SIGALRM signal from ITIMER_REAL. So now how do i implement delay(micro seconds) without effecting ITIMER_REAL?.

I tried to write own delay by reading microseconds. Code given below:
void delay_1ms(long int ms)
{
struct timeval tv;
ms= ms*1000;
gettimeofday(&tv, NULL);
ms +=tv.tv_usec;
while(ms!=tv.tv_usec)
{
gettimeofday(&tv, NULL);
}
}
but for value 1ms it goes for couple of seconds waiting.
I m doubting is gettimeofday is 'real time read'?
whats the way out friends?

Thanks in advance
-Mayank parasher.

smeezekitty 12-24-2009 12:04 PM

Code:

void delay_1ms(long int ms)
{
struct timeval tv;
ms *= 10;
gettimeofday(&tv);
time_t s = tv.tv_usec;
while(ms!=(tv.tv_usec-s))
{
gettimeofday(&tv, NULL);
}
}

try that.


All times are GMT -5. The time now is 05:39 AM.