Thread: cpu usage

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    cpu usage

    ok, just to let you all know, this is basically my first complete program, so if what i'm asking is just plain stupid, don't yell.

    the program is designed to make a backup of a text file every two hours until manually terminated. the problem i have is that i don't know a good way to make a timer to do that. i tried one way, and yes it runs fine, but it ended up taking about 70% of my cpu resources to run a 90-line program. so obviously that's no good. here's the way i tried to make the timer.
    Code:
    int main()
    {
    	int x=0;
    	int diff=0;
    	long start=0, current=0;
    	char file_name[15]="";
    
    	while(x==0)
    		{
    			start=time(NULL);
    
    			while(diff<60)
    				{
    					current=time(NULL);
    					diff=current-start;
    				}
    			strcat(file_name,get_name());
    			create_backup(file_name);
    		}
    	return 0;
    }
    if anybody knows a good way to do this(and i'm sure everybody but me does), please let me know. any help is greatly appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Review your compilers documentation to find a sleep() or delay() type function. These local implementations won't hog the CPU like your version.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    ahhh, thanks much. just what i was looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 03:07 AM