1 / 24

5. Thread

5. Thread. Khaùi nieäm toång quan Caùc moâ hình multithread Pthreads (POSIX thread) Multithreading trong Solaris 2 Multithreading vôùi Java. Xem xeùt laïi khaùi nieäm quaù trình. Khaùi nieäm quaù trình truyeàn thoáng: quaù trình goàm Khoâng gian ñòa chæ (text section, data section)

leif
Download Presentation

5. Thread

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 5. Thread • Khaùi nieäm toång quan • Caùc moâ hình multithread • Pthreads (POSIX thread) • Multithreading trong Solaris 2 • Multithreading vôùi Java

  2. Xem xeùt laïi khaùi nieäm quaù trình • Khaùi nieäm quaù trình truyeàn thoáng: quaù trình goàm • Khoâng gian ñòa chæ (text section, data section) • Moät luoàng thöïc thi duy nhaát (single thread of execution) • program counter • caùc register • stack • Caùc taøi nguyeân khaùc (caùc open file, caùc quaù trình con,…)

  3. Môû roäng khaùi nieäm quaù trình • Môû roäng khaùi nieäm quaù trình truyeàn thoáng baèng caùch hieän thöïc nhieàu luoàng thöïc thi trong cuøng moät moâi tröôøng cuûa quaù trình. • Quaù trình goàm • Khoâng gian ñòa chæ (text section, data section) • Moät hay nhieàuluoàng thöïc thi (thread of execution), moãi luoàng thöïc thi (thread) coù rieâng • program counter • caùc register • stack • Caùc taøi nguyeân khaùc (caùc open file, caùc quaù trình con,…)

  4. Quaù trình multithreaded • Caùc thread trong cuøng moät process chia seû code section, data section vaø taøi nguyeân khaùc (caùc file ñang môû,...) cuûa process. • Quaù trình ña luoàng (multithreaded process) laø quaù trình coù nhieàu luoàng.

  5. Söû duïng thread A word processor with three threads mouse

  6. Single vaø multithreaded process PC PC PC PC

  7. Multiplexing CPU giöõa caùc thread time CPU quaù trình single-threaded

  8. Multiplexing CPU giöõa caùc thread (tt) time CPU quaù trình multithreaded

  9. #include <stdio.h> void* thread1(){ int i; for (i = 0; i < 10; i++){ printf(“Thread 1\n”); sleep(1); } } void* thread2(){ int i; for (i = 0; i < 10; i++){ printf(“Thread 2\n”); sleep(1); } int main(){ pthread_t th1, th2; pthread_create(&th1, NULL, thread1, NULL); pthread_create(&th2, NULL, thread2, NULL); sleep(20); return 0; } Ví duï Pthread program (Linux) Static Data Heap thread1 SP1 stack thread2 SP2 stack Text PC1 PC2 Sô ñoà boä nhôù

  10. Öu ñieåm cuûa thread • Tính ñaùp öùng (responsiveness) cao cho caùc öùng duïng töông taùc multithreaded • Chia seû taøi nguyeân (resource sharing): vd memory • Tieát kieäm chi phí heä thoáng (economy) • Chi phí taïo/quaûn lyù thread nhoû hôn so vôùi quaù trình • Chi phí chuyeån ngöõ caûnh giöõa caùc thread nhoû hôn so vôùi quaù trình • Taän duïng kieán truùc ña xöû lyù (multiprocessor) • Moãi thread chaïy treân moät processor rieâng, do ñoù taêng möùc ñoä song song cuûa chöông trình.

  11. User thread • Moät thö vieän thread (thread library, run-time system) ñöôïc hieän thöïc trong user space ñeå hoå trôï caùc taùc vuï leân thread • Thö vieän thread cung caáp caùc haøm khôûi taïo, ñònh thôøi vaø quaûn lyù thread nhö • thread_create • thread_exit • thread_wait • thread_yield • Thö vieän thread duøng Thread Control Block (TCB) ñeå löu traïng thaùi cuûa user thread (program counter, caùc register, stack)

  12. User thread (tt) • Kernel khoâng bieát söï coù maët cuûa user thread • Ví duï thö vieän user-thread • POSIX Pthreads

  13. u u u u u u u u u User thread (tt) • Ví duï: heä ñieàu haønh truyeàn thoáng chæ cung caáp moät “kernel thread” duy nhaát (bieåu dieãn bôûi moät PCB) cho moãi process. • Blocking problem: Khi moät thread trôû neân blocked thì kernel thread cuõng trôû neân blocked, do ñoù moïi thread khaùc cuûa process cuõng seõ trôû neân blocked. user thread thread library thread library thread library PCB PCB PCB kernel

  14. Kernel thread • Cô cheá multithreading ñöôïc heä ñieàu haønh tröïc tieáp hoã trôï • Kernel quaûn lyù caû process vaø caùc thread • Vieäc ñònh thôøi CPU ñöôïc kernel thöïc hieän treân thread

  15. Kernel thread (tt) • Cô cheá multithreading ñöôïc hoã trôï bôûi kernel • Khôûi taïo vaø quaûn lyù caùc thread chaäm hôn • Taän duïng ñöôïc lôïi theá cuûa kieán truùc multiprocessor • Thread bò blocked khoâng keùo theo caùc thread khaùc bò blocked. • Moät soá heä thoáng multithreading (multitasking) • Windows 9x/NT/200x • Solaris • Linux

  16. Hieän thöïc thread • Thread coù theå hieän thöïc theo moät trong caùc moâ hình sau • Moâ hình many-to-one • Moâ hình one-to-one • Moâ hình many-to-many

  17. Moâ hình many-to-one • Nhieàu user-level thread “chia seû” moät kernel thread ñeå thöïc thi • Vieäc quaûn lyù thread ñöôïc thöïc hieän thoâng qua caùc haøm cuûa moät thread library ñöôïc goïi ôû user level. • Blocking problem: Khi moät thread trôû neân blocked thì kernel thread cuõng trôû neân blocked, do ñoù moïi thread khaùc cuûa process cuõng seõ trôû neân blocked. • Coù theå ñöôïc hieän thöïc ñoái vôùi haàu heát caùc heä ñieàu haønh. K kernel thread

  18. Moâ hình one-to-one • Moãi user-level thread thöïc thi thoâng qua moät kernel thread rieâng cuûa noù • Moãi khi moät user thread ñöôïc taïo ra thì cuõng caàn taïo moät kernel thread töông öùng • Heä ñieàu haønh phaûi coù cô cheá cung caáp ñöôïc nhieàu kernel thread cho moät quaù trình • Ví duï: Windows NT/2000 K K K K kernel thread

  19. Moâ hình many-to-many • Nhieàu user-level thread ñöôïc phaân chia thöïc thi (multiplexed) treân moät soá kernel thread. • Traùnh ñöôïc moät soá khuyeát ñieåm cuûa hai moâ hình many-to-one vaø one-to-one • Ví duï • Solaris 2 • Windows NT/2000 vôùi package ThreadFiber K K K kernel thread

  20. Pthreads • Chuaån POSIX (IEEE 1003.1c) cung caáp caùc API hoã trôï taïo thread vaø ñoàng boä thread (synchronization) • Phoå bieán trong caùc heä thoáng UNIX/Linux • Laø moät thö vieän hoã trôï user-level thread • Tham khaûo theâm ví duï veà laäp trình thö vieän Pthread vôùi ngoân ngöõ C trong heä thoáng Unix-like, trang 140, “Operating System Concepts”, Silberschatz et al, 6th Ed, 2003. • Bieân dòch vaø thöïc thi chöông trình multithreaded C trong Linux $ gcc source_file.c -lpthread –o output_file $ ./output_file

  21. Thread trong Solaris • User-level threads • Pthread vaø UI-thread • Lightweight process (LWP) • Moãi process chöùa ít nhaát moät LWP • Thö vieän thread coù nhieäm vuï phaân ñònh user thread vaøo caùc LWP • User-level thread ñöôïc gaén vôùi LWP thì môùi ñöôïc thöïc thi. • Thö vieän thread chòu traùch nhieäm ñieàu chænh soá löôïng LWP • Kernel-level thread • Moãi LWP töông öùng vôùi moät kernel-level thread • Ngoaøi ra, heä thoáng coøn coù moät soá kernel threads daønh cho moät soá coâng vieäc ôû kernel (caùc thread naøy khoâng coù LWP töông öùng) • Ñoái töôïng ñöôïc ñònh thôøi trong heä thoáng laø caùc kernel thread

  22. Thread trong Solaris (tt) many-to-many

  23. Thread trong Solaris (tt) … LWP2 LWP1 LWP3 Quá trình trong Solaris

  24. Thread trong Java • Hoã trôï taïo vaø quaûn lyù thread ôû möùc ngoân ngöõ laäp trình (language-level) • Taát caû chöông trình Java chöùa ít nhaát laø moät thread • Caùc thread cuûa Java ñöôïc quaûn lyù bôûi JVM • Hai phöông phaùp taïo Java Threads • extend Thread class vaø override method run() • Hieän thöïc (implementing) Runnable interface

More Related