Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to threads

Similar presentations


Presentation on theme: "Introduction to threads"— Presentation transcript:

1 Introduction to threads
CSSE 332 Operating Systems Rose-Hulman Institute of Technology 1

2 Thread overview A basic unit of CPU utilization Comprises A thread ID
A program counter A register set A stack Shares with other threads of the same process Code section Data section Other OS resources (open files, signals, etc) A traditional process has a single thread of control. If a process has more than one thread of control, it can perform multiple tasks at a time. 2

3 Single and multithreaded processes
3

4 Motivation for threads
Many modern software packages are multithreaded A single app may be required to perform multiple similar tasks Most modern OS kernels are now multithreaded Web-browser may have one thread to display images or text, while another thread retrieves data from the network. A web server may have several requests from clients requesting web pages, texts, images, sound files, etc. Kernel has threads for managing devices, handling interrupts, managing free space (LINUX does this last thing) 4

5 Benefits of multithreading
Responsiveness (to user) Resource sharing Economy Scalability Have students think-pair-share on this. Interactive application  program can continue running even if parts of it are blocked. No need of message passing or shared memory techniques to share code and data and files, etc. It is more economical to create and context switch threads than processes (processes are heavyweight entities) Multithreading on a multi-CPU machine increases parallelism 5

6 Multicore programming
Multicore systems put pressure on programmers Challenges include Dividing activities Balance Data splitting Data dependency Testing and debugging Do think-pair-share on why these are challenges Examining app to find areas that can be divided into separate, concurrent tasks that can run on individual cores Ensuring that tasks do equal work of equal value Data accessed and manipulated by tasks must be divided to run on separate cores Does one task depend on data from another task? The tasks must be synchronized in this case Different execution paths --- testing and debugging inherently more difficult. 6

7 Multithreaded server architecture
The main thread waits for requests from clients, creates thread to service request, and resumes waiting for requests.

8 Concurrent execution on single-core system

9 Parallel execution on multicore system

10 User Threads Thread management done by user-level threads library
Three primary thread libraries: POSIX Pthreads Win32 threads Java threads A thread library provides the programmer with an API for creating and managing threads POSIX library may be provided as either user or kernel level library Win32 is kernel level library available on Windows systems Java allows threaded management to be done directly in Java program but the thread API is generally implemented using the thread library on host system 10

11 Kernel Threads Supported by the Kernel Examples Windows XP/2000
Solaris Linux Tru64 UNIX Mac OS X Threading model supported by the kernel. Needed for true parallelism on multiple CPUs. 11

12 Multithreading Models
Many-to-One One-to-One Many-to-Many Multithreading models answer the question of how do we map user threads to kernel threads in order to experience parallelism 12

13 Many-to-One Many user-level threads mapped to single kernel thread
Examples: Solaris Green Threads GNU Portable Threads Thread management is done by thread library in user space. Efficient thread management but no parallelism. If thread blocks, process blocks. 13

14 Many-to-One Model Thread management is done by thread library in user space. 14

15 One-to-One Each user-level thread maps to kernel thread Examples
Windows NT/XP/2000 Linux Solaris 9 and later Each user thread is mapped to a kernel thread  true parallelism LINUX threads are not the same as POSIX threads 15

16 One-to-one Model Only drawback: creating a user thread requires the overhead of creating a kernel thread 16

17 Many-to-Many Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package 17

18 Many-to-Many Model 18

19 Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier 19

20 Two-level Model 20


Download ppt "Introduction to threads"

Similar presentations


Ads by Google