Chöông 2.B Thread Khaùi nieäm toång quan Caùc moâ hình multithread

Slides:



Advertisements
Similar presentations
Moät caây lôùn, ñöôïc vun troàng treân thöûa ñaát toát ………... Töø Valdocco... Töø ngöôøi ñôøi...Töø nhöõng neàn vaên hoùa... Töø Mornese.
Advertisements

Hoài qui phi tuyeán Hoài qui phi tuyeán vaø hoài qui tuyeán tính : Moâ hình hoài qui ñöôïc goïi laø phi tuyeán neáu caùc ñaïo haøm cuûa moâ hình töông.
Trao đ ổ i tr ự c tuy ế n t ạ i:
Threads. Readings r Silberschatz et al : Chapter 4.
RESERVATROL:chaát choáng laõo hoùa baûo veä da maïnh. BS.PHAÏM THÒ KIM ANH.
Chapter III1 KYÕ THUAÄT NAÂNG - VAÄN CHUYEÅN CHÖÔNG 3 DAÂY & CAÙC CHI TIEÁT QUAÁN, HÖÔÙNG DAÂY (WIRE ROBES AND CHAINS FOR HOISTING AND HAULAGE. LIFTING.
1 BAØI TAÄP Baøi taäp chöông I Baøi taäp chöông II Baøi taäp chöông III Baøi taäp chöông IV Baøi taäp chöông V vaø VI Baøi taäp chöông VII Baøi taäp chöông.
PHAÂN TÍCH CHUOÃI THÔØI GIAN (time series analysis)
Chapter 5 Threads os5.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Process Concept An operating system executes a variety of programs
CHUYEÂN ÑEÀ ÑIEÄN HOÙA HOÏC. I.PHAÛN ÖÙNG OXI HOÙA - KHÖÛ Laø phaûn öùng coù söï thay ñoåi soá oxi hoùa cuûa caùc nguyeân toá  Coù söï trao ñoåi e 
Grunfeld, Desiderio, Lebowitz, Silverman & Klestadt LLP ASSESSING & MANAGING THE RISK OF ANTIDUMPING/CVD ACTIONS AGAINST VIETNAMESE STEEL.
1 st EÂ TEØO! XUOÁNG LAØM LAÏI CAÙI NAØY THEO HEÄ MEÙT CHÖ KHOÂNG PHAÛI HEÄ INCHES.
HOÀI SINH TIM PHOÅI TS BS Hoà Huyønh Quang Trí Vieän Tim TP HCM.
ÑIEÄN TAÂM ÑOÀ CAÊN BAÛN. BS. Ñinh Hieáu Nhaân BS. Ñinh Hieáu Nhaân.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
-1.1- Chöông 10: Heä Thoáng File 10.C. 2 Chöông 10: Heä Thoáng File  Beân trong ñóa cöùng  Caùc giaûi thuaät ñònh thôøi truy caäp ñóa  Ñònh daïng,
HOÙA HOÏC höõu cô …trong theá giôùi phaân töû Bieân soaïn: Nguyeãn Laân Bieân soaïn: Nguyeãn Laân Tuaán Minh Tuaán Minh.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
CHÖÔNG 6: CAÛM BIEÁN ÑO LÖÔØNG 6.1 Ñaëc tính toång quaùt 6.2 Caûm bieán daïng ñieän trôû 6.3 Caûm bieán daïng ñieän caûm 6.4 Caûm bieán daïng hoå caûm.
MAÏNG SOÁ LIEÄU CHÖÔNG 2: LAN (Local Area Network)
Ñöøng ñoaùn. Haõy bieát cho töôøng taän. “Ñoäc Haïi” coù nghóa gì? Chaát ñoäc haïi laø baát cöù chaát gì coù theå gaây nguy haïi cho con ngöôøi khi hít.
Trình Baøy Mang Laïi: Kieán Thöùc, An Toaøn, Töï Tin Chöông Trình Giaùo Duïc Taøi Chaùnh Cuûa FDIC Tröông Muïc Vaõng Lai.
Do söï giuùp ñôõ cuûa: Cuoäc Haønh Trình tôùi Laøm Chuû Caên Nhaø cuûa Mình Laøm ñôn giaûn… Bôûi: Phaân Ngaønh Tieáp Thò Ña Vaên Hoùa.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
1.Moãi khi coù nhaân vieân nghæ vieäc hoaëc trong doanh nghieäp phaùt sinh theâm coâng vieäc môùi thì xuaát hieän nhu caàu nhaân söï. 2.Caàn xem xeùt lieäu.
-1.1- Chöông 10: Heä Thoáng File 10.C. 2 Chöông 10: Heä Thoáng File  Beân trong ñóa cöùng  Caùc giaûi thuaät ñònh thôøi truy caäp ñóa  Ñònh daïng,
6.5 CAÛM BIEÁN ÑIEÄN DUNG ( Capacitive transducer ) 1- Caáu taïo vaø nguyeân lyù: 2 loaïi : daïng ñôn ( hai baûn cöïc ) vaø daïng vi sai ( 1baûn cöïc chung.
1 CHÖÔNG II MOÄT SOÁ KHAÙI NIEÄM CÔ BAÛN VEÀ CAO TAÀN.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Khoa Khoa Hoïc & Kyõ Thuaät Maùy Tính – Ñaïi Hoïc Baùch Khoa TP HCM 10.C.1 Chöông 10: Heä Thoáng File 10.C  Caáu truùc heä thoáng löu tröõ thöù caáp.
MOÂN HOÏC: CÔ SÔÛ KYÕ THUAÄT TRUYEÀN SOÁ LIEÄU Lôùp Kyõ thuaät vieãn thoâng Chöông 1: MAÏNG TRUYEÀN SOÁ LIEÄU VAØ CAÙC CHUAÅN HEÄ THOÁNG MÔÛ.
Chapter 91 KYÕ THUAÄT NAÂNG-VAÄN CHUYEÅN CHÖÔNG 9 BAÛO ÑAÛM AN TOAØN LAØM VIEÄC MAÙY TRUÏC.
Nöõ tu Meán Thaùnh Giaù vaø Naêm Thaùnh Ñöùc Cha Pheâroâ Nguyeãn Vaên Nhôn, trình baøy vôùi Ñöùc Thaùnh Cha Beâneâñictoâ XVI: * Tình Hình GHVN *
BAØI TAÄP chöông 1 1. Veõ taát caû ñoà thò a. 3 ñænh vaø 3 caïnh. b. 4 ñænh, 4 caïnh vaø khoâng coù voøng, khoâng coù caïnh //. c. lieät keâ 4 ñoà thò.
Operating System Concepts
Chương 4 : Ño L(inductance),C(capacitance)& M (mutual inductance) 4.1- Đo L &C dùng vôn kế và ampe kế Đo L&C dùng cầu đo AC 4.3- Đo M dùng vôn kế.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
PHAÀN 1: MOÄT SOÁ VAÁN ÑEÀ VEÀ NOÄI QUY AN TOAØN TRONG LAÉP ÑAËT- VAÄN HAØNH VAØ BAÛO TRÌ HEÄ THOÁNG KHÍ NEÙN 1.1. Nhöõng quy ñònh veà an toaøn trong laép.
NOÄI DUNG 1. CAÁU TAÏO SÔÏI QUANG
CS 6560: Operating Systems Design
Practice Chapter Four.
Day 12 Threads.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4 Threads.
Chapter 4: Threads 羅習五.
ÑEÀ TAØI : COÂNG NGHEÄ EÙP PHUN & ÖÙNG DUÏNG PHAÀN MEÀM PRO/ENGINEER ÑEÅ THIEÁT KEÁ KHUOÂN MOÙC CAÊNG DAÂY GHEÁ.
Threads & multithreading
Operating System Concepts
May be not.... Con kieán Moät truyeän nguï ngoân... Hay laø
Chapter 5: Threads Overview Multithreading Models Threading Issues
Phong caùch laõnh ñaïo Point out that this course is competency based and their behaviour is a criteria for success. They will be asked questions, role.
Quaûn lyù tieán trình.
GVHD: GS.TS LÊ KHẮC BÌNH HV: NGUYỄN THỊ THU TRANG
HEÄ THOÁNG TAÄP TIN CUÛA UNIX
PHÁT XẠ LẠNH(KÍNH HIỂN VI STM)
2.A Quaûn lyù quaù trình Khaùi nieäm cô baûn Ñònh thôøi CPU
Moân: Mĩ thuật GIAÙO AÙN ÑIEÄN TÖÛ Hä tªn:NguyÔn ThÞ Sen
Lieân Laïc VAÊN PHOØNG TRUNG ÖÔNG TÑT. Nguyeãn Hoàng Lónh
Chöông 9: I/O System Thieát bò phaàn cöùng I/O
Tiết 50 - Bài 33 ÑIEÀU CHEÁ KHÍ HIÑRO PHAÛN ÖÙNG THEÁ.
TÍNH TOAÙN NGAÉN MAÏCH TRÌNH BAØY TS. TRÖÔNG VIEÄT ANH.
Presentation transcript:

Chöông 2.B Thread Khaùi nieäm toång quan Caùc moâ hình multithread Pthread (POSIX thread) Multithreading trong Solaris

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

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 1. Khoâng gian ñòa chæ 2’. Moät hay nhieàu luoàng thöïc thi, moãi luoàng thöïc thi (thread) coù rieâng program counter caùc register stack 3. Caùc taøi nguyeân khaùc (caùc open file, caùc quaù trình con,…)

Quaù trình ña luoàng (Multi-threaded process) Khi quaù trình khôûi ñaàu chæ coù main (hay initial) thread thöïc thi Main thread seõ taïo caùc thread khaùc. Caùc thread trong cuøng moät process chia seû code, data 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.

Trình soaïn thaûo vaên baûn vôùi ba thread Söû duïng thread formatting backup mouse Trình soaïn thaûo vaên baûn vôùi ba thread

Process & thread information Per process items Address space Open files Child processes Signals & handlers Accounting info Global variables Per thread items Program counter Registers Stack & stack pointer State Per thread items Program counter Registers Stack & stack pointer State Per thread items Program counter Registers Stack & stack pointer State Quaù trình coù ba thread

Chia seû CPU giöõa caùc thread (1/2) time CPU ba quaù trình single-threaded

Chia seû CPU giöõa caùc thread (2/2) time CPU hai quaù trình multithreaded

Ví duï chöông trình söû duïng Pthread #include <stdio.h> void* thread1(){ int i; for (i = 0; i < 10; i++){ printf(“Thread 1\n”); sleep(1); } void* thread2(){ 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; Stack Heap SP1 thread1 stack SP2 thread2 stack Static data Text PC1 PC2 Sô ñoà boä nhôù Chöông trình naøy khi chaïy coù bao nhieâu thread?

Öu ñieåm cuûa thread Tính ñaùp öùng cao cho caùc öùng duïng töông taùc Chia seû taøi nguyeân giöõa caùc thread: vd memory Tieát kieäm chi phí heä thoáng 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 ñöôï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.

User thread (1/4) 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 thoâng tin veà user thread (program counter, caùc register, stack) 4.4 What resources are used when a thread is created? How do they differ from those used when a process is created? Answer: Because a thread is smaller than a process, thread creation typically uses fewer resources than process creation. Creating a process requires allocating a process control block (PCB), a rather large data structure. The PCB includes a memory map, list of open files, and environment variables. Allocating and managing the memory map is typically The most time-consuming activity. Creating either a user or kernel thread involves allocating a small data structure to hold a register set, stack, and priority. Is the thread ID unique to the system, or only unique to the process? unique to the process After pthread_create(), which thread is running? creator? new thread? another existing thread? do not assume anything about this! same applies to process fork()

User thread (2/4) Caáu truùc döõ lieäu vaø memory layout ñeå hieän thöïc user thread TCBs D. Feitelson

User thread (3/4) Kernel khoâng bieát söï coù maët cuûa user thread Kernel chæ bieát PCB cuûa quaù trình Ví duï thö vieän user thread POSIX Pthread

User thread (4/4) Vaán ñeà: heä ñieàu haønh chæ caáp phaùt duy nhaát moät PCB cho moãi process ( main/initial thread) Blocking problem: Khi moät thread trôû neân blocked thì moïi thread khaùc cuûa process seõ khoâng tieán trieån ñöôïc user thread thread library thread library thread library PCB PCB PCB kernel

Kernel thread (1/3) Khi kyõ thuaät multithreading ñöôïc heä ñieàu haønh tröïc tieáp hoã trôï Kernel quaûn lyù caû process vaø caùc thread – kernel thread Vieäc ñònh thôøi CPU ñöôïc kernel thöïc hieän treân thread

Kernel thread (2/3) Khi multithreading ñöôïc hoã trôï bôûi kernel Khôûi taïo vaø quaûn lyù caùc thread chaäm hôn so vôùi user thread do system call overhead chuyeån user mode  kernel mode Taän duïng ñöôïc lôïi theá cuûa kieán truùc multiprocessor Duø moät thread bò blocked, caùc thread khaùc cuûa quaù trình vaãn coù theå tieán trieån Moät soá heä thoáng multithreading Windows 9x/NT/200x Solaris Linux 4.2 What are two differences between user-level threads and kernel-level threads? Under what circumstances is one type better than the other? Answer: (1) User-level threads are unknown by the kernel,whereas the kernel is aware of kernel threads. (2) On systems using either M:1 or M:N mapping, user threads are scheduled by the thread library and the kernel schedules kernel threads. (3) Kernel threads need not be associated with a processwhereas every user thread belongs to a process. Kernel threads are generally more expensive tomaintain than user threads as they must be represented with a kernel data structure.

Kernel thread (3/3) Caáu truùc döõ lieäu vaø memory layout ñeå hieän thöïc kernel thread TCBs D. Feitelson

Hieän thöïc user thread Nhaéc laïi kernel thread – thread ñöôïc heä ñieàu haønh quaûn lyù User(-level) multithreading 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

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ì moïi thread khaùc cuûa process khoâng tieán trieån ñöôïc. Coù theå ñöôïc hieän thöïc ñoái vôùi haàu heát caùc heä ñieàu haønh. kernel thread

Moâ hình one-to-one Moãi user 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 cung caáp ñöôïc nhieàu kernel thread cho moät quaù trình Ví duï: Windows NT/2000 kernel thread

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. Keát hôïp öu ñieåm cuûa user-level thread vaø cuûa kernel-level thread Ví duï Solaris 2 Windows NT/2000 vôùi package ThreadFiber kernel thread

Pthread Chuaån POSIX (IEEE 1003.1c) ñaëc taû API cho caùc thuû tuïc taïo thread vaø ñoàng boä thread 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

Thread trong Solaris (1/3) 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 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 thread Ngoaøi ra, heä thoáng coøn coù moät soá kernel thread 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 4.5 Assume an operating system maps user-level threads to the kernel using the many-to-many model and the mapping is done through LWPs. Furthermore, the system allows developers to create real-time threads. Is it necessary to bind a real-time thread to an LWP? Explain. Answer: Yes. Timing is crucial to real-time applications. If a thread is marked as real-time but is not bound to an LWP, the thread may have to wait to be attached to an LWP before running. Consider if a real-time thread is running (is attached to an LWP) and then proceeds to block (i.e. must perform I/O, has been preempted by a higher-priority real-time thread, is waiting for a mutual exclusion lock, etc.) While the real-time thread is blocked, the LWP itwas attached to has been assigned to another thread. When the real-time thread has been scheduled to run again, it must first wait to be attached to an LWP. By binding an LWP to a realtime thread you are ensuring the threadwill be able to runwithminimal delay once it is scheduled.

Thread trong Solaris (2/3) many-to-many

Thread trong Solaris (3/3) … LWP1 LWP2 LWP3 Quá trình trong Solaris

Read more POSIX thread programming, Blaise Barney, Lawrence Livermore National Laboratory https://computing.llnl.gov/tutorials/pthreads/