LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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 08-09-2007, 01:25 PM   #1
omwoyo
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Rep: Reputation: 0
using select() to invoke a timed function


I am using select to read inputs from more than one connected clients. I have some task to perform every after some given period of time, instead of using a thread or a child process I m not user whether it can be done by the select function.

I am therefore kindly requesting for some advice on how I can manage with select if possible be done

Last edited by omwoyo; 08-09-2007 at 01:28 PM.
 
Old 08-09-2007, 07:41 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
You can use function select() to wait for I/O to be occur on one or more file descriptors, and/or wait for a certain amount of time to elapse. I believe this is what you want. For more information:

Code:
man select
Hope this helps.
 
Old 08-10-2007, 06:51 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
select() does take timeout parameter, so it can be used as a sort of timer also. But as long as the connected client(s) do I/O requests within the timeout period, your periodical task will not run.

If this is not a problem, you could use select()'s timeout parameter.

However on Linux it is possible, using this feature of the Linux version of select(). (from the man page):
Quote:
On Linux, select() modifies timeout to reflect the amount of time not slept; most other implementations do not do this. (POSIX.1-2001 permits either behavior.) This causes problems both when Linux code which reads timeout is ported to other operating systems, and when code is ported to Linux that reuses a struct timeval for multiple select()s in a loop without reinitializing it.
So, if you don't reset the timeout struct every loop, you could use select() to do this. But it is not portable.

Another way may be to use setitimer().

Note there is also a man page called "select_tut", which is a more tutorial like man page about select(). I suppose they made this because select() is pretty complex to use.
 
Old 08-11-2007, 01:20 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Quoth the highly esteemed Hko:
Quote:
select() does take timeout parameter, so it can be used as a sort of timer also. But as long as the connected client(s) do I/O requests within the timeout period, your periodical task will not run.

If this is not a problem, you could use select()'s timeout parameter.
You could use select()'s timeout parameter in any case.

Before you call select(), you should call gettimeofday() to figure out the timeout value to give to select(); the closer you are to when you want to do your next periodic tasks, the lower this timeout value should be. And if the timeout value is zero or negative, you do your periodic tasks before you come back to do select() again.
 
Old 08-11-2007, 11:42 PM   #5
sundialsvcs
Senior Member
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 3,685

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
It can be a bit tricky to cause a select()-based routine to also produce reliable timeouts. You have to use gettimeofday() to establish the current time, then calculate the time-of-day ("deadline") at which the timed-event should next occur, and use this to calculate the appropriate timeout. A check against the deadline must then occur every time through the loop, whether it's being run as a result of timeout or because an I/O-operation was actually completed.

If you have threads at your disposal, it is by far easier to delegate the timed activity to a separate thread, which can concern itself only with the timeout while the select()-processing thread concerns itself only with the select.
 
Old 08-13-2007, 08:15 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Quoth the highly esteemed sundialsvcs:
Quote:
It can be a bit tricky to cause a select()-based routine to also produce reliable timeouts. You have to use gettimeofday() to establish the current time, then calculate the time-of-day ("deadline") at which the timed-event should next occur, and use this to calculate the appropriate timeout. A check against the deadline must then occur every time through the loop, whether it's being run as a result of timeout or because an I/O-operation was actually completed.
Pseudocode:

Code:
set initial deadline;

for(;;)
{
  for(;;)
  {
    calculate timeout from deadline;

    if(timeout>0)
    {
      break;   /* <--------- */
    }

    do the timed event;

    deadline+=interval;
  }

  select();

  if(any I/O completed)
  {
    respond to one I/O event;
  }

  /* No special reaction to timeout. */
}
I guess what I'm missing is what's tricky about this.

My experience with threads is that you avoid them if you can, and use them when you must. Since all threads share the same memory space, debugging can be a huge pain, especially when encountering memory corruption problems.
 
  


Reply

Tags
select


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
hwclock error: "select() to /dev/rtc to wait for clock tick timed out" silencestone Linux - Software 3 02-04-2007 10:43 PM
Running a PHP function at timed intervals linux_pioneer Programming 5 09-26-2004 01:22 PM
Timed execution of a function arvind_tyche Programming 1 05-07-2004 08:25 AM
Bash Select Function: Formatting Output mooreted Linux - General 2 03-28-2004 06:26 AM
select function as timeout? frostmagic Programming 2 02-09-2004 11:56 AM


All times are GMT -5. The time now is 06:01 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