Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Last active August 28, 2020 08:48
Show Gist options
  • Select an option

  • Save benoittgt/a8cdfb07845754774b9469de2201885b to your computer and use it in GitHub Desktop.

Select an option

Save benoittgt/a8cdfb07845754774b9469de2201885b to your computer and use it in GitHub Desktop.
Playing with child process in Ruby
pid = Process.pid
puts "Current process pid : #{pid}"
child_pids = []
6.times.each do |i|
child_pids << Process.fork do
Process.setproctitle("ruby childp n #{i} with #{i * 3}s")
puts "sleeping #{i * 3}s"; sleep i * 3
end
end
p Process.waitall
puts "Done"
@benoittgt
Copy link
Author

Will print with while true; do pstree -s ruby; sleep 0.5; clear; done

       \-+= 71447 bti ruby grabbing_pid.rb
         |--- 71556 bti ruby childp n 1 with 3s
         |--- 71557 bti ruby childp n 2 with 6s
         |--- 71558 bti ruby childp n 3 with 9s
         |--- 71559 bti ruby childp n 4 with 12s
         \--- 71560 bti ruby childp n 5 with 15s

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