100% CPU

C

calcop

Hello everyone,

Could someone please point me in the right direction? Here is my
problem. I have created a telnet server for a records management
system. To handle multi users, I made this server thread out each
connection. After threading the connection it threads out to handle the
user interaction. The connection thread is constantly reading data from
the socket and putting it into a variable contained into a class called
socketBuffer.

I have two functions to read data. They are getChar() and getString(int
stringLength). The getChar() function, for example, loops until the
length of socketBuffer is one. It then returns character received
though the socket connection. The getString function does the same
thing, but returns the value when the length equals the specified
length.

Anyway, when I call getChar and getString, the CPU peaks at 100% ? Is
there any better way to do this? Or any other way to wait until I
receive the data?

Please help, if you can.
 
G

Gernot Frisch

Anyway, when I call getChar and getString, the CPU peaks at 100% ?
Is
there any better way to do this? Or any other way to wait until I
receive the data?

In order to release the CPU, you must have some message handling or
sleep command wich is out of C++ language specs. I guess you're better
of with a newsgroup for the platform you are programming on.
Please correct me if I'm wrong.
 
C

calcop

I think you are right, but I am trying to make a cross platform
application :p Oh well, I guess I have to find some way to do it.
 
G

Guest

you must not use the keyword 'while' looping in your code.
Instead of this , you should use
 
J

Jakob Bieling

?? said:
you must not use the keyword 'while' looping in your code.
Instead of this , you should use

Sure you can use while without hogging all the CPU resources. Just
use proper wait functions.

<OT>

int sd = ..;
char buf [100];
while (true)
{
int e = recv (sd, buf, sizeof (buf), 0);
if (e <= 0)
break;
}

receives data on a blocking socket until the connection is closed or
an error occurs. Here, recv is also the 'wait function', as it waits
until data arrives. Unless there is lots and lots of stuff to receive,
the CPU resources will hardly be used.

</OT>

regards
 
Y

Yang Jiao

I think u can use sleep() function to do the job in linux/unix

Jakob Bieling said:
?? said:
you must not use the keyword 'while' looping in your code.
Instead of this , you should use

Sure you can use while without hogging all the CPU resources. Just use
proper wait functions.

<OT>

int sd = ..;
char buf [100];
while (true)
{
int e = recv (sd, buf, sizeof (buf), 0);
if (e <= 0)
break;
}

receives data on a blocking socket until the connection is closed or an
error occurs. Here, recv is also the 'wait function', as it waits until
data arrives. Unless there is lots and lots of stuff to receive, the CPU
resources will hardly be used.

</OT>

regards
 
J

Jakob Bieling

Please do not top-post. Re-arranged:

Yang Jiao said:
you must not use the keyword 'while' looping in your code.
Instead of this , you should use

Sure you can use while without hogging all the CPU resources.
Just use proper wait functions.

<OT>

int sd = ..;
char buf [100];
while (true)
{
int e = recv (sd, buf, sizeof (buf), 0);
if (e <= 0)
break;
}

receives data on a blocking socket until the connection is closed
or an error occurs. Here, recv is also the 'wait function', as it
waits until data arrives. Unless there is lots and lots of stuff to
receive, the CPU resources will hardly be used.

</OT>
I think u can use sleep() function to do the job in linux/unix

You do not need to. On a blocking socket, this will not use 100% CPU
resources (unless there is that much work), whether you are on Windows,
Linux or any other platform that supports Berkeley sockets. On a
non-blocking socket, you would probably use other methods than 'sleep'.

regards
 
C

calcop

I know reading data from the socket is automaticly blocked until data
comes. But, I have a function that is looking at a variable called:
socketBuffer. socketBuffer is populated by the socket when data
arrives. The loop continues to monitor socketBuffer until it is atleast
one character long or, in another function until it is the length
supplied by the function caller.

How do I implimnet a block into my loop? So basicly my loop looks like
this and it is taking 100% CPU:

char XNode::readChar() {
char charToRead;
while(socketBuffer.length() == 0) {
// Just waiting until the length is over 0.
}
charToRead = *socketBuffer.c_str();
clearNodeBuffer();
return charToRead;
}

So, is there a better way to wait until the length of socketBuffer is 1?
 
J

Jakob Bieling

I know reading data from the socket is automaticly blocked until data
comes. But, I have a function that is looking at a variable called:
socketBuffer. socketBuffer is populated by the socket when data
arrives. The loop continues to monitor socketBuffer until it is
atleast one character long or, in another function until it is the
length supplied by the function caller.

How do I implimnet a block into my loop? So basicly my loop looks like
this and it is taking 100% CPU:

char XNode::readChar() {
char charToRead;
while(socketBuffer.length() == 0) {
// Just waiting until the length is over 0.
}
charToRead = *socketBuffer.c_str();
clearNodeBuffer();
return charToRead;
}

So, is there a better way to wait until the length of socketBuffer is
1?

Take a look at the different synchronization methods (depending on
the platform you are working on). Then 'readChar' can block on a
synchronization handle which will be unblocked by the code that gets the
data from the socket.

You should probably post further questions about this in a newsgroup
that deals with the particular platform you are using. For Windows
(using Winsock), alt.winsock.programming and
comp.os.ms-windows.programmer.win32 are good places to start.

hth
 
S

Sohail Ahmed Siddiqui

Hello everyone,

Could someone please point me in the right direction? Here is my
problem. I have created a telnet server for a records management
system. To handle multi users, I made this server thread out each
connection. After threading the connection it threads out to handle the
user interaction. The connection thread is constantly reading data from
the socket and putting it into a variable contained into a class called
socketBuffer.

I have two functions to read data. They are getChar() and getString(int
stringLength). The getChar() function, for example, loops until the
length of socketBuffer is one. It then returns character received
though the socket connection. The getString function does the same
thing, but returns the value when the length equals the specified
length.

Anyway, when I call getChar and getString, the CPU peaks at 100% ? Is
there any better way to do this? Or any other way to wait until I
receive the data?

Please help, if you can.

Hi,
why not use accept, select/pool functions to wait and read data from socket?
after you accept a connection on a socket put in a new thread, you can read
and write data with socket library functions like read/write or send/recv
Br
Sohail
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top