Skip to content

Commit

Permalink
Merge pull request ruby-concurrency#576 from matthewd/popen-close
Browse files Browse the repository at this point in the history
Use block form of IO.popen to prevent zombies
  • Loading branch information
Petr Chalupa authored Nov 11, 2016
2 parents 183f4e6 + 0371ed4 commit 389e87f
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lib/concurrent/utility/processor_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,26 @@ def compute_processor_count
result = WIN32OLE.connect("winmgmts://").ExecQuery(
"select NumberOfLogicalProcessors from Win32_Processor")
result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
elsif File.readable?("/proc/cpuinfo") && (cpuinfo_count = IO.read("/proc/cpuinfo").scan(/^processor/).size) > 0
cpuinfo_count
elsif File.executable?("/usr/bin/nproc")
IO.popen("/usr/bin/nproc --all").read.to_i
elsif File.readable?("/proc/cpuinfo")
IO.read("/proc/cpuinfo").scan(/^processor/).size
IO.popen("/usr/bin/nproc --all", &:read).to_i
elsif File.executable?("/usr/bin/hwprefs")
IO.popen("/usr/bin/hwprefs thread_count").read.to_i
IO.popen("/usr/bin/hwprefs thread_count", &:read).to_i
elsif File.executable?("/usr/sbin/psrinfo")
IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size
IO.popen("/usr/sbin/psrinfo", &:read).scan(/^.*on-*line/).size
elsif File.executable?("/usr/sbin/ioscan")
IO.popen("/usr/sbin/ioscan -kC processor") do |out|
out.read.scan(/^.*processor/).size
end
IO.popen("/usr/sbin/ioscan -kC processor", &:read).scan(/^.*processor/).size
elsif File.executable?("/usr/sbin/pmcycles")
IO.popen("/usr/sbin/pmcycles -m").read.count("\n")
IO.popen("/usr/sbin/pmcycles -m", &:read).count("\n")
elsif File.executable?("/usr/sbin/lsdev")
IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n")
IO.popen("/usr/sbin/lsdev -Cc processor -S 1", &:read).count("\n")
elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i
IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i
IO.popen("/usr/sbin/sysconf NPROC_ONLN", &:read).to_i
elsif File.executable?("/usr/sbin/sysctl")
IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i
IO.popen("/usr/sbin/sysctl -n hw.ncpu", &:read).to_i
elsif File.executable?("/sbin/sysctl")
IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i
IO.popen("/sbin/sysctl -n hw.ncpu", &:read).to_i
else
1
end
Expand All @@ -118,7 +116,7 @@ def compute_processor_count
def compute_physical_processor_count
ppc = case RbConfig::CONFIG["target_os"]
when /darwin1/
IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i
IO.popen("/usr/sbin/sysctl -n hw.physicalcpu", &:read).to_i
when /linux/
cores = {} # unique physical ID / core ID combinations
phy = 0
Expand Down

0 comments on commit 389e87f

Please sign in to comment.