Unix/Linux Go Back    

Programming Post questions about C, C++, Java, SQL, and other programming languages here.

while loop and CPU ..


Thread Tools Search this Thread Display Modes
Old Unix and Linux 08-27-2008
nivea_jmd nivea_jmd is offline
Registered User
Join Date: Jan 2008
Last Activity: 27 August 2008, 12:52 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
while loop and CPU ..

Hello frnds..

i m using while loops in my code.. bec of that my application uses almost 90% of CPU resources.. its not lettin others applications to run..

actually i have to read some signal from serial port... n till i dont get it high or low..
i have to read that signal continuously... its for this reason i m using while loops.

else if(status==0)

some code


someone suggested me to use sleep in the while loop..
i cant use it bec we are workin on realtime systems...
so we usually avoid using delay n sleep....

can anyone help me on this ???

my application is on C++/fedora
Sponsored Links
Old Unix and Linux 08-27-2008
otheus's Unix or Linux Image
otheus otheus is offline Forum Advisor  
Join Date: Feb 2007
Last Activity: 6 September 2017, 5:43 AM EDT
Location: Innsbruck, Austria
Posts: 2,157
Thanks: 12
Thanked 51 Times in 48 Posts
The standard call is select(). Do a "man 2 select". It's a little difficult to understand at first, and maybe you have only one file descriptor to wait on, so you can do a man on poll(), but it's not portable to older UNIX's. Here, you can do:

struct pollfd pollfd_list[] = { 0 /*stdin */, ~0 /* all events */, 0 };
poll( pollfd_list, 1 /* 1 file descriptor */, 1000 /* 1 second */ );

This will return at the latest every second (unless your process is pushed out due to other processes -- Linux isn't realtime) and whether or not you have something to do depends on the last field in pollfd_list.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
S# in a for loop - concatenate $(loop counter) fight4love Shell Programming and Scripting 5 07-05-2011 11:42 PM
BASH loop inside a loop question rethink Shell Programming and Scripting 4 09-15-2010 07:58 AM
Null Handling in Until loop. . .loop won't stop brandono66 Shell Programming and Scripting 4 11-24-2009 03:57 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 04:59 PM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 11:09 AM

All times are GMT -4. The time now is 11:11 AM.