Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-11-2008, 06:33 AM   #1 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default timer in C

i want to implement a C program where the program will run for 1 minute and then terminate itself. I can monitor how much time my program has run but i cannot implement a timer for the same program.
any help is sincerely appreciated. thanks and c ya!!
__________________
<A HREF=http://ubanimator.com><IMG SRC=http://img182.imageshack.us/img182/5168/userbar511415cb2.gif><A>
simple is offline  
Old 03-13-2008, 01:09 PM   #2 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default Re: timer in C

apparently no one has any solution????
__________________
<A HREF=http://ubanimator.com><IMG SRC=http://img182.imageshack.us/img182/5168/userbar511415cb2.gif><A>
simple is offline  
Old 03-13-2008, 06:36 PM   #3 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,484

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: timer in C

Code:
#include<conio.h>
#include<time.h>

int main()
{
   while(!kbhit()) /* this function might use a different header file for you depending on your compiler */
   {
       sleep(60000); /* counts in milliseconds, this is one minute */
   }
}
I think this is what you want. Let me know . Any reason you're using C and not C++? Haven't learned object orientation yet?
__________________


Last edited by Baez; 03-14-2008 at 12:45 AM.
Baez is online now  
Old 03-21-2008, 04:49 AM   #4 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default Re: timer in C

can't use conio.h because i am using gcc compiler on linux platform.
and i have to use C . its a small thing and i dont have any issues with object oriented stuff.
thanks for your time...hope i get some relevant replies!!
c ya!!
__________________
<A HREF=http://ubanimator.com><IMG SRC=http://img182.imageshack.us/img182/5168/userbar511415cb2.gif><A>
simple is offline  
Old 03-21-2008, 06:55 PM   #5 (permalink)
void's Avatar
 
True Techie

Join Date: Oct 2005

Posts: 198

void

Default Re: timer in C

Have a look at the system call getrusage(). If you use RUSAGE_SELF as the first parameter then the rusage struct is filled with process information about the current process. Then you need to process the timeval structures ru_utime and ru_stime. Each of these contain the user and system time used by the process. For example this prints the CPU time:
Code:
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
	struct rusage process_usage;
	getrusage(RUSAGE_SELF, &process_usage);
	printf("CPU time: %ld.%06ld sec user, %ld.%06ld sec system\n", 
		process_usage.ru_utime.tv_sec, process_usage.ru_utime.tv_usec, 
		process_usage.ru_stime.tv_sec, process_usage.ru_stime.tv_usec
}
Also have a look at alarm(). This will raise a SIGALRM which use can handled by setting a function to be called using signal().

If you have the manual pages installed you can find out more with the command:
Code:
man [function name]
Finally I highly recommend the free book Advanced Linux Programming. It details many of the techniques used to solve problems like the one you mentioned. It quite a read but its worth it.

Edit: Heres a sample program that will run for a minute and print CPU time at the end.
Code:
#include <signal.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

void timer_expired(int signal);

sig_atomic_t active;

int main(int argc, char *argv[]) {
	signal(SIGALRM, &timer_expired);
	//Sets the alarm to raise a SIGALRM in 60 seconds.
	alarm(60);
	active = 1;
	fprintf(stdout, "Running ");
	while (active) {
		printf(".");
		fflush(stdout);
		sleep(1);
	}
	printf("\n");
	struct rusage usage;
	if (getrusage(RUSAGE_SELF, &usage) == -1) {
		printf("Failed to get usage information.\n");
		return 0;
	}
	printf("CPU time: %ld.%06ld sec user, %ld.%06ld sec system\n",
			usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
			usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
	return 0;
}


Last edited by void; 03-22-2008 at 06:20 AM.
void is offline  
Old 05-10-2008, 04:23 AM   #6 (permalink)
mattew's Avatar
 
Banned

Join Date: May 2008

Posts: 147

mattew is on a distinguished road

Send a message via MSN to mattew
Default Re: timer in C

well its got much a simpler solution i beleive ..
u can use the delay function which comes under dos.h or proces.h got a doubt about that...
well using the delay function and later on exit the program using exit (0);
or abort();
will do ur job

*abort will not save ne data while program is in excecution and it will directly abort from it .. exit is a safer one to use *
mattew is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Water Cooling First Timer DeadNoMore Building, Buying, or Upgrading High Performance PC Systems 2 02-05-2008 11:18 PM
How's my 2K build (First timer) bull3tx2 Building, Buying, or Upgrading High Performance PC Systems 15 10-27-2007 08:31 PM