Strange consumption CPU for a quite small program

  • Thread starter Guillaume Ebuprofen
  • Start date
G

Guillaume Ebuprofen

Hello,

Below my little program for Linux (because I use ps command)

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
require 'socket'

Thread.abort_on_exception=true

pid=Process.fork do
##Processus 1
thread=Thread.new do
tcp_server = TCPServer.new('0.0.0.0',10000)
socket = tcp_server.accept
end
thread.join
end

#Porcessus 2
while true
output=''
IO.popen("ps -p #{pid} -o pcpu,size") do |pipe|
output=pipe.read
end
puts output
sleep(1)
end
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

When I execute this program (with ruby 1.8.7 or 1.9.2) and after 10
steps, I can see process 1 consumes CPU, why ?

Best regards
 
R

Robert Klemme

Hello,

Below my little program for Linux (because I use ps command)

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
require 'socket'

Thread.abort_on_exception=3Dtrue

pid=3DProcess.fork do
=A0##Processus 1
=A0thread=3DThread.new do
=A0 =A0tcp_server =3D TCPServer.new('0.0.0.0',10000)
=A0 =A0socket =3D tcp_server.accept
=A0end
=A0thread.join
end

#Porcessus 2
while true
=A0output=3D''
=A0IO.popen("ps -p #{pid} -o pcpu,size") do |pipe|
=A0 =A0output=3Dpipe.read
=A0end
=A0puts output
=A0sleep(1)
end
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

When I execute this program (with ruby 1.8.7 or 1.9.2) and after 10
steps, I can see process 1 consumes CPU, why ?

I see

15:08:16 Temp$ ruby19 so.rb
so.rb:8: [BUG] native_mutex_unlock return non-zero: 1
ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-cygwin]

-- control frame ----------
c:0005 p:---- s:0015 b:0015 l:000014 d:000014 CFUNC :initialize
c:0004 p:---- s:0013 b:0013 l:000012 d:000012 CFUNC :new
c:0003 p:0019 s:0008 b:0008 l:000764 d:000007 BLOCK so.rb:8
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:---- s:0002 b:0002 l:000001 d:000001 TOP
---------------------------
-- Ruby level backtrace information----------------------------------------=
-
so.rb:8:in `initialize'
so.rb:8:in `new'
so.rb:8:in `block (2 levels) in <main>'

[NOTE]
You may encounter a bug of Ruby interpreter. Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html


Btw, why don't you just do

require 'socket'

Thread.abort_on_exception=3Dtrue

pid=3Dfork do
tcp_server =3D TCPServer.new('0.0.0.0',10000)
socket =3D tcp_server.accept
end

#Porcessus 2
loop do
system "ps", "-p", pid.to_s, "-o", "pcpu,size"
sleep(1)
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top