Skip to content

Instantly share code, notes, and snippets.

Daemon Process

  • run in the background
  • not under the control of a user at a terminal.
  • When the kernel is bootstrapped it spawns a process called the init process. This process has a ppid of 0 and is the 'grandparent of all processes'. It's the first one and it has no ancestor. Its pid is 1 .

Diving into Rack

  def daemonize_app
    if RUBY_VERSION < "1.9"

exit if fork

Pipe

  • uni-directional stream of data

  • data can be passed along the pipe but only in one direction. So if one process 'claims' the position of reader, rather than writer, it will not be able to write to the pipe. And vice versa.

        reader, writer = IO.pipe #=> [#<IO:fd 5>, #<IO:fd 6>]
    
  • Ruby's amazing IO class 1 is the superclass to File, TCPSocket, UDPSocket, and others. As such, all of these resources have a common interface.

        reader, writer = IO.pipe 
    

writer.write("Into the pipe I go...")

Process get signals

  • Process.wait. it is a blocking call: it will not return until a child process dies.
  • Signal delivery is unreliable.
  • your code is handling a CHLD signal while another child process dies you may or may not receive a second CHLD signal.
  • signals are sent by the kernel
  • Signals are sent from one process to another process, using the kernel as a middleman.
  • trapping a signal is a bit like using a global variable
  • global variables signal handlers can't be namespaced
  • If you simply want to wire up some behaviour to clean up resources before exiting you can use an at_exit hook
# Set the prefix to ^A.
unbind C-b
set -g prefix ^a
set -g base-index 1
# other ^A
unbind ^A
bind ^A last-window

Zombie Processes

A Child Process existed but the parent doesn't wait it or detach it.

If you're not going to wait for a child process to exit using Process.wait (or the technique described in the next chapter) then you need to 'detach' that child process.

What does Process.detach do? It simply spawns a new thread whose sole job is to wait for the child process specified by pid to exit. This ensures that the kernel doesn't hang on to any status information we don't need.

  ps -ho pid,state -p [pid of zombie process]

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 .

  • 3:46

    • each Rails process is actually single-threaded and handle one concurrent request, if use thread-safe we could really run multiple threads in a single Rails process
    • memory usages increases linearly with each process
  • 5:30 fork and copy-on-write

    • child process will still share most it memory with parent and only add another memory page for the value changed
  • 5:50 how perl work

Mongodb ReplicaSet

No Arbiter Version

Run 3 mongod instances in local machine.

You would get 3 data nodes

Run the first one

mongod --replSet myreplset --rest

brew install libxml2 libxslt
brew link libxml2 libxslt
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar xvfz libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/Cellar/libiconv/1.13.1
make
sudo make install
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2