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

June 13, 2015
Tweet

More Decks by Koji SHIMADA

Other Decks in Programming

Transcript

  1.  فٗإأהכ ˖ 㹋遤⚥ךفؚٗٓيך㹋⡤ ˖ ׅץגך؝٦سכفٗإأ♳ד㹋遤ׁ׸׷ % 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 ... ֿך遤遤ָ植㖈㹋遤⚥ךفٗإأ 痥畍չ劤剅ך䩛䒷ֹպ
  2.  فٗإأחכ*%ָ֮׷ ˖ ぐفٗإأכ♧䠐ז*% فٗإأ*% ׾䭯א ˖ 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*%⴨ד׉׸׊׸ךفٗإأ*%׾然钠דֹ׷ 痥畍չفٗإأחכ*%ָ֮׷պ
  3. > 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>>
  4. 3VCZדכMFOPדؿ؋؎ٕر؍أؙٔفة׾然钠דֹ׷ >> STDIN.fileno => 0 >> STDOUT.fileno => 1 >>

    STDERR.fileno => 2 >> file = File.open(“a.txt”, “w”) => #<File:a.txt> >> file.fileno => 9 㢩鿇ؿ؋؎ٕפךⰅ⳿⸂׮ずׄ״ֲח盖椚ׁ׸גְ׷ֿהָ׻ַ׷ 3VCZ׾⢪׏ג⡤꿀
  5.  فٗإأחכ鋵ְָ׷ ˖ ׅץגךفٗإأחכ鋵הז׷فٗإأָ㶷㖈ׅ׷ ˖ 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فٗإأד֮׷ֿהָ׻ַ׷ 痥畍չفٗإأחכ鋵ְָ׷պ
  6.  荈ⴓָ鋵חז׷ 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) ⹛⡲然钠 痥畍չفٗإأכ㶨فٗإأ׾⡲׸׷պ
  7.  荈ⴓָ鋵חז׷ 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ָㄎן⳿ׁ׸׷ה 荈ⴓ׾؝ؾ٦׃ג 倜׃ְفٗإأָ⡲׵׸׷
  8.  荈ⴓָ鋵חז׷ 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فؚٗٓي׾ 㹋遤⚥ךفٗإأ ך醱醡 欰䧭ׁ׸׋فٗإأכ ـٗحؙך⚥׌ֽ׾㹋遤ׅ׷ ⯋ךفٗإأכ׉ך䖓׾ 㹋遤׃גְֻ
  9.  㶨⣘חⴽך麣׾遤ַׇ׷ 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ίϚϯυ͕࣮ߦ͞Εͨ͸ͣ ⹛⡲然钠 痥畍չ畭劣فٗإأ׾⡲׷պ
  10.  ػ؎ف ؝وٝسך穠卓׾ⴽך؝وٝسךⰅ⸂חׅ׷ $ ps -x | wc -l QT؝وٝسך穠卓ך遤侧׾XD؝وٝسדؕؐٝزׅ׷

    ֿ׸ָ6/*9ךㆸ㷕ד֮׷կ♧אךֿה׾遤 ְծת׋׉׸׾ֲתֻװ׷فؚٗٓي׾剅 ֽկ⼿锃׃ג⹛ֻفؚٗٓي׾剅ֽկ .%وؗٗ؎ l
  11. *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> ػ؎ف׾⡲䧭ׅ׷ ػ؎فח剅ֹ鴥׬ ػ؎ف׾铣׫⳿ׅ
  12. *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” ـٗحؙ晛
  13. 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
  14. ➙傈װ׏׋ֿה ˖ فؚٗٓيכفٗإأה׃ג㹋遤ׁ׸׷ ˖ فٗإأכ*%׾䭯א ˖ فٗإأכ垥彊أزٔ٦يח籬ָ׏גְ׷ ˖ ٔا٦أכؿ؋؎ٕر؍أؙٔفة׾䭯א ˖

    鋵فٗإأה㶨فٗإأחאְג ˖ GPSL FYFDדⴽךفؚٗٓي׾㹋遤ׅ׷ ˖ فٗإأず㡦ךⰅ⳿⸂׾ػ؎فד籬־ג׫׷