ideas on how to make this not use 99% system resources?

This is a discussion on ideas on how to make this not use 99% system resources? within the C++ Programming forums, part of the General Programming Boards category; Okay this is my code and its pretty simple and self-explainatory. The problem is that this program must be running ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    ideas on how to make this not use 99% system resources?

    Okay this is my code and its pretty simple and self-explainatory. The problem is that this program must be running in the background at all times so I don't want it to use all my CPU power. I know that the reason is because it recreates the time everytime it enters each loop but I can't really think of how else you would constantly update the time. I'm sure it is a very easy concept but I cannot get my mind out of this mode of thinking so insight would be great. Thanks

    Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <iostream>
    
    using namespace std;
    int main ( void )
    {
      time_t now;
      struct tm *tm_now;
      bool ONTIME;
      
      if (tm_now->tm_hour < 20){
          system("ipconfig /release");
          ONTIME = false;
          cout << "Session ended.\n";
          }
      else{
          ONTIME = true;
          }
      
      while(!ONTIME)
      {
       now = time ( NULL );
       tm_now = localtime ( &now );
       if (tm_now->tm_hour == 20){
          system("ipconfig /renew");
          ONTIME = true;
          cout << "Session started.\n";
          }
      }
       
      while(ONTIME)
      {
       now = time ( NULL );
       tm_now = localtime ( &now );
       if (tm_now->tm_hour == 0){
          system("ipconfig /release");
          ONTIME = false;       
          cout << "Session ended.\n";
          }  
      )
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Use a platform dependent version of Sleep/sleep to sleep for however many seconds/milliseconds you want to sleep.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Assuming you are running Windows, you can use Sleep(). A one-millisecond sleep-delay somewhere inside the loop should help. This is system-specific. With standard C++, there is no way to tell the CPU to "take a break".

    Sleep() tells the operating system that it's OK to take a break from your program... so that it can go-away and run another program. "Well behaved" programs don't hog the CPU on a multitasking operating system. (Windows won't allow a user-mode program to completely hog the CPU. The OS is in-charge of how much CPU time each program gets.)

    The problem is not with time. It's with your continuous loop. The operating system assumes that you want to run that loop as fast as possible, and it will give you any available CPU cycles.
    Last edited by DougDbug; 07-22-2005 at 06:39 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Since technically you are only checking for the hour, you should sleep for a lot more than 1 millisecond... I would do 1-60 seconds depending on how accurate you need to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operating system construction
    By AdamLAN in forum Tech Board
    Replies: 7
    Last Post: 03-05-2005, 12:31 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. Anyone want to make something or have Ideas?
    By adamg in forum C Programming
    Replies: 4
    Last Post: 05-13-2004, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21