Idle Issue.

This is a discussion on Idle Issue. within the C++ Programming forums, part of the General Programming Boards category; Greetings, I have been having an issue with some code that is taking up 98% of the cpu usage when ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    32

    Idle Issue.

    Greetings,

    I have been having an issue with some code that is taking up 98% of the cpu usage when there is nothing happening.

    I found a post

    Idling and not looping.

    that spoke of the same issue, however i do have my program using Sleep whenever there is no data, but it is still using the same CPU usage.

    The program is going to end up being a plugin irc protocol for an online game, however for right now i its just a standalone applicatoin, and finding a way to optimize it to stay on low cpu usage while no data is comming in would be great.

    Note: I can't used the windows message calls like Peekmessage and PumpMessage since it will be ported later, however for now any other methods that anyone can think of that doesn't use the windows message queue would be great, also its a console app so message queue won't do much good =/.

    here is the current loop code.

    Code:
    	while(1)
    	{
    		if(newinput)
    		{
    			sprintf(buffer,"%s",input);
    			send(cData.sock,buffer,strlen(buffer),0);
    			newinput = false;
    			input = "";
    		}
    		//Get Incommming Data
    		for (i=0;i<512;i++) { 
    		err2 = recv(cData.sock, &ireadbuf[i], 1, 0); 
    		if ((ireadbuf[i] == '\n') || (err2 == 0) || (err2 == SOCKET_ERROR)) { 
    	         if (ireadbuf[i] == '\n') {
    				 ireadbuf[i] = '\0';
    				 if((message = irc->parse(ireadbuf)) != NULL) {
    					 if(message[strlen(message)-1]== '\r')
    						 message[strlen(message)-1] = '\0';
    					 irc->ircOut(message);
    				 }
    			 }
    			break; 
    		}
    		}
    
    
    
    		if ((err2 == 0) || (err2 == SOCKET_ERROR)) {
    			if (!(WSAGetLastError() == WSAEWOULDBLOCK)) {
    				irc->ircOut("**Connection closed.**\n");
    				if (err == SOCKET_ERROR) {
    					sprintf(buffer,"**Error %i\n", WSAGetLastError());
    					irc->ircOut(buffer);
    				}
    				break;
    			} else {
    				Sleep(1); // Idle
    				continue;
    			}
    		}
    		Sleep(1);
    	}
    There is an Input Thread running the background processing input, however even with it turned off, the cpu usage is still the same.

    The Full Messy Source can be found Here

    Any advice or help is appricated.

    Also if you see any eyesores please point them out, even if its the whole thing!

    Thanks.
    Last edited by Charmy; 06-22-2005 at 06:13 PM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thats some very complex and intresting code you have there, I would suggest you post this message on WINDOWS programming board, people on there may be able to help you further, from what I have seen, I really don't know what the problem is either. If you have told the CPU to sleep ie: Sleep(1); then it should work, but I know nought about windows programming, if you go there, you may find a soloution... I wish you luck

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    All sleep does it tell the OS it is ready to take a break. Sleep() takes as an argument the number of miliseconds to sleep, which is meaningless as the typical timeslice on a machine is around 12-15 miliseoncds. If nothing else is going on, the OS just wakes the program up again and it is off to the races. The point of sleeping (actually, Sleep(0) is better than Sleep(1) as it simply releases its timeslice and the OS can just let it keep running) is to give other applications a chance to run (which, btw, the OS will ensure anyway), so unless you have a busy machine AND your application is taking up 98% of the CPU, I wouldn't worry about it. If you are interested, I have a little writeup on performance coding here: http://sol-biotech.com/code/PerformanceProgramming.html.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Much appricated for the responses. Just a side question here, would it change anything if i could somehow set the process/ or thread priorities to low?

    Or.. Perhaps is there a way to make the OS "wake" the program i.e. when it sees activity on the active socket, can it send a signal to the program to bring it out of a sleep type state?

    And thanks for the link to the paper .

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You can use 'select()' on a socket and let the OS handle things for you.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,297
    Your basic problem won't be solved by sleeping (unless you use large values to sleep) or by changing priority of the process.

    The issue is that you have a continuous loop, and are essentially polling for input and writing output. If there is no input to be obtained, your function loops continually (chewing up CPU time) until some appears. Try changing your program so it waits for data, rather than polling for it.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Thank you, any insight on how this is accomplished? It sounds to be exactly what i am looking for.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,297
    As mitakeet said, the select() call can be used: that function waits until a file descriptor changes status (chaing status includes: data comes in, the socket is closed at the other end, etc etc). Essentially, you use select() to wait until data comes in, and then recv() to read whatever has come in.

    Under unix, I would also suggest looking at the fnctl() call: that can be used to change whether the socket is blocking or non-blocking (where blocking means it waits until data is available, rather than returning straight away). I can't recall offhand if that call is available with the winsock library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 08:32 PM
  3. directsound issue
    By valis in forum Tech Board
    Replies: 0
    Last Post: 06-25-2006, 09:28 PM
  4. System Idle Process
    By moonwalker in forum Tech Board
    Replies: 10
    Last Post: 08-09-2003, 08:17 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM

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