Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Koji SHIMADA
PRO

June 13, 2015
Tweet

More Decks by Koji SHIMADA

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. 3VCZד㷕ע

    View Slide

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

    View Slide

  12. http://tatsu-zine.com/books/naruhounix
    3VCZכ6OJYך㶨⣘דׅկ 殛

    3VCZך֮׵ײ׷הֿ׹כ6OJYךءأذي
    ؝٦ٕװ俑⻉٥䙼䟝׾⿾僥׃גְתׅկ

    View Slide

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

    View Slide

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

    View Slide


  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
    ...
    ֿך遤遤ָ植㖈㹋遤⚥ךفٗإأ
    痥畍չ劤剅ך䩛䒷ֹպ

    View Slide

  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

    View Slide


  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*%⴨ד׉׸׊׸ךفٗإأ*%׾然钠דֹ׷
    痥畍չفٗإأחכ*%ָ֮׷պ

    View Slide


  18. 垥彊أزٔ٦ي
    ˖ فٗإأחכ垥彊أزٔ٦يָֻ׏אְגֻ׷
    ˖ 垥彊Ⰵ⸂ծ垥彊⳿⸂ծ垥彊ؒٓ٦⳿⸂ךא
    ˖ ׉׸׊׸ךⰅ⳿⸂כ鸐䌢ة٦ىشٕה籬ָ׏גְ׷
    فٗإأ
    λʔϛφϧ
    垥彊Ⰵ⸂
    垥彊⳿⸂
    ؗ٦ن٦س
    ر؍أفٖ؎
    痥畍չفٗإأחכؿ؋؎ٕر؍أؙٔفةָ֮׷պ

    View Slide

  19. > input = STDIN.gets
    foo
    => “foo¥n”
    > STDOUT.puts “bar”
    bar
    => nil
    3VCZ׾⢪׏ג⡤꿀
    ס׌׿⢪׏גְ׷QVUTװHFUT׾ծ״׶⿑㺘ח邌植ׅ׷ה˘
    3VCZדכ45%*/ծ45%065ծ45%&33ד⿫撑דֹ׷
    > STDIN
    => #>
    > STOUT
    => #>

    View Slide


  20. ؿ؋؎ٕر؍أؙٔفة
    ˖ 垥彊Ⰵ⳿⸂׮ؿ؋؎ٕⰅ⳿⸂׮ծ04ַ׵כׅץ
    גずׄ״ֲז׮ךٔا٦أה׃ג䪔׻׸׷
    ˖ فٗإأָ籬ָ׏גְ׷ٔا٦أכչؿ؋؎ٕ
    ر؍أؙٔفةպהְֲ♧䠐ז侭侧⦼ה׃ג邌
    植ׁ׸׷
    痥畍չفٗإأחכؿ؋؎ٕر؍أؙٔفةָ֮׷պ
    ؿ؋؎ٕ
    ؗ٦ن٦س
    λʔϛφϧ
    فٗإأ
    ر؍أفٖ؎
    GE
    GE
    GE

    View Slide

  21. 3VCZדכMFOPדؿ؋؎ٕر؍أؙٔفة׾然钠דֹ׷
    >> STDIN.fileno
    => 0
    >> STDOUT.fileno
    => 1
    >> STDERR.fileno
    => 2
    >> file = File.open(“a.txt”, “w”)
    => #
    >> file.fileno
    => 9
    㢩鿇ؿ؋؎ٕפךⰅ⳿⸂׮ずׄ״ֲח盖椚ׁ׸גְ׷ֿהָ׻ַ׷
    3VCZ׾⢪׏ג⡤꿀

    View Slide


  22. ٔت؎ؙٖءّٝ
    垥彊Ⰵ⳿⸂⯓׾ؿ؋؎ٕח➰ֽ剏ִ׷
    $ wc -l < in.txt
    Ⰵ⸂ٔت؎ؙٖءّٝ
    ⳿⸂ٔت؎ؙٖءّٝ
    $ ps > out.txt
    ず儗ח遤ֲ
    $ wc -l < in.txt > out.txt

    View Slide

  23. file = File.open(“out.txt”, “w”)
    STDOUT.reopen(file)
    puts “ඪ४ग़ྗઌ͕มΘ͍ͬͯΔ͸ͣ”
    3VCZ׾⢪׏ג⡤꿀
    SFPQFO׾⢪ֲֿהדⰅ⳿⸂⯓׾➰ֽ剏ִ׵׸׷
    [email protected]
    $ ruby redirection_example.rb
    $ cat out.txt
    ඪ४ग़ྗઌ͕มΘ͍ͬͯΔ͸ͣ
    ⹛⡲然钠

    View Slide

  24. ֿֿתדךתה׭

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


  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فٗإأד֮׷ֿהָ׻ַ׷
    痥畍չفٗإأחכ鋵ְָ׷պ

    View Slide


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

    View Slide


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

    View Slide


  34. 荈ⴓָ鋵חז׷
    GPSLׅ׷ֿהדծ荈ⴓָ鋵חז׷ֿהָדֹ׷
    puts “ࣗ෼: PID(#{Process.pid}) PPID(#{Process.pid})”
    pid = fork do
    puts “ ࢠ: PID(#{Process.pid}) PPID(#{Process.pid})”
    end
    Process.waitpid(pid)
    [email protected]
    SVCZفؚٗٓي׾
    㹋遤⚥ךفٗإأ
    SVCZفؚٗٓي׾
    㹋遤⚥ךفٗإأ
    ך醱醡
    欰䧭ׁ׸׋فٗإأכ
    ـٗحؙך⚥׌ֽ׾㹋遤ׅ׷
    ⯋ךفٗإأכ׉ך䖓׾
    㹋遤׃גְֻ

    View Slide


  35. 㶨⣘חⴽך麣׾遤ַׇ׷
    FYFD׾⢪ֲהⴽךفؚٗٓي׾㹋遤ׅ׷فٗإأח
    㢌魦ׇׁ׷ֿהָדֹ׷
    pid = fork do
    exec “ps”, “-l”
    puts “͜͜ͷॲཧ͸΋͏ಈ͔ͳ͍”
    end
    Process.waitpid(pid)
    puts “psίϚϯυ͕࣮ߦ͞Εͨ͸ͣ”
    [email protected]@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ίϚϯυ͕࣮ߦ͞Εͨ͸ͣ
    ⹛⡲然钠
    痥畍չ畭劣فٗإأ׾⡲׷պ

    View Slide

  36. ֿֿתדךתה׭

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  44. ػ؎ف

    View Slide


  45. ػ؎ف
    ؝وٝسך穠卓׾ⴽך؝وٝسךⰅ⸂חׅ׷
    $ ps -x | wc -l
    QT؝وٝسך穠卓ך遤侧׾XD؝وٝسדؕؐٝزׅ׷
    ֿ׸ָ6/*9ךㆸ㷕ד֮׷կ♧אךֿה׾遤
    ְծת׋׉׸׾ֲתֻװ׷فؚٗٓي׾剅
    ֽկ⼿锃׃ג⹛ֻفؚٗٓي׾剅ֽկ
    .%وؗٗ؎
    l

    View Slide


  46. ػ؎ف
    ؝وٝسך穠卓׾ⴽך؝وٝسךⰅ⸂חׅ׷
    וֲװ׏ג
    ˖ אך؝وٝسך垥彊⳿⸂ה垥彊Ⰵ⸂׾אז־׷
    ˖ QJQFءأذي؝٦ٕ׾⢪׏ג׉׸׾㹋植ׅ׷
    ˖ 3VCZדכ*0QJQF׾⢪׏גずֿׄהָדֹ׷
    痥畍չفٗإأכ鸐⥋דֹ׷պ

    View Slide

  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>, #
    ػ؎ف׾⡲䧭ׅ׷
    ػ؎فח剅ֹ鴥׬
    ػ؎ف׾铣׫⳿ׅ

    View Slide

  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”
    ـٗحؙ晛

    View Slide

  49. QJQF׾⢪׏גְ׷הֹך؎ً٦آ
    فٗإأ
    [email protected]
    [email protected]
    ػ؎ف
    ˑGPPdO˒
    ˑCBSdO˒

    View Slide

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

    View Slide

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

    View Slide

  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
    [email protected]
    ⹛⡲然钠
    $ ruby pipe_example.rb

    View Slide

  53. תה׭

    View Slide

  54. ➙傈װ׏׋ֿה
    ˖ فؚٗٓيכفٗإأה׃ג㹋遤ׁ׸׷
    ˖ فٗإأכ*%׾䭯א
    ˖ فٗإأכ垥彊أزٔ٦يח籬ָ׏גְ׷
    ˖ ٔا٦أכؿ؋؎ٕر؍أؙٔفة׾䭯א
    ˖ 鋵فٗإأה㶨فٗإأחאְג
    ˖ GPSLFYFDדⴽךفؚٗٓي׾㹋遤ׅ׷
    ˖ فٗإأず㡦ךⰅ⳿⸂׾ػ؎فד籬־ג׫׷

    View Slide

  55. ➙傈װ׵זַ׏׋ֿה
    ˖ 橆㞮㢌侧ךֿה
    ˖ ؝وٝسٓ؎ٝ䒷侧ךֿה
    ˖ فٗإأ꟦ך鸐⥋חאְגך鑫稢
    ˖ ءؚشٕךֿה
    ˖ ر٦ٌٝفٗإأךֿה
    ˖ 植㹋⚅歲דך⢽

    View Slide

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

    View Slide

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

    View Slide

  58. Comming Soon…

    View Slide

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

    View Slide