Page 1 of 1

100% CPU Usage...

Posted: Wed Jan 10, 2007 11:23 am
by Alexis Blaze
hi all.. i was developing a new software for my college project.. it was acting as a middletier between application and database for safety reason... the software works fine but when i look at task manager->performance, when my software ran, cpu time will rise up to 100%...

here's a little look at how my software works logically...

BEGIN LISTING
-----------------------------------------------------------------------------
// this will load up some configuration and prepare the socket
middleTier.InitMiddleTier();

while(!middleTier.isShutDown()) {
// here i will see if the socket active (is any client trying to connect)
if(middleTier.peekSocket()) {
// if the socket is active, accept the connection, then create a thread
// for handling further request from that client
middleTier.addClient();
}
}

// the middleTier has been closed/died, cleanup
middleTier.cleanUp();
-------------------------------------------------------------------------------
END LISTING

can you spot the problem why my software would eat up cpu time to 100%?

Posted: Wed Jan 10, 2007 1:03 pm
by Krzysztof Duleba
You should be using select instead of active waiting. Use Google to find more info about it.

Posted: Sat Jan 13, 2007 5:58 pm
by Alexis Blaze
The middleTier.peekSocket() method is actually a select with error handling, but it's on unblocking state...

The problem is, if the select is blocking, then when i stop the application then it will still waiting for a client...

Can we somehow notify the select to stop waiting?

Posted: Sun Jan 14, 2007 6:31 am
by Krzysztof Duleba
Select would be useless if it couldn't handle it, would it? You can use exception sets, you can set a timeout. Read the manual or look for examples, there are plenty of them on the web.