LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-24-2005, 01:30 AM   #1
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Rep: Reputation: 15
About timer.... help


Hi all,
I want to use a timer to avoid busy loop to check a particular value in Linux platform using C language.
Is there any such timer....?
I dont know how to use...?
Plz help me if any body has any idea......
If possible with some sample code......

Thanking u
With Regard
RajSun
 
Old 05-24-2005, 03:58 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
I'm too bored to write code right now but what you are asking is really easy.
Either use a loop and put a sleep to suspend and wake the process every 2-3 seconds, in order to keep the CPU usage in a low level or use alarm.

Hope you know what signals and signal handling is.
alarm(3) sends a SIGALARM after 3 seconds.
if you define a signal handler for SIGALARM, every time a SIGALARM is sent, the signalhandler which is just a simple function will be executed.
Well what the hell... I'll give you some sample code:

Code:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void alarm_handler(int);
 
int main()
{  
    signal(SIGALRM,alarm_handler);
   alarm(3);
   while(1){}
}

void alarm_handler(int num)

{
   fprintf(stderr,"Alarm signal catched\n");
   alarm(3);
}
 
Old 05-24-2005, 04:46 AM   #3
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks to replay me.....

But i m working in multithreading so i want individual timer for each thread......
and i cant use alarm because the signals and signal handlers are process wide therefore i cannt make it specific for each thread.......

So if u know plz let me know.......

once again thanking u .......
 
Old 05-24-2005, 06:06 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 101Reputation: 101
Quote:
Originally posted by rajsun
But i m working in multithreading so i want individual timer for each thread......
and i cant use alarm because the signals and signal handlers are process wide therefore i cannt make it specific for each thread.......
Are you sure about that? In Linux threads are implemented as processes. So I'd guess each thread has its own sigalarm timer. I'm not 100% sure though, as I've no experience in thread programming myself.

You may also check "man 2 setitimer".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
timer in c?? AcidHell2 Programming 9 08-12-2004 01:01 AM
First timer, help!!!!! jcniest5 Linux - Newbie 7 02-24-2004 08:32 AM
timer in C timmy334 Programming 10 11-18-2003 11:40 AM
timer... perdesiz Linux - Software 1 09-18-2003 03:39 AM
QT Timer kusio Linux - Software 1 04-15-2003 05:48 PM


All times are GMT -5. The time now is 03:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration