Sign in to follow this  
MilindSP

Help with timers on Linux

Recommended Posts

[font="Verdana"][size="2"]Hey All,[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]I have application which runs on windows and Linux. [/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]Its rely on timers. In windows I could find way to implement system independent timer using gettickcount() API.[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]However under linux I am using gettimeofday() / clock_gettime() which depends on system Time.[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]If user or any other application changes system time, It adversely affect my application.[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]After doing lil google I found times() API can be used along with [color=#1C2837]sysconf [/color] to implement system independent timer. However when I read man pages for times() It is very confusing on how to use it.[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]Here is what I am doing in my app[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]clock_gettime(CLOCK_REALTIME,&start);[/size][/font]
[font="Verdana"][size="2"]Some activity ()[/size][/font]
[font="Verdana"][size="2"]clock_gettime(CLOCK_REALTIME,&stop);[/size][/font]
[font="Verdana"][size="2"]
[/size][/font][font="Verdana"][size="2"]if (diff(stop - star) ) [/size][/font][font="Verdana"][size="2"]then[/size][/font][font="Verdana"][size="2"] some activity[/size][/font][font="Verdana"][size="2"]
[/size][/font][font="Verdana"][size="2"]If during activity system time changes it affect My logic ..[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]If anybody has already used the same, I would like to see code or pseudo code whichever possible.[/size][/font]
[font="Verdana"][size="2"]
[/size][/font]
[font="Verdana"][size="2"]Thanks for help ..[/size][/font]

Share this post


Link to post
Share on other sites
Reading the [url="http://linux.die.net/man/3/clock_gettime"]man page for clock_gettime[/url], it's clear that what you want to use is [font="arial, sans-serif"][size="2"]CLOCK_MONOTONIC:[/size][/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"][size="2"][quote][/size][/font]CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point.[/quote]

EDIT: More information in [url="http://juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html"]this FAQ[/url].

Share this post


Link to post
Share on other sites
Hey guys Thanks for all help ...

However I presented question incorrectly .

I have application in which I am waiting on semaphore and network event for certain amount of time .

Application works smooth as long as nobody tampers the system time.

The moment system time is changed Application starts utilizing high CPU and becomes slow ..

I am using socket events and semaphores ..

Any clue on the same will help a lot

Thanks In advance..

Share this post


Link to post
Share on other sites
"I have application in which I am waiting on semaphore and network event for certain amount of time .

Application works smooth as long as nobody tampers the system time.

The moment system time is changed Application starts utilizing high CPU and becomes slow .."


How are you doing the waiting?

Share this post


Link to post
Share on other sites
Hey I am sorry for delayed response on thread

I am using select call for socket activities ..

In case of semaphore its sem_timedwait is used for specific time duration ..

Here is something which I tried to simulate the same scenario but unsuccessful .

I wrote some dummy code with similar logic and API I changed system time but in that case all events and activities are going smooth ..

However when all clubbed in application with multiple threads (to be precise 12) application shows strange behavior ..

It slows down If i move system timer back

and shoots up cpu usage in case timer is moved ahead ..


Also I have used clock_gettimeofday(MONOTONIC) which is system timer independent..

Anybody has seen such behavior please help ..

Any inputs for performance optimization are also welcome ..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this