Naruhounix

 Naruhounix

オープンソースカンファレンスHokkaido2015 jus研究会札幌大会「なるほどUnixプロセス – RubyでUnixの基礎を学ぼう」の発表資料です。

https://www.ospn.jp/osc2015-do/modules/eguide/event.php?eid=11

動画: https://www.youtube.com/watch?v=3i55DKSIdSY

941b170e5a115a295cccb5f5cdf0a800?s=128

Koji SHIMADA

June 13, 2015
Tweet

Transcript

  1. ͳΔ΄Ͳ 6/*9 ϓϩηε Koji Shimada @snoozer05 3VCZד6OJYך㛇燉׾㷕רֲ 04$)0,,"*%0KVT灇瑔⠓劦䌽㣐⠓

  2. http://ruby-sapporo.org 3VCZ4BQQPSP

  3. http://www.jus.or.jp 傈劤6/*9ِ٦ؠ⠓

  4. ͳΔ΄Ͳ 6/*9 ϓϩηε Koji Shimada @snoozer05 3VCZד6OJYך㛇燉׾㷕רֲ 04$)0,,"*%0KVT灇瑔⠓劦䌽㣐⠓

  5. http://tatsu-zine.com/books/naruhounix

  6. ؔلٖ٦ذ؍ؚٝءأذي׾㹋鄲ׅ׷״ֲז فؚٗٓوぢֽדכזֻגծ6OJY׾ֲתְֿ ה⢪׏גفؚٗٓىؚٝדֹ׷״ֲחז׶׋ ְ➂ぢֽךⱁדׅկ ջז׷קו6OJYفٗإأռⰻ㺁稱➜ l

  7. ؔلٖ٦ذ؍ؚٝءأذي׾㹋鄲ׅ׷״ֲז فؚٗٓوぢֽדכזֻגծ6OJY׾ֲתְֿ ה⢪׏גفؚٗٓىؚٝדֹ׷״ֲחז׶׋ ְ➂ぢֽךⱁדׅկ ջז׷קו6OJYفٗإأռⰻ㺁稱➜ l

  8. http://razokulover.hateblo.jp/entry/2014/11/12/084645

  9. http://yuuki.hatenablog.com/entry/2015-webserver-architecture

  10. 3VCZד㷕ע

  11. http://tatsu-zine.com/books/naruhounix

  12. http://tatsu-zine.com/books/naruhounix 3VCZכ6OJYך㶨⣘דׅկ 殛  3VCZך֮׵ײ׷הֿ׹כ6OJYךءأذي ؝٦ٕװ俑⻉٥䙼䟝׾⿾僥׃גְתׅկ

  13. ͳΔ΄Ͳ 6/*9 ϓϩηε Koji Shimada RubySapporo 3VCZד6OJYך㛇燉׾㷕רֲ 04$)0,,"*%0KVT灇瑔⠓劦䌽㣐⠓

  14. فٗإأ׏ג ♧⡤ו׿ז׮ך

  15.  فٗإأהכ ˖ 㹋遤⚥ךفؚٗٓيך㹋⡤ ˖ ׅץגך؝٦سכفٗإأ♳ד㹋遤ׁ׸׷ % ps -x PID

    TTY TIME CMD 326 ?? 0:20.78 /usr/libexec/UserEventAgent (Aqua) 328 ?? 6:57.66 /usr/sbin/distnoted agent 329 ?? 0:08.13 /usr/sbin/universalaccessd launchd -s 330 ?? 0:44.20 /usr/sbin/cfprefsd agent 333 ?? 1:50.07 /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock 335 ?? 11:16.33 /System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/… 336 ?? 4:03.26 /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder ... ֿך遤遤ָ植㖈㹋遤⚥ךفٗإأ 痥畍չ劤剅ך䩛䒷ֹպ
  16. 3VCZ׾⢪׏ג⡤꿀 % irb > 㹋遤⚥ךJSCךفٗإأ׾然钠דֹ׷ אךة٦ىشٕדJSC׾饯⹛׃גֶֹ ׮ֲאךة٦ىشٕדQT؝وٝس׾㹋遤ׅ׷ % ps PID

    TTY TIME CMD 35558 ttys000 0:00.13 -zsh 38910 ttys000 0:00.53 irb 38956 ttys001 0:00.15 -zsh
  17.  فٗإأחכ*%ָ֮׷ ˖ ぐفٗإأכ♧䠐ז*% فٗإأ*% ׾䭯א ˖ 3VCZדכ1SPDFTTQJEד然钠דֹ׷ % irb

    > Process.pid => 38910 % ps PID TTY TIME CMD 35558 ttys000 0:00.13 -zsh 38910 ttys000 0:00.53 irb 38956 ttys001 0:00.15 -zsh QT؝وٝسדכ1*%⴨ד׉׸׊׸ךفٗإأ*%׾然钠דֹ׷ 痥畍չفٗإأחכ*%ָ֮׷պ
  18.  垥彊أزٔ٦ي ˖ فٗإأחכ垥彊أزٔ٦يָֻ׏אְגֻ׷ ˖ 垥彊Ⰵ⸂ծ垥彊⳿⸂ծ垥彊ؒٓ٦⳿⸂ךא ˖ ׉׸׊׸ךⰅ⳿⸂כ鸐䌢ة٦ىشٕה籬ָ׏גְ׷ فٗإأ λʔϛφϧ

    垥彊Ⰵ⸂ 垥彊⳿⸂ ؗ٦ن٦س ر؍أفٖ؎ 痥畍չفٗإأחכؿ؋؎ٕر؍أؙٔفةָ֮׷պ
  19. > input = STDIN.gets foo => “foo¥n” > STDOUT.puts “bar”

    bar => nil 3VCZ׾⢪׏ג⡤꿀 ס׌׿⢪׏גְ׷QVUTװHFUT׾ծ״׶⿑㺘ח邌植ׅ׷ה˘ 3VCZדכ45%*/ծ45%065ծ45%&33ד⿫撑דֹ׷ > STDIN => #<IO:<STDIN>> > STOUT => #<IO:<STDOUT>>
  20.  ؿ؋؎ٕر؍أؙٔفة ˖ 垥彊Ⰵ⳿⸂׮ؿ؋؎ٕⰅ⳿⸂׮ծ04ַ׵כׅץ גずׄ״ֲז׮ךٔا٦أה׃ג䪔׻׸׷ ˖ فٗإأָ籬ָ׏גְ׷ٔا٦أכչؿ؋؎ٕ ر؍أؙٔفةպהְֲ♧䠐ז侭侧⦼ה׃ג邌 植ׁ׸׷ 痥畍չفٗإأחכؿ؋؎ٕر؍أؙٔفةָ֮׷պ

    ؿ؋؎ٕ ؗ٦ن٦س λʔϛφϧ فٗإأ ر؍أفٖ؎ GE GE GE
  21. 3VCZדכMFOPדؿ؋؎ٕر؍أؙٔفة׾然钠דֹ׷ >> STDIN.fileno => 0 >> STDOUT.fileno => 1 >>

    STDERR.fileno => 2 >> file = File.open(“a.txt”, “w”) => #<File:a.txt> >> file.fileno => 9 㢩鿇ؿ؋؎ٕפךⰅ⳿⸂׮ずׄ״ֲח盖椚ׁ׸גְ׷ֿהָ׻ַ׷ 3VCZ׾⢪׏ג⡤꿀
  22.  ٔت؎ؙٖءّٝ 垥彊Ⰵ⳿⸂⯓׾ؿ؋؎ٕח➰ֽ剏ִ׷ $ wc -l < in.txt Ⰵ⸂ٔت؎ؙٖءّٝ ⳿⸂ٔت؎ؙٖءّٝ

    $ ps > out.txt ず儗ח遤ֲ $ wc -l < in.txt > out.txt
  23. file = File.open(“out.txt”, “w”) STDOUT.reopen(file) puts “ඪ४ग़ྗઌ͕มΘ͍ͬͯΔ͸ͣ” 3VCZ׾⢪׏ג⡤꿀 SFPQFO׾⢪ֲֿהדⰅ⳿⸂⯓׾➰ֽ剏ִ׵׸׷ SFEJSFDUJPO@FYBNQMFSC

    $ ruby redirection_example.rb $ cat out.txt ඪ४ग़ྗઌ͕มΘ͍ͬͯΔ͸ͣ ⹛⡲然钠
  24. ֿֿתדךתה׭

  25. فؚٗٓيכفٗإأה׃ג㹋遤ׁ׸׷ $ ls ة٦ىشַٕ׵؝وٝس׾㹋遤ׅ׷ה˘

  26. فؚٗٓيכفٗإأה׃ג㹋遤ׁ׸׷ MT׾㹋遤ׅ׷ فٗإأ $ ls ة٦ىشַٕ׵؝وٝس׾㹋遤ׅ׷ה˘

  27. فٗإأחכ*%ָ֮׷ MT׾㹋遤ׅ׷ فٗإأ $ ls ϓϩηε*%͸ 

  28. فٗإأכ垥彊أزٔ٦ي׾䭯א فٗإأ $ ls ة٦ىشٕ ر؍أفٖ؎ ؗ٦ن٦س 垥彊Ⰵ⸂ 垥彊⳿⸂

  29. 垥彊أزٔ٦ي׮ؿ؋؎ٕ׮ׅץגָٔا٦أה׃ג盖椚ׁ׸׷ $ ls ة٦ىشٕ 垥彊Ⰵ⸂ 垥彊⳿⸂ ؿ؋؎ٕ ؿ؋؎ٕ⳿⸂ ؗ٦ن٦س ر؍أفٖ؎

  30. ٔا٦أכؿ؋؎ٕر؍أؙٔفةה׃ג盖椚ׁ׸׷ $ ls ة٦ىشٕ 殢湡ךٔا٦أ 殢湡ךٔا٦أ ؿ؋؎ٕ 殢湡ךٔا٦أ ؗ٦ن٦س ر؍أفٖ؎

  31.  فٗإأחכ鋵ְָ׷ ˖ ׅץגךفٗإأחכ鋵הז׷فٗإأָ㶷㖈ׅ׷ ˖ 3VCZדכ1SPDFTTQQJEד然钠דֹ׷ % irb > Process.ppid

    => 38910 % ps -l UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD 501 35558 35557 4006 0 31 0 2481048 3920 - S 0 ttys000 0:00.13 -zsh 501 38910 35558 4046 0 31 0 2497224 12768 - S+ 0 ttys000 0:00.55 irb 501 38956 38955 4006 0 31 0 2491288 4184 - S 0 ttys001 0:00.15 -zsh QT؝وٝسדכ11*%⴨ד鋵فٗإأך*%׾然钠דֹ׷ JSCفٗإأך鋵فٗإأכ[TIفٗإأד֮׷ֿהָ׻ַ׷ 痥畍չفٗإأחכ鋵ְָ׷պ
  32.  荈ⴓָ鋵חז׷ GPSLׅ׷ֿהדծ荈ⴓָ鋵חז׷ֿהָדֹ׷ puts “ࣗ෼: PID(#{Process.pid}) PPID(#{Process.pid})” pid = fork

    do puts “ ࢠ: PID(#{Process.pid}) PPID(#{Process.pid})” end Process.waitpid(pid) GPSL@FYBNQMFSC $ ruby fork_example.rb ࣗ෼: PID(45261) PPID(35558) ɹࢠ: PID(45306) PPID(45261) ⹛⡲然钠 痥畍չفٗإأכ㶨فٗإأ׾⡲׸׷պ
  33.  荈ⴓָ鋵חז׷ GPSLׅ׷ֿהדծ荈ⴓָ鋵חז׷ֿהָדֹ׷ puts “ࣗ෼: PID(#{Process.pid}) PPID(#{Process.pid})” pid = fork

    do puts “ ࢠ: PID(#{Process.pid}) PPID(#{Process.pid})” end Process.waitpid(pid) GPSL@FYBNQMFSC SVCZفؚٗٓي׾ 㹋遤⚥ךفٗإأ SVCZفؚٗٓي׾ 㹋遤⚥ךفٗإأ ך醱醡 SVCZفؚٗٓي׾ 㹋遤⚥ךفٗإأ ך醱醡 GPSLָㄎן⳿ׁ׸׷ה 荈ⴓ׾؝ؾ٦׃ג 倜׃ְفٗإأָ⡲׵׸׷
  34.  荈ⴓָ鋵חז׷ GPSLׅ׷ֿהדծ荈ⴓָ鋵חז׷ֿהָדֹ׷ puts “ࣗ෼: PID(#{Process.pid}) PPID(#{Process.pid})” pid = fork

    do puts “ ࢠ: PID(#{Process.pid}) PPID(#{Process.pid})” end Process.waitpid(pid) GPSL@FYBNQMFSC SVCZفؚٗٓي׾ 㹋遤⚥ךفٗإأ SVCZفؚٗٓي׾ 㹋遤⚥ךفٗإأ ך醱醡 欰䧭ׁ׸׋فٗإأכ ـٗحؙך⚥׌ֽ׾㹋遤ׅ׷ ⯋ךفٗإأכ׉ך䖓׾ 㹋遤׃גְֻ
  35.  㶨⣘חⴽך麣׾遤ַׇ׷ FYFD׾⢪ֲהⴽךفؚٗٓي׾㹋遤ׅ׷فٗإأח 㢌魦ׇׁ׷ֿהָדֹ׷ pid = fork do exec “ps”,

    “-l” puts “͜͜ͷॲཧ͸΋͏ಈ͔ͳ͍” end Process.waitpid(pid) puts “psίϚϯυ͕࣮ߦ͞Εͨ͸ͣ” GPSL@FYFD@FYBNQMFSC $ ruby fork_exec_example.rb fork_exec_example.rb:2: warning: Insecure world writable dir /usr/local/Cellar in PATH, mode 040777 UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD 501 22184 22183 4006 0 31 0 2499544 6856 - S 0 ttys000 0:00.35 -zsh 501 25237 22184 4046 0 31 0 2447816 7080 - S+ 0 ttys000 0:00.05 ruby -w0 fork_exec_example.rb pwdίϚϯυ͕࣮ߦ͞Εͨ͸ͣ ⹛⡲然钠 痥畍չ畭劣فٗإأ׾⡲׷պ
  36. ֿֿתדךתה׭

  37. فٗإأכفٗإأַ׵欰䧭ׁ׸׷ $ ls ة٦ىشַٕ׵؝وٝس׾㹋遤ׅ׷הכ˘

  38. ءٕؑ׾㹋遤׃גְ׷ فٗإأ $ ls فٗإأכفٗإأַ׵欰䧭ׁ׸׷ Ⰵ⸂ׁ׸׋ⰻ㺁׾ءٕؑفٗإأָ鍑ꅸ׃ג 垥彊⳿⸂ 垥彊Ⰵ⸂ MT 垥彊⳿⸂

  39. ءٕؑ׾㹋遤׃גְ׷ فٗإأ $ ls فٗإأכفٗإأַ׵欰䧭ׁ׸׷ GPSL׃גفٗإأ׾醱醡׃ 垥彊Ⰵ⸂ 垥彊⳿⸂ ءٕؑ׾㹋遤׃גְ׷ فٗإأ

    垥彊Ⰵ⸂ 垥彊⳿⸂
  40. ءٕؑ׾㹋遤׃גְ׷ فٗإأ $ ls فٗإأכفٗإأַ׵欰䧭ׁ׸׷ FYFD׃ג؝وٝس׾㹋遤ׅ׷فٗإأח㢌魦ׇׁ׷ 垥彊Ⰵ⸂ 垥彊⳿⸂ MT׾㹋遤׃גְ׷ فٗإأ

    垥彊Ⰵ⸂ 垥彊⳿⸂
  41. ءٕؑ׾㹋遤׃גְ׷ فٗإأ $ ls فٗإأכفٗإأַ׵欰䧭ׁ׸׷ FYFD׃ג؝وٝس׾㹋遤ׅ׷فٗإأח㢌魦ׇׁ׷ 垥彊Ⰵ⸂ 垥彊⳿⸂ MT׾㹋遤׃גְ׷ فٗإأ

    垥彊Ⰵ⸂ 垥彊⳿⸂ 穠卓׾⳿⸂
  42. ءٕؑ׾㹋遤׃גְ׷ فٗإأ $ ls فٗإأכفٗإأַ׵欰䧭ׁ׸׷ Ⳣ椚׾穄ִ׷ה㶨فٗإأכ穄✪ׅ׷ 垥彊Ⰵ⸂ 垥彊⳿⸂

  43. ׈׿ע׾籬ֺさ׻ׇ׷

  44. ػ؎ف

  45.  ػ؎ف ؝وٝسך穠卓׾ⴽך؝وٝسךⰅ⸂חׅ׷ $ ps -x | wc -l QT؝وٝسך穠卓ך遤侧׾XD؝وٝسדؕؐٝزׅ׷

    ֿ׸ָ6/*9ךㆸ㷕ד֮׷կ♧אךֿה׾遤 ְծת׋׉׸׾ֲתֻװ׷فؚٗٓي׾剅 ֽկ⼿锃׃ג⹛ֻفؚٗٓي׾剅ֽկ .%وؗٗ؎ l
  46.  ػ؎ف ؝وٝسך穠卓׾ⴽך؝وٝسךⰅ⸂חׅ׷ וֲװ׏ג ˖ אך؝وٝسך垥彊⳿⸂ה垥彊Ⰵ⸂׾אז־׷ ˖ QJQFءأذي؝٦ٕ׾⢪׏ג׉׸׾㹋植ׅ׷ ˖ 3VCZדכ*0QJQF׾⢪׏גずֿׄהָדֹ׷

    痥畍չفٗإأכ鸐⥋דֹ׷պ
  47. *0QJQF׾אַ׏ג׫׷ > write_io.puts “foo” => nil > write_io.puts “bar” =>

    nil > read_io.gets => “foo¥n” > read_io.gets => “bar¥n” > read_io, write_io = IO.pipe => [#>IO:fd 9>, #<IO:fd 10> ػ؎ف׾⡲䧭ׅ׷ ػ؎فח剅ֹ鴥׬ ػ؎ف׾铣׫⳿ׅ
  48. *0QJQF׾אַ׏ג׫׷ >IO.pipe do |read_io, write_io| > write_io.puts “foo” > write_io.puts

    “bar” > p read_io.gets > p read_io.gets >end “foo¥n” “bar¥n” ـٗحؙ晛
  49. QJQF׾⢪׏גְ׷הֹך؎ً٦آ فٗإأ XSJUF@JP SFBE@JP ػ؎ف ˑGPPdO˒ ˑCBSdO˒

  50. ֿ׿ז괏ח⢪ֲֿהדػ؎ف׾㹋植דֹ׷ فٗإأ" λʔϛφϧ 垥彊Ⰵ⸂ 垥彊⳿⸂ فٗإأ# 垥彊Ⰵ⸂ 垥彊⳿⸂ ػ؎ف

  51. 3VCZ׾⢪׏ג⡤꿀ׅ׷

  52. 3VCZ׾⢪׏ג⡤꿀 IO.pipe do |read_io, write_io| fork do STDOUT.reopen(write_io) exec ‘ps’,

    ‘-x’ end fork do STDIN.reopen(read_io) exec ‘wc’, ‘-l’ end end Process.waitall QJQF@FYBNQMFSC ⹛⡲然钠 $ ruby pipe_example.rb
  53. תה׭

  54. ➙傈װ׏׋ֿה ˖ فؚٗٓيכفٗإأה׃ג㹋遤ׁ׸׷ ˖ فٗإأכ*%׾䭯א ˖ فٗإأכ垥彊أزٔ٦يח籬ָ׏גְ׷ ˖ ٔا٦أכؿ؋؎ٕر؍أؙٔفة׾䭯א ˖

    鋵فٗإأה㶨فٗإأחאְג ˖ GPSL FYFDדⴽךفؚٗٓي׾㹋遤ׅ׷ ˖ فٗإأず㡦ךⰅ⳿⸂׾ػ؎فד籬־ג׫׷
  55. ➙傈װ׵זַ׏׋ֿה ˖ 橆㞮㢌侧ךֿה ˖ ؝وٝسٓ؎ٝ䒷侧ךֿה ˖ فٗإأ꟦ך鸐⥋חאְגך鑫稢 ˖ ءؚشٕךֿה ˖

    ر٦ٌٝفٗإأךֿה ˖ 植㹋⚅歲דך⢽
  56. http://tatsu-zine.com/books/naruhounix

  57. http://www.garbagecollect.jp/~usa/d/201304c.html ջז׷קו6OJYفٗإأռד㷕ע 8JOEPXT晛3VCZך㛇燉

  58. Comming Soon…

  59. ͳΔ΄Ͳ 6/*9 ϓϩηε Koji Shimada RubySapporo 3VCZד6OJYך㛇燉׾㷕רֲ 04$)0,,"*%0KVT灇瑔⠓劦䌽㣐⠓