LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-10-2009, 01:52 AM   #1
Manjunath1847
Member
 
Registered: Mar 2007
Posts: 44

Rep: Reputation: 16
CPU usage 100% due to loops in multiple threads


I have an linux application (written in C++), which spawns 60 threads, and all these 60 threads executes an nested loop which iterates around 100 times. Number of iterations is same for all the threads. When I spawn only one thread time taken by application is just 10-20 seconds. When I run with 60 threads time taken increases to more than 1 minute. Also CPU usage goes to 100% and stays there for more than 1 minute till the application exits. When CPU usage is 100% I cannot run any other commands. Since I need to run this application for every 5 minutes I keep hitting this problem. Please can anyone suggest a solution for this. How can I avoid loop? Is there any way to avoid 100% CPU usage inspite of this loop. Please HELP.

Thanks in advance.
 
Old 08-10-2009, 01:57 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Depends what your loops do.
Do you REALLY need 60 copies of the same code all running at once? Its obviously cpu bound, which suggests its some kind of calculation.
We need more info.
 
Old 08-10-2009, 02:06 AM   #3
Manjunath1847
Member
 
Registered: Mar 2007
Posts: 44

Original Poster
Rep: Reputation: 16
Yes I need 60 copies of same code running at same time. My application collects the device data from 60 different devices and loops over the collected data to get the different informations. This data collections happens for every 5 min.
 
Old 08-10-2009, 02:11 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Well, then it should be on a dedicated server system so you don't need to run other (urgent) cmds.

You've got 2 options:
1. slow the processing down a bit so other cmds can be run eg
You could try adding a short sleep at the bottom of each loop to allow other processes to access the cpu.

2. If you REALLY need the processing done in seconds instead of 1 minute, use HW with more cpus.
 
Old 08-10-2009, 02:45 AM   #5
Manjunath1847
Member
 
Registered: Mar 2007
Posts: 44

Original Poster
Rep: Reputation: 16
Thanks chrism01.
Adding small sleep will work fine when number of threads is less than 30. When running more threads sleep also will not help. Since many threads are fighting for CPU, sleep effect is getting nullified by other threads.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
100% CPU due to while loop dhanju Linux - General 4 04-28-2008 04:05 PM
CPU usage constantly at 100% due to application-browser and gnome-panel Perps Suse/Novell 1 05-14-2007 10:55 AM
FF 2.0 100% CPU Usage cexshun Slackware 12 04-05-2007 01:11 AM
CPU usage 100% ecsfan Red Hat 17 11-19-2003 03:06 PM
100 % CPU usage ???? rolandi Linux - Newbie 1 10-07-2003 11:52 PM


All times are GMT -5. The time now is 06:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration