100% CPU



 DEVELOP > c-Plus-Plus > 100% CPU

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 30 Mar 2006 01:26:48 AM
Object: 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.
.

User: "Sohail Ahmed Siddiqui"

Title: Re: 100% CPU 04 May 2006 11:55:39 PM
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
.

User: "Gernot Frisch"

Title: Re: 100% CPU 30 Mar 2006 03:30:53 AM

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.
.
User: ""

Title: Re: 100% CPU 30 Mar 2006 03:38:05 AM
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.
.
User: "=?utf-8?B?5rW36aOO?="

Title: Re: 100% CPU 30 Mar 2006 04:13:25 AM
you must not use the keyword 'while' looping in your code.
Instead of this , you should use
.
User: "Jakob Bieling"

Title: Re: 100% CPU 30 Mar 2006 04:19:42 AM
?? <wangdongyu3d@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)
.
User: "Yang Jiao"

Title: Re: 100% CPU 30 Mar 2006 04:25:16 AM
I think u can use sleep() function to do the job in linux/unix
"Jakob Bieling" <argfhesNGtzkQBGarg@rot13.com> wrote in message
news:e0gbcq$4k8$01$1@news.t-online.com...

?? <wangdongyu3d@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)

.
User: "Jakob Bieling"

Title: OT: Re: 100% CPU 30 Mar 2006 04:32:34 AM
Please do not top-post. Re-arranged:
Yang Jiao <johninprc@gmail.com> wrote:

"Jakob Bieling" <argfhesNGtzkQBGarg@rot13.com> wrote in message

?? <wangdongyu3d@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)
.
User: ""

Title: Re: OT: Re: 100% CPU 30 Mar 2006 02:50:56 PM
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?
.
User: "Jakob Bieling"

Title: Re: OT: Re: 100% CPU 31 Mar 2006 01:43:21 AM
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)
.





User: "Yang Jiao"

Title: Re: 100% CPU 30 Mar 2006 04:16:21 AM
So what?
"º£·ç" <wangdongyu3d@hotmail.com> wrote in message
news:1143713605.828306.4150@i39g2000cwa.googlegroups.com...

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

.

User: ""

Title: Re: 100% CPU 30 Mar 2006 02:44:23 PM
use what? :) thanks for your reply.
=E6=B5=B7=E9=A3=8E wrote:

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

.
User: "=?UTF-8?B?TWFydGluIErDuHJnZW5zZW4=?="

Title: Re: 100% CPU 30 Mar 2006 05:03:18 PM
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
.






  Page 1 of 1

1

 


Related Articles
~!~!~ 100% FRee - TOP MONEY MAKING SECRET ~!~!~
want to earn money from web?? 100% gurantee
##### Natural Penis enlargement IS NOW POSSIBLE AND 100% SAFE #####
$100
******Earn $100 t0 $500 Per day / Make your PC Ultimate Money MakingMachine ******
the different between aaa m1[100] and aaa *p = new [100]
%%%%% Natural Penis enlargement IS NOW POSSIBLE AND 100% SAFE %%%%%
Why is for(int i=0; i < 100; ++i) poor?
How to replace: #if VAR1 == 100 ... #elif VAR2 = 1 ... #endif, With #ifdef VAR1 ... ? ... #endif
How to optimize? Object gets constructed a 100 million times
##### Natural Penis enlargement IS NOW POSSIBLE AND 100% SAFE #####
Earn Up To 100's of $ per Day with Ease!
class A { private: static const int a[] = {100,200}; };
Speed up you typing speed more than 100 wpm
Gow i can free allocated by new [1][100]
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER