●●● ●●●● 4. Threads ●●●●● ●●●● ●●●0● ●●●0 Objectives To introduce the notion of a thread -a fundamental unit of cpu utilization that forms the basis of multi-threaded computer systems o To discuss the apis for the pthreads Win 32 and Java thread libraries o To examine issues related to multi- threaded programming
2 4. Threads ⚫ Objectives ⚫ To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multi-threaded computer systems ⚫ To discuss the APIs for the Pthreads, Win32, and Java thread libraries ⚫ To examine issues related to multithreaded programming
●●● ●●●● 4. Threads ●●●●● ●●●● ●●●0● ●●●0 ●41 Overview o 4.2 Multi-threading Models ●4.3 Thread libraries 4.4 Threading Issues 0 4.5 Operating System Examples
3 4. Threads ⚫ 4.1 Overview ⚫ 4.2 Multi-threading Models ⚫ 4.3 Thread Libraries ⚫ 4.4 Threading Issues ⚫ 4.5 Operating System Examples
●●● ●●●● 4.1 Overview ●●●●● ●●●● ●●●0● ●●●0 ● a thread is a basic unit of cpu utilization°。 It comprises ● A thread|D ● A program counter A register set e A stack o It shares with other threads belonging to the same process Code section o Data section Other Os resources, such as open files and sIgnals
4 4.1 Overview ⚫ A thread is a basic unit of CPU utilization ⚫ It comprises ⚫ A thread ID ⚫ A program counter ⚫ A register set ⚫ A stack ⚫ It shares with other threads belonging to the same process ⚫ Code section ⚫ Data section ⚫ Other OS resources, such as open files and signals
●●● ●●●● 4.1 Overview ●●●●● ●●● ●●●0● ●●0 o A traditional (or heavyweight)process has a single thread of o control a lightweight process has multiple threads of control, it can perform more than one task at a time code data data files registers stack registers‖ registers‖ registers stack ck stack ← thread single-threaded multithreaded
5 4.1 Overview ⚫ A traditional (or heavyweight) process has a single thread of control ⚫ A lightweight process has multiple threads of control, it can perform more than one task at a time
●●● ●●●● 4.1 Overview ●●●●● ●●●● ●●●0● ●●●0 ● Motivation EXamples of multi-threaded program o web browser o word processor o database server web server
6 4.1 Overview ⚫ Motivation ⚫ Examples of multi-threaded program: ⚫ web browser ⚫ word processor ⚫ database server ⚫ web server
●●● ●●●● 4.1 Overview ●●●●● ●●● ●●●0● ●●●0 ● Benefits Responsiveness o Interactive application may continue running when part of it is blocked or is performing a lengthy operation ° Resource sharing o threads share the memory and the resource of process to which they belong To allow an application to have several different threads of activity within the same address space Economy e creation 30: 1, context switching 5: 1 o Utilization of multiprocessor architectures
7 4.1 Overview ⚫ Benefits ⚫ Responsiveness ⚫ Interactive application may continue running when part of it is blocked or is performing a lengthy operation ⚫ Resource sharing ⚫ Threads share the memory and the resource of process to which they belong ⚫ To allow an application to have several different threads of activity within the same address space ⚫ Economy ⚫ creation 30:1, context switching 5:1 ⚫ Utilization of multiprocessor architectures
●●● ●●●● 4. Threads ●●●●● ●●●● ●●●0● ●●●0 ●41 Overview e 4.2 Multithreading Models ●4.3 Thread libraries 4.4 Threading Issues 0 4.5 Operating System Examples
8 4. Threads ⚫ 4.1 Overview ⚫ 4.2 Multithreading Models ⚫ 4.3 Thread Libraries ⚫ 4.4 Threading Issues ⚫ 4.5 Operating System Examples
●●● ●●●● 4.2 Multithreading Models :99 ●●●0 User threads vs, Kernel threads ● User threads o Provided by a thread library at the user level e PosiX Pthreads. Mach c-threads, solaris ul-threads Kernel Threads Provided and managed by the os directly Linux Solaris2 Windows. BeOS Relationship between kernel threads and user threads Many-to-one model One-to-one model ●Many- to-many model
9 4.2 Multithreading Models ⚫ User Threads vs. Kernel Threads ⚫ User Threads ⚫ Provided by a thread library at the user level ⚫ POSIX Pthreads, Mach C-threads, Solaris UI-threads ⚫ Kernel Threads ⚫ Provided and managed by the OS directly ⚫ Linux, Solaris2, Windows, BeOS ⚫ Relationship between kernel threads and user threads ⚫ Many-to-one model ⚫ One-to-one model ⚫ Many-to-many model
●●● ●●●● 4.2 Multithreading Models :99 ●●●0 ● Many-to- one model Many user-level threads mapped to single kernel thread ser thread k刀+ kernel thread
10 4.2 Multithreading Models ⚫ Many-to-one model ⚫ Many user-level threads mapped to single kernel thread
●●● ●●●● 4.2 Multithreading Models : ●●●0 ● Threads management is done by the thread°° library in user space so it is efficient(no often to invoke system call) o The entire process will block if a thread makes blocking system call o Only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors ● Example: o Green threads for solaris2 o GNU Portable Threads
11 4.2 Multithreading Models ⚫ Threads management is done by the thread library in user space, so it is efficient (no often to invoke system call) ⚫ The entire process will block if a thread makes blocking system call ⚫ Only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors ⚫ Example: ⚫ Green threads for Solaris2. ⚫ GNU Portable Threads