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. ໨࣍ l ϓϩηεͱ͸ʁ l ϓϩηεͱ͸ʁ l ϓϩηεʹ͸*%͕͋Δ l ϓϩηεʹ͸਌͕͍Δ l

    ϓϩηεͷϦιʔεΛݟͯΈΑ͏ʂ l ϓϩηεΛ࡞Δ l ϓϩηε͸ࢠϓϩηεΛ࡞ΕΔ l GPSLͷڍಈΛ௥ͬͯΈΑ͏ʂ l ϓϩηεؒ௨৴ l ϓϩηεؒ௨৴ͱ͸ʁ l ύΠϓʹΑΔ௨৴ l ·ͱΊ
  2. ϓϩηεʹ͸*%͕͋Δ $ 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*%͸୯ʹ࿈൪ʹͳ͍ͬͯΔ਺஋ͷϥϕϧͰ͋Δɻ
  3. ϓϩηεʹ͸਌͕͍Δ 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ͷࢠ
  4. ϓϩηεʹ͸਌͕͍Δ ਌ϓϩηεͷ*%Λऔಘ͢Δ ͍͍ͨͯͷ৔߹ɺ਌ϓϩηε͸ͦͷϓϩηεΛىಈͨ͠ ϓϩηεͱͳΔɻ $ 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ϓϩηε ʢγεςϜىಈ࣌ʹ࠷ॳʹ্ཱ͕ͪΔϓϩηεʣ ਌ΛͨͲ͍ͬͯ͘ͱɾɾ
  5. ϓϩηε͸ࢠϓϩηεΛ࡞ΕΔ l GPSL  γεςϜίʔϧʹΑͬͯɺ࣮ߦதͷϓϩηε͔Β ৽͍͠ϓϩηεΛੜ੒Ͱ͖Δɻ l GPSL  ΛݺͿଆͷϓϩηε͸ʮ਌ϓϩηεʯɺੜ੒͞Ε

    Δϓϩηε͸ʮࢠϓϩηεʯͱͳΔɻ l ࢠϓϩηε͸ɺ਌ϓϩηεͰ࢖ΘΕ͍ͯΔ͢΂ͯͷϝϞ Ϧͷίϐʔͱɺ਌ϓϩηε͕։͍͍ͯΔϑΝΠϧσΟε ΫϦϓλΛҾ͖ܧ͙ɻ
  6. 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
  7. 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
  8. 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
  9. ύΠϓʹΑΔ௨৴ ύΠϓ͸਌ࢠؔ܎ʹ͋Δϓϩηεؒͷ୯ํ޲ͷ௨৴ Ϣʔβۭؒ Χʔωϧۭؒ ϑΝΠϧσΟεΫϦϓλ ϑΝΠϧσΟεΫϦϓλ    

          ਌ϓϩηε ࢠϓϩηε σΟεϓϨΠ ΩʔϘʔυ ύΠϓ ୺຤ ࢀߟɿIUUQXXXDPJOTUTVLVCBBDKQdZBTDPJOTTZTQSP GPSL 
  10. ύΠϓʹΑΔ௨৴ 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! ࣮ߦ ਌ϓϩηε ࢠϓϩηε