Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active August 29, 2015 14:08
Show Gist options
  • Save ifyouseewendy/a70ca8cb3a8531081b27 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/a70ca8cb3a8531081b27 to your computer and use it in GitHub Desktop.
Check native threads used by Ruby 1.8 and 1.9. Example from http://www.csinaction.com/2014/10/10/multithreading-in-the-mri-ruby-interpreter

Write a file named two-threads.rb.

t1 = Thread.new{ while true; end }
t2 = Thread.new{ while true; end }

t1.join
t2.join

Check by Ruby 1.8

$ rvm use 1.8.7
$ ruby two-threads.rb &
$ ps uax | grep two-threads
wendi           21148 100.0  0.0  2443676   1936 s019  R+   11:32AM   0:03.49 ruby two-threads.rb
wendi           21153   0.0  0.0  2432784    492 s006  R+   11:32AM   0:00.00 grep --color=auto two-threads

$ ps -M -p 21148
USER    PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND
wendi 21148 s019  100.0 R    31T   0:00.02   0:10.67 ruby two-threads.rb

$ kill 21148

Check by Ruby 1.9

$ rvm use 1.9.3
$ ruby two-threads.rb &
$ ps uax | grep two-threads
wendi           21385 102.1  0.1  2460076   6748 s019  RN   11:33AM   0:01.56 ruby two-threads.rb
wendi           21398   0.0  0.0  2432780    476 s006  R+   11:33AM   0:00.00 grep --color=auto two-threads

$ ps -M -p 21385
USER    PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND
wendi 21385 s019    0.0 S    26T   0:00.01   0:00.03 ruby two-threads.rb
      21385         0.0 S    26T   0:00.00   0:00.00 
      21385        61.2 S    26T   0:00.01   0:03.31 
      21385        38.8 R    26T   0:00.01   0:03.02 

$ kill 21385
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment