Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.

Similar presentations


Presentation on theme: "Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads."— Presentation transcript:

1 Chapter 4: Threads

2 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads

3 3 Recall Process Control Block together with stack, define execution flow or thread of execution

4 4 Single-Threaded Process Process until now (‘traditional’ proc.) includes a single thread of execution Extend single-threaded process to multithreaded process Fig from Feitelson

5 5 Thread Application A word processor (e.g. MS Words) with three threads mouse backup formatting

6 6 Benefits of Multithreading Responsiveness Multithreading an interactive application may increase responsiveness  Multithreaded web browser could still allow user interaction in one thread while an image was being loaded in another thread Resource sharing Threads share the code and data of the process Economy Creating / managing thread consumes less time than creating / managing process Utilization of multiprocessor architectures On a multiprocessor, threads can run in parallel

7 7 Implementing Threads User-level threads OS does not know about them managed by a user thread library  thread_create: create thread  thread_exit  thread_wait  thread_yield: yield execution to another thread that is ready to run Kernel-level threads managed by OS

8 8 User Threads Thread management done by user-level thread library The operating system does not know about user threads If a thread of a process makes a blocking system call, then all other threads of the process cannot progress

9 9 User Threads Thread Control Block (Thread Descriptor) represents thread in thread library From Feitelson

10 10 User Threads Three primary thread libraries: POSIX Pthreads Win32 threads Linux threads

11 11 Using Pthreads Library -- Example #include 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; } Static Data Heap thread1 stack thread2 stack Text PC 1 PC 2 SP 1 SP 2 How many threads does this process have?

12 12 Kernel-Level Threads Thread Control Block (Thread Descriptor) represents thread, is used by OS for management Fig from Feitelson

13 13 Kernel-Level Threads Supported by the kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

14 14 Classifying Multithreading Implementations Multithreading models Many-to-One One-to-One Many-to-Many  Combine the advantages of user-level threads with that of kernel- level threads

15 15 Many-to-One Model Many user-level threads “mapped” to single kernel thread User-level threads are multiplexed over a single kernel-level thread  user thread scheduler Examples: Solaris Green Threads GNU Portable Threads

16 16 Many-to-One Model user threads kernel thread, or single threaded PCB user thread scheduler, part of thread library

17 17 One-to-One Model Each user-level thread “maps” to single kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

18 18 One-to-one Model kernel thread user thread

19 19 Many-to-Many Model Allows many user-level threads to be “mapped” to many kernel threads User-level threads are multiplexed over many kernel threads  user thread scheduler Allows the operating system to create a sufficient number of kernel threads

20 20 Many-to-Many Model user thread scheduler kernel thread user thread

21 21 user-level thread scheduler kernel’s thread scheduler Fig from SUN

22 22 Pthreads A POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization Specifies behavior of thread Implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)

23 23 Windows XP Threads Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)

24 24 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process)

25 End of Chapter 4


Download ppt "Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads."

Similar presentations


Ads by Google