190 likes | 274 Views
Chapter 4: Threads. Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads. 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
E N D
Chapter 4: Threads • Overview • Multithreading Models • Threading Issues • Pthreads • Windows XP Threads
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
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
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
User-Level vs. Kernel-Level Threads • What are they? • How they are different? • Pros & Cons
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
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
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
Kernel Threads • Supported by kernel • Kernel manages not only processes but also threads • Kernel provides system calls to create & manage threads
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
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
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
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
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)
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