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 09-09-2011, 07:19 AM   #1
avee137
Member
 
Registered: Apr 2010
Location: bangalore,india
Distribution: ubuntu 9.10,CentOS 5
Posts: 120

Rep: Reputation: 16
implementing a time delay in program


I have a code like this :

Code:
int main(){
{
//do something
 while(1)
 {
   while(1)
   {
     //check for some condition to satisfy
    //if the condition does not satisfy for 1 s : break from loop 
   }

 }
}
in the internal while loop i need to check for time delay. please suggest some available
APIs in linux for this.



Thanks.
 
Old 09-09-2011, 07:26 AM   #2
serafean
Member
 
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 872
Blog Entries: 10

Rep: Reputation: 103Reputation: 103
Is this what you are looking for?

Serafean
 
Old 09-09-2011, 03:11 PM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,213

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
maybe fork a new process.

then sleep(1) the main process and kill the pid of the subprocess ?
 
Old 09-10-2011, 12:53 AM   #4
avee137
Member
 
Registered: Apr 2010
Location: bangalore,india
Distribution: ubuntu 9.10,CentOS 5
Posts: 120

Original Poster
Rep: Reputation: 16
I can't afford to sleep() there. I need a timer which can give an indication that 1 second elapsed since i entered the internal while loop.
 
Old 09-10-2011, 07:49 AM   #5
cheddarcheese
Member
 
Registered: Aug 2011
Location: Massachusetts, USA
Distribution: Fedora; Centos; Puppy
Posts: 80

Rep: Reputation: 5
You may find the following link useful. It is helpful in explaining how to measure elapsed time in Bash scripts. I'm sure you can probably use the same idea in your own script.

http://www.linuxjournal.com/content/...e-elapsed-time

Hope that helps.
 
Old 09-10-2011, 08:09 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
maybe the <sys/time.h> header and functions like gettimeofday are what you're looking for
 
Old 09-10-2011, 10:57 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
It depends on the condition. If it's waiting for anything related to input / output (serial port, keyboard, socket, ...), select or poll are the way to go.

Else maybe you can tell us the condition that you're waiting for so we can comeupwith the right idea /suggestion.

By the way, there is a dedicated programming section here at LQ. There are some people there that only seem to answer coding related questions and don't visit other sections here. You can ask a moderator to move this thread by reporting your opening post.
 
Old 09-10-2011, 11:18 AM   #8
avee137
Member
 
Registered: Apr 2010
Location: bangalore,india
Distribution: ubuntu 9.10,CentOS 5
Posts: 120

Original Poster
Rep: Reputation: 16
Thanks for replies .

I got it done. The sample code is like
Code:
#include<time.h>

       main()
       { 

	time_t t1 ,t2,tin,tout ;
	t1 = time(&tin);

	while(1)
		{

			printf("busy waiting\n") ;
			t2 = time (&tout);

			if((t2-t1) >= 1)
				break ;
			else
				continue ;


		}
	printf("%ld\n",(long)(tout-tin));
	printf("out of internal while\n");

        }
As my requirement has no hard limits. This is working fine for time being.

Last edited by avee137; 09-10-2011 at 11:20 AM. Reason: info missing
 
Old 09-10-2011, 11:43 AM   #9
Ian John Locke II
Member
 
Registered: Mar 2008
Location: /dev/null
Distribution: Slackware, Android, Slackware64
Posts: 130

Rep: Reputation: 17
Don't forget to mark this as solved
 
Old 09-10-2011, 01:28 PM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
You are aware that this solution chews cpu power? I think that select and poll will be more efficient but I've never used it with an empty set.
 
Old 09-12-2011, 01:13 AM   #11
avee137
Member
 
Registered: Apr 2010
Location: bangalore,india
Distribution: ubuntu 9.10,CentOS 5
Posts: 120

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Wim Sturkenboom View Post
You are aware that this solution chews cpu power? I think that select and poll will be more efficient but I've never used it with an empty set.
Thanks for the suggestion.HoweverCPU power is not a big concern for time being in my application.
 
  


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
Implementing time based file rolling jawaid Programming 5 09-11-2008 11:21 PM
gimp changes my layers delay time rblampain Linux - Desktop 3 01-26-2008 03:28 AM
Time Delay program needed koby *BSD 2 06-14-2005 12:14 PM
implementing Class-based queue from a C program.. shrike_912 Programming 1 07-05-2004 02:01 PM
The following delay program is confusing me ...... purpleburple Programming 5 09-15-2002 11:11 PM


All times are GMT -5. The time now is 03:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration