Skip to content

Instantly share code, notes, and snippets.

@ilake
Last active December 16, 2015 09:29
Show Gist options
  • Select an option

  • Save ilake/5413602 to your computer and use it in GitHub Desktop.

Select an option

Save ilake/5413602 to your computer and use it in GitHub Desktop.

Process friendly, can wait

Process.wait is a blocking call instructing the parent process to wait for one of its child processes to exit before continuing.

Process.wait blocks until any one of its child processes exit

you can pass -1 as the pid to Process.waitpid to get it to wait for any child process.

The kernel queues up information about exited processes so that the parent always receives the information in the order that the children exited.

calling any variant of Process.wait when there are no child processes will raise Errno::ECHILD .

Appendix: How Unicorn Reaps Worker Processes

pre-forking web server.

you boot it up and tell it how many worker processes you would like it to have

initializing its network sockets and loading your application.

it uses fork(2) to create the worker processes. It uses the master-worker pattern.

if a parent process doesn't kill its children before it exits they will continue on without stopping.

Unicorn stores a list of currently active worker processes in its WORKERS constant. WORKERS is a hash where the key is the pid of the worker process and the value is an instance of Unicorn::Worker.

http://ruby-doc.org/core-1.9.3/Process.html

 Process::WNOHANG (do not block if no child available) or Process::WUNTRACED (return stopped children that haven’t been reported).

 include Process
 fork { exit 99 }                 #=> 27429
 wait                             #=> 27429
 $?.exitstatus                    #=> 99

 pid = fork { sleep 3 }           #=> 27440
 Time.now                         #=> 2008-03-08 19:56:16 +0900
 waitpid(pid, Process::WNOHANG)   #=> nil
 Time.now                         #=> 2008-03-08 19:56:16 +0900
 waitpid(pid, 0)                  #=> 27440
 Time.now                         #=> 2008-03-08 19:56:19 +0900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment