U:RDoc::AnyMethod[iI"detach:ETI"Process::detach;TT:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"BSome operating systems retain the status of terminated child ;TI"Eprocesses until the parent collects that status (normally using ;TI"Hsome variant of wait()
). If the parent never collects ;TI"Gthis status, the child stays around as a zombie process. ;TI"@Process::detach
prevents this by setting up a ;TI"Fseparate Ruby thread whose sole job is to reap the status of the ;TI"?process _pid_ when it terminates. Use detach
;TI"Eonly when you do not intend to explicitly wait for the child to ;TI"terminate.;To:RDoc::Markup::BlankLine o;
; [ I"HThe waiting thread returns the exit status of the detached process ;TI"Dwhen it terminates, so you can use Thread#join
to ;TI"Gknow the result. If specified _pid_ is not a valid child process ;TI".ID, the thread returns +nil+ immediately.;T@o;
; [I"JThe waiting thread has pid
method which returns the pid.;T@o;
; [I"FIn this first example, we don't reap the first child process, so ;TI":it appears as a zombie in the process status display.;T@o:RDoc::Markup::Verbatim; [
I"p1 = fork { sleep 0.1 }
;TI"p2 = fork { sleep 0.2 }
;TI"Process.waitpid(p2)
;TI"
sleep 2
;TI")system("ps -ho pid,state -p #{p1}")
;T:@format0o;
; [I"produces:;T@o;; [I"
27389 Z
;T;
0o;
; [I"GIn the next example, Process::detach
is used to reap ;TI"the child automatically.;T@o;; [I"p1 = fork { sleep 0.1 }
;TI"p2 = fork { sleep 0.2 }
;TI"Process.detach(p1)
;TI"Process.waitpid(p2)
;TI"
sleep 2
;TI")system("ps -ho pid,state -p #{p1}")
;T;
0o;
; [I""(produces no output);T:
@fileI"process.c;T:0@omit_headings_from_table_of_contents_below0I"%Process.detach(pid) -> thread
;T0[ I" (p1);T@@FI"Process;TcRDoc::NormalModule00