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

Pythonで学ぶUnixプロセスの基礎

tsurubee
December 20, 2017

 Pythonで学ぶUnixプロセスの基礎

「なるほどUnixプロセス」をPythonで学ぶ

tsurubee

December 20, 2017
Tweet

More Decks by tsurubee

Other Decks in Technology

Transcript

  1. 1ZUIPOͰֶͿ
    6OJYϓϩηεͷجૅ
    1Z'VLVPLB

    View full-size slide

  2. ࣗݾ঺հ
    ͭΔͨ
    ʢ!UTVSVCFFʣ
    ΤϯδχΞྺɿ
    ೥ͪΐ͍
    େֶͷઐ߈͸Խֶ
    લ৬͸ফ๷࢜
    झຯ͸ےτϨ
    ʢಛʹϕϯνϓϨεʣ

    View full-size slide

  3. ࠓճͷ໨త
    Θ͔ͬͨΑ͏ͰΘ͔Βͳ͍ʮϓϩηεʯʹ͍ͭͯ
    1ZUIPOΛ༻͍ͯཧղΛਂΊΔ͜ͱɻ
    ͜ͷຊΛࢀߟʹ
    ͠·ͨ͠ɻ
    ௒Φεεϝʂ
    ಈ࡞؀ڥɿ6CVOUV
    ʢ7JSUVBM#PY্ʹߏஙʣ
    ࢀߟจݙ

    View full-size slide

  4. ໨࣍
    l ϓϩηεͱ͸ʁ
    l ϓϩηεͱ͸ʁ
    l ϓϩηεʹ͸*%͕͋Δ
    l ϓϩηεʹ͸਌͕͍Δ
    l ϓϩηεͷϦιʔεΛݟͯΈΑ͏ʂ
    l ϓϩηεΛ࡞Δ
    l ϓϩηε͸ࢠϓϩηεΛ࡞ΕΔ
    l GPSLͷڍಈΛ௥ͬͯΈΑ͏ʂ
    l ϓϩηεؒ௨৴
    l ϓϩηεؒ௨৴ͱ͸ʁ
    l ύΠϓʹΑΔ௨৴
    l ·ͱΊ

    View full-size slide

  5. ϓϩηεͱ͸ʁ

    View full-size slide

  6. ϓϩάϥϜͷ࣮ߦத
    ͷ࢟Ͱ͋Δɻ
    ϓϩηεͱ͸ʁ

    View full-size slide

  7. ϓϩηεʹ͸*%͕͋Δ
    $ ps u
    USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
    ubuntu 2005 0.0 0.5 21392 5392 pts/0 Ss+ 02:21 0:00 –bash
    ubuntu 2212 0.1 0.5 21388 5260 pts/1 Ss 06:18 0:00 –bash
    ubuntu 2228 0.0 0.3 36084 3348 pts/1 R+ 06:18 0:00 ps u
    1ZUIPOͰ͸ɾɾ
    $ python
    >>> import os
    >>> os.getpid()
    1229
    QTίϚϯυ
    04ͷػೳΛར༻͢ΔͨΊͷʮPTʯͱ͍͏ඪ४ϥΠϒϥϦ͕͋Δɻ
    1*%͸୯ʹ࿈൪ʹͳ͍ͬͯΔ਺஋ͷϥϕϧͰ͋Δɻ

    View full-size slide

  8. ϓϩηεʹ͸਌͕͍Δ
    QTUSFFίϚϯυ
    $ pstree
    systemd─┬─VBoxService─┬─{automount}
    │ ├─{control}
    │ ├─{cpuhotplug}
    │ ├─{memballoon}
    │ ├─{timesync}
    │ ├─{vminfo}
    │ └─{vmstats}
    ├─polkitd─┬─{gdbus}
    │ └─{gmain}
    ├─snapd───6*[{snapd}]
    ├─sshd─┬─sshd───sshd───bash
    │ └─sshd───sshd───bash───pstree
    ├─systemd───(sd-pam)
    ├─systemd-journal
    ˞Ұ෦লུ
    ϓϩηεͷ਌ࢠؔ܎ΛπϦʔදࣔͰ͖Δɻ
    QTUSFF͸CBTIͷࢠ

    View full-size slide

  9. ϓϩηεʹ͸਌͕͍Δ
    ਌ϓϩηεͷ*%Λऔಘ͢Δ
    ͍͍ͨͯͷ৔߹ɺ਌ϓϩηε͸ͦͷϓϩηεΛىಈͨ͠
    ϓϩηεͱͳΔɻ
    $ python
    >>> import os
    >>> os.getppid()
    1165
    USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
    root 1 0.9 0.5 37916 5976 ? Ss 06:43 0:08 /sbin/init
    ͢΂ͯͷى఺͸1*%͕ͷJOJUϓϩηε
    ʢγεςϜىಈ࣌ʹ࠷ॳʹ্ཱ͕ͪΔϓϩηεʣ
    ਌ΛͨͲ͍ͬͯ͘ͱɾɾ

    View full-size slide

  10. ϓϩηεͷϦιʔεΛݟͯΈΑ͏ʂ
    l TUBUVT
    l DXE
    l FOWJSPO
    l FYF
    l UBTL
    l NBQT ͳͲ
    QSPDϓϩηε*%

    View full-size slide

  11. ϓϩηεΛ࡞Δ

    View full-size slide

  12. ϓϩηε͸ࢠϓϩηεΛ࡞ΕΔ
    l GPSL
    γεςϜίʔϧʹΑͬͯɺ࣮ߦதͷϓϩηε͔Β
    ৽͍͠ϓϩηεΛੜ੒Ͱ͖Δɻ
    l GPSL
    ΛݺͿଆͷϓϩηε͸ʮ਌ϓϩηεʯɺੜ੒͞Ε
    Δϓϩηε͸ʮࢠϓϩηεʯͱͳΔɻ
    l ࢠϓϩηε͸ɺ਌ϓϩηεͰ࢖ΘΕ͍ͯΔ͢΂ͯͷϝϞ
    Ϧͷίϐʔͱɺ਌ϓϩηε͕։͍͍ͯΔϑΝΠϧσΟε
    ΫϦϓλΛҾ͖ܧ͙ɻ

    View full-size slide

  13. GPSLͷڍಈΛ௥ͬͯΈΑ͏
    import os
    if os.fork():
    print("Here in the if block.")
    else:
    print("Here in the else block.")
    $ python fork.py
    Here in the if block.
    Here in the else block.
    ࣮ߦ
    JG۟΋FMTF۟΋࣮ߦ͞Ε͍ͯΔɻͳʹ͕ى͖ͯΔʁ
    GPSL͢ΔͱԿ͕ى͖Δͷ͔ʁ
    fork.py

    View full-size slide

  14. GPSLͷڍಈΛ௥ͬͯΈΑ͏
    import os
    print("---- before fork ----")
    print("pid: {0}".format(os.getpid()))
    if os.fork():
    print("----- if block ------")
    print("pid: {0}".format(os.getpid()))
    else:
    print("---- else block -----")
    print("pid: {0}".format(os.getpid()))
    $ python fork.py
    ---- before fork ----
    pid: 1547
    ----- if block ------
    pid: 1547
    ---- else block -----
    pid: 1548
    ࣮ߦ
    JG͕۟਌ϓϩηεɺ
    FMTF͕۟ࢠϓϩηε
    ʹΑ࣮ͬͯߦ͞Ε͍ͯΔɻ
    ϓϩηε*%͕Ͳ͏ͳ͍ͬͯΔ͔ௐ΂Δ
    fork.py

    View full-size slide

  15. GPSLͷڍಈΛ௥ͬͯΈΑ͏
    import os
    ret = os.fork()
    if ret:
    print("----- if block ------")
    print("return value: {0}".format(ret))
    else:
    print("---- else block -----")
    print("return value: {0}".format(ret))
    $ python fork.py
    ----- if block ------
    return value: 1548
    ---- else block -----
    return value: 0
    ࣮ߦ
    ਌ϓϩηεଆʹ͸ʮੜ੒ͨ͠ࢠϓϩηεͷ1*%ʯΛฦ͠ɺ
    ࢠϓϩηεଆʹ͸ʮʯΛฦ͢ɻ
    GPSLͷฦΓ஋͕Ͳ͏ͳ͍ͬͯΔ͔ௐ΂Δ
    fork.py

    View full-size slide

  16. ϓϩηεؒ௨৴

    View full-size slide

  17. ϓϩηεؒ௨৴ͱ͸ʁ
    ϓϩηεؒ௨৴ʢ*1$*OUFS1SPDFTT$PNNVOJDBUJPOʣͱ͸ɺ
    ෳ਺ͷϓϩηεؒͰσʔλΛ΍ΓͱΓ͢ΔͨΊͷ࢓૊ΈͰ͋Δɻ
    *1$ͷख๏͸༷ʑ
    ɾڞ༗ϝϞϦ
    ɾγάφϧ
    ɾύΠϓ
    ɾιέοτ
    ɾηϚϑΥ
    ɾϝοηʔδύογϯά ͳͲ

    View full-size slide

  18. ϓϩηεؒ௨৴ͱ͸ʁ
    ϓϩηεؒ௨৴ʢ*1$*OUFS1SPDFTT$PNNVOJDBUJPOʣͱ͸ɺ
    ෳ਺ͷϓϩηεؒͰσʔλΛ΍ΓͱΓ͢ΔͨΊͷ࢓૊ΈͰ͋Δɻ
    *1$ͷख๏͸༷ʑ
    ɾڞ༗ϝϞϦ
    ɾγάφϧ
    ɾύΠϓ
    ɾιέοτ
    ɾηϚϑΥ
    ɾϝοηʔδύογϯά ͳͲ
    ࠓ೔͸ίϨʂ

    View full-size slide

  19. ύΠϓʹΑΔ௨৴
    ύΠϓ͸਌ࢠؔ܎ʹ͋Δϓϩηεؒͷ୯ํ޲ͷ௨৴
    Ϣʔβۭؒ
    Χʔωϧۭؒ
    ϑΝΠϧσΟεΫϦϓλ ϑΝΠϧσΟεΫϦϓλ










    ਌ϓϩηε ࢠϓϩηε
    σΟεϓϨΠ
    ΩʔϘʔυ
    ύΠϓ
    ୺຤
    ࢀߟɿIUUQXXXDPJOTUTVLVCBBDKQdZBTDPJOTTZTQSP
    GPSL

    View full-size slide

  20. ύΠϓʹΑΔ௨৴
    import os
    reader, writer = os.pipe()
    if os.fork():
    os.close(reader)
    write_pipe = os.fdopen(writer, 'w')
    write_pipe.write('Hello child!')
    write_pipe.close()
    else:
    os.close(writer)
    read_pipe = os.fdopen(reader, 'r')
    message= read_pipe.readline()
    read_pipe.close()
    print(message)
    pipe.py
    $ python pipe.py
    Hello child!
    ࣮ߦ
    ਌ϓϩηε
    ࢠϓϩηε

    View full-size slide

  21. ·ͱΊ
    ϓϩηε͸ɺ*%͕͋ͬͯɺ਌͕͍ͯɺࢠΛ࡞Εͯɺ
    ௨৴͕Ͱ͖Δɻ
    ͦͯ͠ɺͦͷ༷ࢠΛ1ZUIPO͔Β֬ೝͰ͖Δɻ
    ࠓճ࿩ͨ͜͠ͱҎ֎ʹ΋͍Ζ͍Ζಛ௃͕͋ΔͷͰɺ
    ͦΕ͸·ͨ࣍ճʂ
    ʻྫʼ
    ɾݽࣇϓϩηε
    ɾκϯϏϓϩηε
    ɾσʔϞϯϓϩηε
    ɾιέοτ௨৴
    ɾϚϧνϓϩηεॲཧ

    View full-size slide

  22. ͝ਗ਼ௌ͋Γ͕ͱ͏
    ͍͟͝·ͨ͠ʂ

    View full-size slide