Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Chapter 4: Threads

2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads

3 What is a thread? One sequence of control in a process  One process may have multiple threads  Multithreads in a process mean multiple sequences of control  Each thread has its own PC, stack & a register set  Threads belonging to a process share the address space and all the resources of the process, i.e., code, data, open files,... Entity for CPU utilization Concurrent processing  Only one thread can run at a time in a single processor system  Quickly switch between threads  Exploit multiprocessor archictecture if available

4 Single vs. Multiple Thread(s) of Control

5 Benefits: Why multithreads rather than multi-processes? Concurrent processing for much less overhead  Much less resource consumption for thread creation due to resource sharing among the threads belonging to one process No separate address space, code & data sections, or OS resources need to be allocated for each thread A thread only needs a separate PC, a stack, and storage space to copy register values

6 Benefits: Why multithreads rather than multi-processes? Context swithiching between threads is more efficient than switching btwn processes  Just save & restore the PC, stack, and register values of the two corresponding threads Synchronization btwn threads is easier & faster than that btwn processes  Threads of a process share the same address space;thus, they can directly pass data between them through variables  Processes have to communicate via IPC (Inter Process Communication) that is more complex and slower

7 User-Level vs. Kernel-Level Threads What are they? How they are different? Pros & Cons

8 User-Level Threads Threads are implemented by user-level libraries rather than by system calls No need to call OS for switching btwn threads Kernel knows nothing about user-level threads

9 Advantages of User-Level Threads Thread package can be implemented on top of an OS that may not support threads No need to modify OS Each thread is defined in user space  No need for kernel to keep the full TCB (Thread Control Block) for each & every thread No kernel interevention for thread creation, context switches, and synchronization – Fast and Efficient

10 Disadvantages of User-Level Threads Lack of coordination between threads & OS kernel  A process gets one time slice no matter how many threads it has  Each thread needs to pass control to other threads The entire process will block in the kernel if only one thread blocks, e.g., for I/O  There could be executable threads in the processes, but kernel has no idea about the user-level threads; it simply sees processes

11 Kernel Threads Supported by kernel Kernel manages not only processes but also threads Kernel provides system calls to create & manage threads

12 Advantages of Kernel Threads Kernel has full knowledge of all threads Better scheduling is possible  For example, give a large time slice to a process with many threads If a thread of a multithreaded process blocks, run another thread of the process rather than blocking the entire process Kernel routines can be multithreaded

13 Disadvantages of Kernel Threads Slow & inefficient  Switching btwn two threads in a single process involves the kernel (2 user-kernel mode switches)  Orders of magnitude slower than user-level threads Kernel has to manage both processes and threads  Store & maintain every PCB and TCB  Large overhead  Increased kernel complexity

14 Thread Package Design Example: Solaris User-level threads LWP – Virtual Processor Kernel thread for each LWP A kernel thread can be pinned to a CPU

15 Other Multithreading Issues

16 Thread Pools Create a number of threads in a pool where they await work Advantages:  Usually slightly faster to service a request with an existing thread than creating a new thread  Allows the number of threads in the application(s) to be bound to the size of the pool

17 Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

18 Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) You will use it for a programming assignment

19 End of Chapter 4


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

Similar presentations


Ads by Google