POSIX Threads

POSIX Threads

社内LT大会 0x64物語 #01 Parallel / Concurrent 資料

3f1b3c7b051241022d973fff2e713c1b?s=128

HORINOUCHI Masato

October 06, 2015
Tweet

Transcript

  1. POSIX Threads 2015/10/06 0x64 Tales #01 Parallel / Concurrent Livesense

    Inc. HORINOUCHI Masato
  2. pthread ͬͯͳʹ? (1) • ਖ਼໊ࣜশ͸ POSIX Threads Ͱɺͦͷ໊ͷ௨Γ POSIX ඪ४ͷඇಉ

    ظॲཧ(thread)ͷ API ܈ɻ • ࢓༷͸ IEEE Std 1003.1c-1995 Ͱఆ͍ٛͯ͠Δɻ • API ܈ͳͷͰ࢖༻͢Δݴޠ͸໰Θͳ͍ɻ • جຊతʹ Cݴޠ޲͚ͷϥΠϒϥϦʔΛ֤ݴޠʹҠ২ɻ
  3. pthread ͬͯͳʹ? (2) • ͋͘·Ͱ API Λنఆ͍ͯ͠Δ͚ͩͳͷͰɺͲͷΑ͏ͳ࣮૷͔͸ OS (ਖ਼֬ʹ͸ Kernel

    ͱ Library) ʹΑΔɻ • POSIX४ڌͷγεςϜͰ͋Ε͹࢖༻Ͱ͖Δͷ͸΋ͪΖΜɺ Win32 ࣮૷΋͋ͬͨΓ͢Δɻ • ͨͩ͠ 3rd party ੡(ݱঢ় RedHat)ɻ
  4. Linux ͷεϨου • ͱ͜ΖͰ "Linux kernel ͷεϨουͬͯ݁ہ͸ϓϩηε" ͬͯ࿩ Λฉ͍ͨ͜ͱͳ͍Ͱ͔͢Ͷɻ •

    ࠓճ͸͜ͷ఺Λ؆୯ʹઆ໌͢Δ͜ͱʹ͠·͢ɻ
  5. pthread σϞ • ৯ࣄ͢Δ఩ֶऀͷ໰୊ Λ໰͍ͯΈΑ͏ɻ • source ͸ Sun Studio

    12 ͷϖʔδ ͔Β͖࣋ͬͯ·ͨ͠ɻ • din_philo.c ͸σουϩοΫͷՄೳੑ͋Γɻ • din_philo_fix1.c ͸τʔΫϯ (ηϚϑΥ) Λ༻͍ͯσου ϩοΫΛղܾ͍ͯ͠Δɻ
  6. ps -Lf UID PID PPID LWP C NLWP STIME TTY

    TIME CMD horinou+ 21135 1804 21135 0 6 16:51 pts/1 00:00:00 ./din_philo horinou+ 21135 1804 21136 0 6 16:51 pts/1 00:00:00 ./din_philo horinou+ 21135 1804 21137 0 6 16:51 pts/1 00:00:00 ./din_philo horinou+ 21135 1804 21138 0 6 16:51 pts/1 00:00:00 ./din_philo horinou+ 21135 1804 21139 0 6 16:51 pts/1 00:00:00 ./din_philo horinou+ 21135 1804 21140 0 6 16:51 pts/1 00:00:00 ./din_philo • PID (process ID) ͕ಉҰͰ LWP (thread ID) ͕ҧ͏εϨουΛ 5 ͭੜ੒Ͱ͖ͨɻ
  7. Linux ͷ pthreads ࣮૷ • Ϟμϯͳ Linux + glibc Ͱ͸

    Na/ve POSIX Threads Library (NPTL) ͕༻͍ΒΕΔɻ • ॳظͷ Linux 2.6 Ͱ͸ LinuxThreads ͕༻͍ΒΕ͍ͯͨɻ • ΑΓલ͸ϢʔβʔϥϯυͰ࣮૷͍ͯͨ͠ (໊শෆ໌)ɻ • Lightweight Process (LWP) Λ࢖༻࣮ͯ͠૷͍ͯ͠Δɻ • LWP ͸ෳ਺ͷ LWPಉ࢜ͰϦιʔεΛڞ༗͢Δ͜ͱ͕Մೳɻ
  8. NPTL • LWP ͱεϨου͕ 1:1 ͷ࣮૷ɺ͍ΘΏΔ 1:1 ϞσϧΛ࠾༻ɻ • ͦΕͧΕͷεϨου͕

    Kernel εέδϡʔϧ࣮ମʹ 1:1 Ϛοϐ ϯάͱͳΔɻ • ϓϩηε΋εϨου (LWP) ΋ task_struct ߏ଄ମͰ؅ཧ͠ɺ ϓϩηεεέδϡʔϦϯά͸ಉҰͷΩϡʔͰߦͳ͏ɻ • ཁ͢ΔʹϓϩηεͱεϨουͷҧ͍͸΄ͱΜͱͳ͍ɻ
  9. ϓϩηεͱεϨουͷϝϞϦ؅ཧ • ϓϩηε • fork ࣌ʹ৽͘͠Ծ૝ΞυϨεۭؒΛੜ੒͠਌ͷϝϞϦΛࢠʹ ίϐʔɻ • εϨου •

    ෳ਺ͷεϨου͕ 1ͭͷԾ૝ΞυϨεۭؒΛڞ༗ɻ
  10. ϓϩηεͷԾ૝ΞυϨεۭؒ ϚϧνεϨουͷίϯςΩετ੾Γସ͑ʹ൐͏ίετ ͔ΒҾ༻ɻ

  11. εϨουͷԾ૝ΞυϨεۭؒ ϚϧνεϨουͷίϯςΩετ੾Γସ͑ʹ൐͏ίετ ͔ΒҾ༻ɻ

  12. NPTL ͷར఺ • kernel ͔Β͸εϨου΋ϓϩηε(1:1Ϟσϧ)ͱͯ͠ѻ͑Δͷ Ͱ… • 1ͭͷϓϩηεεέδϡʔϥʔ͚ͩͰ؅ཧ͢ΔͷͰγϯϓ ϧɻ •

    SMP γεςϜͰ΋ෳ਺ͷίΞͰεϨουͷฒྻॲཧΛ͢Δͷ ͕؆୯ɻ
  13. Linux Ҏ֎ͩͱ • ͪͳΈʹ Linux Ҏ֎ͷ POSIX γεςϜ͸ M:N Ϟσϧ࠾༻ྫ͕

    ଟ͍ɻ • FreeBSD • Kernel Scheduler En;;es (KSE) • Solaris • Solaris Lightweight Process (LWP)
  14. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠