LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2009, 12:03 AM   #1
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Rep: Reputation: 0
periodic timer callbacks!!!!


Hi,I am quite new to this community,could some one please help me out regarding this

issue. This is a sample code in kernel space.

struct timer_list mytimer;
init_timer (&mytimer);
mytimer.expires = jiffies + HZ/1000;
mytimer.data = 0/*or the pointer that pointing to your argument*/;
mytimer.function = /*your expireing function*/
add_timer (&mytimer);

Can any one of you plz suggest a way to implement this in user space in Linux? Thanks in

advance. And we need timer resolution of 1 ms.
 
Old 06-04-2009, 06:59 AM   #2
shibeaux
LQ Newbie
 
Registered: Nov 2008
Posts: 24

Rep: Reputation: 15
You could use select() function. here is an example of a times of five seconds:

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
while(1){

fd_set rfds;
struct timeval tv;
int retval;

/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds); /* Initialize the set of file descriptors */
//FD_SET(0, &rfds); /* Add stdin to the file descriptors set*/

/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;

retval = select(0, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */

if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\n");
}

return 0;
}

This program waits 5 seconds and tells u if there have been changes in stdin (file descriptor 0, the first parameter in select() function). You coul also use sleep() function into a while loop...

Hope this helps!
 
Old 06-05-2009, 01:24 AM   #3
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
thanx shibeaux,
That works!!!!Have a silly doubt regarding my application,plz excuse. I actually want to transfer the data from the upper layer to lower layer once this timer starts and before it expires. And then i need to call the timer function again to restart the timer once it expires and again check for data and transfer it.This needs to be repeted. Will it work if I integrate this code with the lower and upper layers? Thanx in advance again.
 
Old 06-08-2009, 08:44 AM   #4
shibeaux
LQ Newbie
 
Registered: Nov 2008
Posts: 24

Rep: Reputation: 15
I don't understand what u r trying to do, (who is the sender, the receiver, anh the information flow), but if u want to receive data from the kernel, your user space app could make use of rtnetlink socket instead of waiting until a timer ends.

If u explain your case in more detail, I'll try to help u.

Regards.
 
Old 06-10-2009, 01:13 AM   #5
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Okie,I will explain you in detail. I have a timer module,scheduler module,upper layers(RRC,RLC),MUX,DEMUX module and finally the lower layer(PHY layer). I need a timer resolution of 1 ms which has to be given to the scheduler and that signal is passed to upper layers to tell them to send the data through MUX/DEMUX module to PHY layer. All this happens within the timer expires. Once it expires,the timer function should be called again to start the timer with 1 ms resolution and this continues. This is the application i need to develop. So,Can I use the above CODE for the 1 ms resolution and starting the timer again? Thanks in advance.

Last edited by sandeepvignesh; 06-10-2009 at 01:14 AM. Reason: neede to include something.
 
Old 06-12-2009, 08:05 AM   #6
shibeaux
LQ Newbie
 
Registered: Nov 2008
Posts: 24

Rep: Reputation: 15
U could try this code changing the following:

tv.tv_sec = 0;
tv.tv_usec = 1000;

And test your app to see whether it works or not.

Regards.
 
Old 06-12-2009, 11:18 AM   #7
NightHorse
Member
 
Registered: Jun 2009
Location: Egypt
Distribution: Ubuntu 10.04
Posts: 117

Rep: Reputation: 16
ummm, Wouldn't the timer fires if you got anything in stdin, thro a keyboard, maybe??
 
Old 06-14-2009, 11:47 PM   #8
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
@ shibeaux: Yup,that is what I am up to now. Regards.

@ NightHorse : Could you plz explain me about that & how does it work,coz I have no idea about it. Regards.
 
Old 06-15-2009, 12:08 AM   #9
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
I have one more doubt regarding endianess. My system here is little endian and the board I am communicating with is big endian. Hence the data received by the board is not same as what I am sending. I have heard about htons and htonl,are they used only for IP address conversion or even for data? I just need to make sure that data should be matched both the sides,so can you please help me out!!!!Thanks in advance.......
 
Old 06-15-2009, 04:10 AM   #10
shibeaux
LQ Newbie
 
Registered: Nov 2008
Posts: 24

Rep: Reputation: 15
If the timer expires and there is no entry in stdin, it will be printed out "No data within ... seconds".
 
Old 06-15-2009, 10:45 PM   #11
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Okie,any idea about the endianess problem????Thanx in advance.
 
Old 06-16-2009, 03:31 AM   #12
NightHorse
Member
 
Registered: Jun 2009
Location: Egypt
Distribution: Ubuntu 10.04
Posts: 117

Rep: Reputation: 16
Well. For the timer what u really need is just a timer that can initiate a signal or a thread so u can handle whatever u want on that time. I have created a timer class. u can use it. It's a bit flexible. u can handle the timer by either a thread or a signal. I have only used it with signals tho. It's not well documented and am sorry about that. I uploaded it quickly today to post it for you. Any questions pls ask. Here is the link.

btw if you want to learn more about timers and signals in linux, There is a book that is well explaining both subjects. U will have to read only two or chapters. Won't take much time from you. The book name is Linux system programming. You can find the pdf if you do some googling.

I have no clue about big and small endian programming. May be someone else can help.
 
Old 06-16-2009, 11:07 PM   #13
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
@ NightHorse : Thanks for all the links. But I am trying to implement the timer in C language,sigh!!!!!
 
Old 06-17-2009, 12:03 AM   #14
sandeepvignesh
LQ Newbie
 
Registered: Jun 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Any idea about how to implement it in C?
 
Old 06-17-2009, 02:00 AM   #15
NightHorse
Member
 
Registered: Jun 2009
Location: Egypt
Distribution: Ubuntu 10.04
Posts: 117

Rep: Reputation: 16
The class is written in c++ but it should be working as c as well. It's using some system calls. If u r looking for simpler way u can still use select passing it 3 NULL parameters like this,

retval = select(0, NULL, NULL, NULL, &tv);

This will simply wait for the time set in tv. It won't return until it expires. unless an error happened. The only difference between this and what shibeaux posted is that it won't return if the stdin got any input.

And btw the timer class provide nanoseconds resolution. You only have to pick what's best for you. When I was looking in timers, I had to implement that class cause I wasn't looking for just sleeping. I was looking for something like an API timers. That means it doesn't stop my program till it ends. I wanted to keep my program flowing till the timer expires and go do specific method. *That's the big difference between select in this case and the timer class*
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I get LAPIC timer to run instead of the PIT timer? sixerjman Linux - Kernel 1 10-16-2007 10:59 PM
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 04:49 PM
Callbacks in Squid s_araj Programming 2 07-02-2005 07:06 AM
callbacks in C? MylesCLin Programming 2 02-10-2005 11:27 AM
include for callbacks.c abs Programming 5 04-07-2004 03:39 PM


All times are GMT -5. The time now is 06:50 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration