Non Blocking Server - High CPU Usage

G

Gordon Beaton

I was trying out the source code from

http://www.owlmountain.com/tutorials/NonBlockingIo.htm#_Toc524339534

It so happens that CPU Usage becomes 100% even if a single client is
connected to the server. It stays at 100% until all clients
disconnect.

I haven't tested the code, but I'll guess the problem is due to the
fact that they register interest in OP_WRITE for every connection.
Remove that and see if the problem goes away.

Normally a connection is writeable until write() fails. It's only then
you should register OP_WRITE, and then only until the connection
becomes writeable again.

There was a thread about this here recently, "NIO busywaiting".

/gordon
 
R

Remon van Vliet

Gordon Beaton said:
I haven't tested the code, but I'll guess the problem is due to the
fact that they register interest in OP_WRITE for every connection.
Remove that and see if the problem goes away.

Normally a connection is writeable until write() fails. It's only then
you should register OP_WRITE, and then only until the connection
becomes writeable again.

There was a thread about this here recently, "NIO busywaiting".

/gordon


It's either the issue mentioned above or you using selectNow instead of
select. There are only two points in time where you want to register for
OP_WRITE namely after a connect/accept (using it as a notification for when
writing is possible) and when a write initially failed so you can try again
once OP_WRITE is selected. At all other times writing should be valid.

Remon van Vliet
 
R

rajatag

Another issue that just came to my mind ... if a write fails and I then
register interest in OP_WRITE, it means that the message that was
supposed to be sent in the failed attempt should be buffered by me?
 
G

Gordon Beaton

Another issue that just came to my mind ... if a write fails and I then
register interest in OP_WRITE, it means that the message that was
supposed to be sent in the failed attempt should be buffered by me?

If write fails, then it's completely up to you to deal with it,
including buffering the message in order to try again later if that's
what you want to do.

/gordon
 
R

rajatag

thanks gordon,

also i have tried removing the OP_WRITE and it works great. CPU usage
is no longer 100%

Thank you so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top