--- !ruby/object:RI::MethodDescription
aliases: []
block_params:
comment:
- !ruby/struct:SM::Flow::P
body: Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait(). If the parent never collects this status, the child stays around as a zombie process. Process::detach prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach only when you do not intent to explicitly wait for the child to terminate. detach only checks the status periodically (currently once each second).
- !ruby/struct:SM::Flow::P
body: The waiting thread returns the exit status of the detached process when it terminates, so you can use Thread#join to know the result. If specified pid is not a valid child process ID, the thread returns nil immediately.
- !ruby/struct:SM::Flow::P
body: In this first example, we don't reap the first child process, so it appears as a zombie in the process status display.
- !ruby/struct:SM::Flow::VERB
body: " p1 = fork { sleep 0.1 }\n p2 = fork { sleep 0.2 }\n Process.waitpid(p2)\n sleep 2\n system("ps -ho pid,state -p #{p1}")\n"
- !ruby/struct:SM::Flow::P
body: produces:
- !ruby/struct:SM::Flow::VERB
body: " 27389 Z\n"
- !ruby/struct:SM::Flow::P
body: In the next example, Process::detach is used to reap the child automatically.
- !ruby/struct:SM::Flow::VERB
body: " p1 = fork { sleep 0.1 }\n p2 = fork { sleep 0.2 }\n Process.detach(p1)\n Process.waitpid(p2)\n sleep 2\n system("ps -ho pid,state -p #{p1}")\n"
- !ruby/struct:SM::Flow::P
body: (produces no output)
full_name: Process::detach
is_singleton: true
name: detach
params: |
Process.detach(pid) => thread
visibility: public