working with unix processes の残りを読んだよ

28
Working With Unix Processes の残りを読んだよ Katsuji Ishikawa <[email protected] >

Upload: katsuji-ishikawa

Post on 20-Jun-2015

720 views

Category:

Documents


1 download

DESCRIPTION

Working With Unix Processes の残りを読んだ。 社内自グループでの勉強会で使用。 本書は Ruby で解説してある中、多少 Python ではどうよと触れるなど。

TRANSCRIPT

Page 1: Working With Unix Processes の残りを読んだよ

Working With Unix Processes の残りを読んだよ

Katsuji Ishikawa <[email protected]>

Page 2: Working With Unix Processes の残りを読んだよ

前回

Page 3: Working With Unix Processes の残りを読んだよ

Orphaned Processesの章まで

Page 4: Working With Unix Processes の残りを読んだよ
Page 5: Working With Unix Processes の残りを読んだよ

続き

Page 6: Working With Unix Processes の残りを読んだよ

menu

• (再)Working With Unix Processes てなに

• 各チャプターまとめ

Page 7: Working With Unix Processes の残りを読んだよ

Working With Unix Processes てなに

• http://workingwithunixprocesses.com/

• What a file descriptor is and how it works.

• When you need a daemon process, and when you don't.

• Creating new processes with fork(2).

• 4 different ways to exit a process.

• The real world concerns of spawning shell commands and how to avoid them.

• High level discussion about the costs and pitfalls of creating processes.

• Defining signal handlers that don't steal from other developers.

• Internals of Resque and Unicorn and how they use this stuff.

• Lots more! It's 130 pages packed with guidelines, sample code, and best practices.

Page 8: Working With Unix Processes の残りを読んだよ

各チャプターまとめ

Page 9: Working With Unix Processes の残りを読んだよ

Processes Are Friendly

• fork(2) は親プロセスのコピー

• メモリ全てまるっと

• かなりオーバーヘッドがある

• モダーンな *nix ではcopy-on-write(CoW)

が採用されている

Page 10: Working With Unix Processes の残りを読んだよ

Processes Are Friendly(cont.)

• (その名の通り)書き換える必要があるまでコピーを遅らせる

• リソース節約♡

• でも・・・

Page 11: Working With Unix Processes の残りを読んだよ

Processes Are Friendly(cont.)

• MRI(~1.8.x) (と Rubinius) は CoW サポートしてない

• GC 周りが CoW に向いてなかったらしい

• なので Ruby Enterprise Edition (REE)作られたり

• Twitter は REE に手を加えた kiji 作ったり

• その後改善されて REE は\(^o^)/オワタ

• http://www.infoq.com/jp/news/2012/03/ruby-eee-eol

Page 12: Working With Unix Processes の残りを読んだよ

Processes Are Friendly(cont.)

• じゃ、Python の GC はどうなのよ

• reference counting / mark-and-sweep らしいけどよくわからん><

Page 13: Working With Unix Processes の残りを読んだよ

Processes Can Wait

• まてるよ

• サンプルコードども

• Process.wait

• pid を返す

Page 14: Working With Unix Processes の残りを読んだよ

Processes Can Wait(cont.)

• Process.wait は pid を返す

• Process.wait2 というのもあるよ

• Process.wait2 は pid と status を返す

• サンプルコード

Page 15: Working With Unix Processes の残りを読んだよ

Processes Can Wait(cont.)

• Process.waitpid, Process.waitpid2 というのもある

• Process.wait, Process.wait2 と実体は同じ。”特定の” pid の子を待つ

Page 16: Working With Unix Processes の残りを読んだよ

Processes Can Wait(cont.)

• 競合問題 - 大丈夫だ、問題ない

• サンプルコード

• *nix プログラミングでは一般的な方法

• babysitting processes

• master/worker

• preforking

• などと呼ぶ

Page 17: Working With Unix Processes の残りを読んだよ

Processes Can Wait(cont.)

• unicorn

• (python なら gunicorn)

• fork した子プロセスがリクエストを捌く

• 応答してるか親がチェック

• 並行性と信頼性の両立

• Ruby の Process.wait は waitpid(2)

Page 18: Working With Unix Processes の残りを読んだよ

Zombie Processes

• (略)m(_ _)m

Page 19: Working With Unix Processes の残りを読んだよ

Processes Can Get Signals

• シグナル

• SIGCHLD をトラップしてみる

• サンプルコード

• シグナルの配送は信頼できない><

Page 20: Working With Unix Processes の残りを読んだよ

Processes Can Get Signals(cont.)

• どれかの動作をする

• 無視する

• 指定のアクションを実行

• デフォルトのアクションを実行

Page 21: Working With Unix Processes の残りを読んだよ

Processes Can Get Signals(cont.)

• シグナルはどこからくるの

• カーネルによって送られる

• ほげプロセス <=> カーネル <=> ふがプロセス

• irb 実行したターミナルを2つ起動してみる

• INT, TERM, KILL

Page 22: Working With Unix Processes の残りを読んだよ

Processes Can Get Signals(cont.)

• シグナルを再定義

• シグナルの無視

• ほか - kill コマンド

Page 23: Working With Unix Processes の残りを読んだよ

Processes Can Get Signals(cont.)

• unicorn だと

• INT で即座に終了

• USR2 でダウンタイムなしの再起動

• TTIN で ワーカーを増やす

• Ruby の Process.kill は kill(2)

Page 24: Working With Unix Processes の残りを読んだよ

Processes Can Communicate

• プロセス間通信

• パイプ

• 一方通行

• irb で試してみる

• IO.pipe は pipe(2)

Page 25: Working With Unix Processes の残りを読んだよ

Daemon Processes

• バックグラウンドで動き続けるプロセス

• Webサーバ、DBサーバ

• GUI ならウィンドウシステムだったり

• いろいろ

• Rack をみてみる (server.rb)

Page 26: Working With Unix Processes の残りを読んだよ

Daemon Processes (cont.)

• 1.9 以降なら Process.daemon だけで済む..

• なにをしてるか追って見る

• Process.setsid は setsid(2), Process.getpgrp

は getpgrp(2), など

Page 27: Working With Unix Processes の残りを読んだよ

Spawing Terminal Processes

• fork + exec

• プロセスを置き換える

• fork でプロセス作成(コピー), exec で変身

• Kernel#system は system(3), Kernel#exec は

execve(2)など

Page 28: Working With Unix Processes の残りを読んだよ

Ending

• 2つ

• 抽象化

• 通信

• ネットワークは別の書籍に..

• Appendix も読もう

• Resque, Unicorn, Prefork サーバについて