473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

100% CPU

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.

Mar 30 '06 #1
12 2595
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.
Mar 30 '06 #2
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.

Mar 30 '06 #3
you must not use the keyword 'while' looping in your code.
Instead of this , you should use

Mar 30 '06 #4
So what?
"º£·ç" <wa**********@hotmail.com> wrote in message
news:11********************@i39g2000cwa.googlegrou ps.com...
you must not use the keyword 'while' looping in your code.
Instead of this , you should use

Mar 30 '06 #5
?? <wa**********@hotmail.com> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Mar 30 '06 #6
I think u can use sleep() function to do the job in linux/unix

"Jakob Bieling" <ar****************@rot13.com> wrote in message
news:e0*************@news.t-online.com...
?? <wa**********@hotmail.com> wrote:
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
--
jb

(reply address in rot13, unscramble first)

Mar 30 '06 #7
Please do not top-post. Re-arranged:

Yang Jiao <jo*******@gmail.com> wrote:
"Jakob Bieling" <ar****************@rot13.com> wrote in message
?? <wa**********@hotmail.com> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Mar 30 '06 #8
use what? :) thanks for your reply.

海风 wrote:
you must not use the keyword 'while' looping in your code.
Instead of this , you should use


Mar 30 '06 #9
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?

Mar 30 '06 #10
ca****@gmail.com wrote:
use what? :) thanks for your reply.

海风 wrote:
you must not use the keyword 'while' looping in your code.
Instead of this , you should use


I agree 100%. Instead of what you're doing, you should use:
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 30 '06 #11
ca****@gmail.com wrote:
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
--
jb

(reply address in rot13, unscramble first)
Mar 31 '06 #12
ca****@gmail.com wrote:
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
May 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
newbie code -------------------------- #include <iostream> using namespace std; #include <cstring> class aaa {
6
by: jslaybaugh | last post by:
I'm working on an ASP.NET 2.0 application and am having trouble with a very simple table layout. Using ASP.NET 2.0 it defaults to XHTML 1.0 Transitional and I am trying to comply. However, I...
1
by: Hypnotron | last post by:
Dim Dimensions as RectangleF = RectangleF.FromLTRB(-100, 100, 100, -100) debug.writeline( "Height = " & RectangleF.Height.ToString) Why the heck is height -200 ? How can you have a negative...
18
by: P.N. | last post by:
Hi! when i define array (any type) then compilator say that array is to large! ?? i need at least 10.000 instead 100. This is for numeric methods in "C" any sugestion? thx Greetings P
17
by: Mike | last post by:
I'm trying to create a page: Three sections (left, topright and bottomright), each with a heading and scrolling (overflow) content. The size of these sections should be based upon the size of the...
0
by: Markus Olderdissen | last post by:
i want to create my page with 100% height. <table height="100%"works but is not correct by default. i saw various information how to do it with stylesheet. i really have problems to create my page....
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
1
by: psion | last post by:
Hi, We have a gridview on a webpage, which we would like to be 100% of the table cell in which it is placed. When we specify the width to be 100%, this has no effect, but only if we specify a...
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
1
by: Meganutter | last post by:
Hello, i am having some troubles with DIVs. heres my setup i have a wrapper, 900px wide 100% height nested header 100% wide 20px high nested menubar 100%wide 80px high
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.