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

なるほどUNIXプロセス14_15章

 なるほどUNIXプロセス14_15章

なるほどUNIXプロセス14_15章
http://tatsu-zine.com/books/naruhounix

indigolain

May 07, 2016
Tweet

More Decks by indigolain

Other Decks in Programming

Transcript

  1. 14~15ষ
    ͳΔ΄ͲUNIXϓϩηε

    View full-size slide

  2. ςΩετ
    ୈ̍̐ষϓϩηε͸଴ͯΔ
    ▸ ࢠकΓ
    ▸ Process.waitͱͦͷҰ଒
    ▸ Process.wait2Λ࢖ͬͯ΍ΓͱΓ͢Δ
    ▸ ಛఆͷࢠϓϩηεΛ଴ͭ
    ▸ ڝ߹ঢ়ଶ
    ▸ ࣮༻ྫ
    ▸ ͜ͷষͰѻͬͨγεςϜίʔϧ

    View full-size slide

  3. ςΩετ
    ࢠकΓ
    ▸ ͜Ε·Ͱѻ͖ͬͯͨfork(2)ͷྫ͸ʮܸͪͬͺͳ͠(fire and
    forget)ʯ
    ▸ ࢠϓϩηεଆͰ͋ΔॲཧΛͤͭͭ͞ɺඇಉظͰ਌ϓϩηε
    ͰผͷॲཧΛ͢Δ৔߹ʹ͸ྑ͍
    ▸ ͜ͷ৔߹Ҏ֎͸ࢠϓϩηεΛ؅ཧͯ͘͠ΕΔ࢓૊Έ͕ඞཁ
    ▸ ͜Ε͕Process.wait

    View full-size slide

  4. ςΩετ
    ࢠकΓ
    ▸ process_wait_example.rb
    ▸ Process.wait͸ࢠϓϩηεͷͲΕ͔͕̍ͭऴྃ͢Δ·Ͱͷ
    ؒɺ਌ϓϩηεΛϒϩοΫͯ͠଴ͭɻ

    View full-size slide

  5. ςΩετ
    PROCESS.WAITͱͦͷҰ଒
    ▸ ̍ͭҎ্ͷϓϩηεΛ؂ࢹ͢Δඞཁ͕͋Δ৔߹ɺͲͷࢠϓϩ
    ηε͕ऴ͔ྃͨ͠Λ஌Δඞཁ͕͋Δ
    ▸ Process.waitͷ໭Γ஋͸ऴྃͨ͠ࢠϓϩηεͷpidΛฦ͢ͷ
    Ͱɺ͜ΕΛ࢖͏
    ▸ process_wait_pid_example.rb

    View full-size slide

  6. ςΩετ
    PROCESS.WAIT2Λ࢖ͬͯ΍ΓͱΓ͢Δ
    ▸ Process.waitʹ͸Process.wait2ͱ͍͏਌ੰ͕͋Δ
    ▸ Process.wait2͸pidͱऴྃεςʔλεΛฦΓ஋ͱͯ͠ฦ͢
    ▸ ऴྃεςʔλε͸ऴྃίʔυʹΑΔϓϩηεಉ࢜ͷ௨৴खஈͱ͠
    ͯ࢖ΘΕΔ
    ▸ Process.wait2͕ฦ͢ऴྃεςʔλε͸Process::StatusΫϥεͷΠ
    ϯελϯε
    ▸ process_wait2_example.rb

    View full-size slide

  7. ςΩετ
    ಛఆͷࢠϓϩηεΛ଴ͭ
    ▸ Process.watpidͱProcess.waitpid2
    ▸ ऴྃΛ଴ͭͷ͸೚ҙͷࢠϓϩηεͰ͸ͳ͘ɺࢦఆͨ͠ࢠϓϩηε
    ▸ ऴྃΛ଴ͭࢠϓϩηε͸pidͰࢦఆ͢Δ
    ▸ ݩ͸ಉؔ͡਺Λࢦ͍ͯ͠Δ
    ▸ Process.waitʹpidΛ౉͢
    ▸ Process.waitpidʹ-1Λ౉͢

    View full-size slide

  8. ςΩετ
    ڝ߹ঢ়ଶ
    ▸ Χʔωϧ͸ऴྃͨ͠ϓϩηεΛΩϡʔʹೖΕΔ
    ▸ ऴྃΛ଴ͭࢠϓϩηε͕1ͭ΋ແ͍ঢ়ଶͰProcess.waitΛݺ
    ͼग़͢ͱErrno::ECHILDྫ֎͕ૹग़͞ΕΔ
    ▸ no_processwait.rb

    View full-size slide

  9. ςΩετ
    ࣮༻ྫ
    ▸ ࢠϓϩηεΛ׆༻͢Δύλʔϯ
    ▸ ༻ҙͨ͠1ͭͷϓϩηε͔ΒฒߦॲཧͷͨΊʹෳ਺ͷࢠϓϩηεΛੜ
    ੒ͯ͠ɺͦͷޙ͸ࢠϓϩηεͷ໘౗ΛΈΔͱ͍͏ܗࣜ
    ▸ Ԡ౴/ऴྃ࣌ͷޙ࢝຤ͳͲ
    ▸ Unicorn͕͜ͷύλʔϯΛ࠾༻͍ͯ͠Δ
    ▸ αʔόىಈ࣌ʹϫʔΧʔϓϩηεΛ͍ͭ͘࢖͏͔Λࢦఆ͢Δ
    ▸ Unicornʹؔͯ͠͸෇࿥2ʮUnicornͷϫʔΧʔϓϩηεͷ؅ཧʯࢀর

    View full-size slide

  10. ςΩετ
    ͜ͷষͰѻͬͨγεςϜίʔϧ
    ▸ Process.waitܥ͸waitpid(2)ʹରԠ͍ͯ͠Δ

    View full-size slide

  11. ςΩετ
    ୈ̍̑ষκϯϏϓϩηε
    ▸ Ռใ͸৸ͯ଴ͯ
    ▸ κϯϏ͸ͲΜͳ෩ʹΈ͑Δͷ͔ʁ
    ▸ ࣮༻ྫ
    ▸ ͜ͷষͰѻͬͨγεςϜίʔϧ

    View full-size slide

  12. ςΩετ
    Ռใ͸৸ͯ଴ͯ
    ▸ Χʔωϧ͸਌ϓϩηε͕Process.waitΛ࢖ͬͯͦͷ৘ใΛཁٻ͢Δ·Ͱ
    ऴྃͨ͠ࢠϓϩηεͷ৘ใΛͣͬͱ࣋ͪଓ͚Δ
    ▸ ΧʔωϧͷϦιʔεͷແବ࢖͍ʹͭͳ͕ͬͯ͠·͏
    ▸ Process.waitΛ࢖ͬͯࢠϓϩηεͷऴྃΛ଴ͨͳ͍৔߹ɺࢠϓϩηεΛ
    σλονͤͶ͹ͳΒͳ͍
    ▸ Process.detach͸pidͰࢦఆ͞ΕͨࢠϓϩηεͷऴྃΛ଴ͭεϨουΛ
    ੜ੒͢Δ
    ▸ detach_example.rb

    View full-size slide

  13. ςΩετ
    κϯϏͱ͸ͲͷΑ͏ʹݟ͑Δͷ͔ʁ
    ▸ zombie_process_example.rb
    ▸ ps -ho pid, state -p [κϯϏʹͳͬͨϓϩηεͷpid]

    View full-size slide

  14. ςΩετ
    ࣮༻ྫ
    ▸ ਌ϓϩηεʹ଴ͨΕͣʹࢮΜͰ͠·ͬͨࢠϓϩηε͸ྫ֎ͳ͘κϯϏϓ
    ϩηεʹͳΔ
    ▸ ਌ϓϩηε͕κϯϏϓϩηεͷऴྃεςʔλεΛऔಘ͢Ε͹ɺ৘ใ͸
    ফ͑Δ
    ▸ ॲཧΛόοΫάϥ΢ϯυͰ࣮ߦ͢Δඞཁ͕͋ΔόΠΠʹ͸ɺઐ༻ͷόο
    Ϋάϥ΢ϯυΩϡʔΠϯάγεςϜ͕࢖ΘΕΔ͜ͱ͕ଟ͍
    ▸ spawnlingͱ͍͏gemΛ࢖͑͹ɺϓϩηε΍εϨουΛѻ͏૯শతͳ
    APIΛఏڙ͢Δ͜ͱʹՃ͑ͯɺʮܸͪͬ์͠ʯͰੜ੒ͨ͠ࢠϓϩηεΛ
    Ͱλονͯ͘͠ΕΔ

    View full-size slide

  15. ςΩετ
    ͜ͷষͰѻͬͨγεςϜίʔϧ
    ▸ Process.detachʹ͋ͨΔγεςϜίʔϧ͸ଘࡏ͠ͳ͍
    ▸ RubyͰ͸εϨουͱProcess.waitΛ࢖࣮ͬͯݱ͍ͯ͠Δ

    View full-size slide