Search code examples
rubymultithreadingmacosfork

How do get exit code from Ruby's Process.fork()?


As it says on the tin

pid = Process.fork do
  exit 25
end

pid seems positively useless besides telling me what actual system process number the fork ran under. Will Thread be more useful?


Solution

  • From the manual for fork:

    The parent process should use Process.wait to collect the termination statuses of its children or use Process.detach to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.

    So in your case:

    Process.wait(pid).exitstatus
    

    Threads are a completely different thing than a multi-process model. If you're writing parallel code you'll need to determine what your best approach is.