Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 762441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:57:30+00:00 2026-05-14T15:57:30+00:00

I work on shared linux machines with between 4 and 24 cores. To make

  • 0

I work on shared linux machines with between 4 and 24 cores. To make best use of them, I use the following code to detect the number of processors from my ruby scripts:

return `cat /proc/cpuinfo | grep processor | wc -l`.to_i

(perhaps there is a pure-ruby way of doing this?)

But sometimes a colleague is using six or eight of the 24 cores. (as seen via top). How can I get an estimate of the number of currently unused processors that I can use without making anyone upset?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:57:31+00:00Added an answer on May 14, 2026 at 3:57 pm

    You can use the data in the /proc filesystem to get CPU affinity info for running processes. The following should give you the number of CPUs currently in use (Note: I don’t have a Linux or Ruby box handy so this code is untested, but you can get the idea):

    def processors_in_use
        procs=[]
        Dir.glob("/proc/*/stat") {|filename|
            next if File.directory?(filename)
            this_proc=[]
            File.open(filename) {|file| this_proc = file.gets.split.values_at(2,38)}
            procs << this_proc[1].to_i if this_proc[0]=="R"
        }
        procs.uniq.length
    end
    
    def num_processors
        IO.readlines("/proc/cpuinfo").delete_if{|x| x.index("processor")==nil}.length
    end
    
    def num_free_processors
        num_processors - processors_in_use
    end
    
    def estimate_free_cpus(count, waittime)
        results=[]
        count.times {
            results << num_free_processors
            sleep(waittime)
        }
        sum=0
        results.each {|x| sum += x}
        (sum.to_f / results.length).round
    end
    

    Edit: I verified that the above code works (I was using Ruby 1.9)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The company I work for currently uses Go To Meeting to share our desktops
Where I work I have several projects in separate repositories. Each project shares a
At work we are currently still using JUnit 3 to run our tests. We
At work we are being asked to create XML files to pass data to
I work in VBA, and want to parse a string eg <PointN xsi:type='typens:PointN' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
I work with C# at work but dislike how with webforms it spews out
At work, we have multiple branches that we may be working on at any
I work on a complex application where different teams work on their own modules
I work for a .NET/MSSQL shop that has trouble supporting customers running Novell, partially
I work a lot with network and serial communications software, so it is often

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.