Successfully reported this slideshow.



Published on

Published in: Technology, Education
  • Be the first to comment


  1. 1. Thread
  2. 2. What? A thread is called a lightweight process: ● it is comprised over a thread ID, program counter, a register set and a stack ● it shares with other threads belonging to the same process its code section, data section and other OS resources (e.g., open files) ● a process that has multiples threads can do more than one task at a time A traditional (or heavyweight) process has a single thread of control. Operating System 2011/2012
  3. 3. Why? A single application may be required to perform several similar tasks (such as Web server and Word processor). ● One solution is to have the a single process for each Web request or Word document. Each process perform on a single request/document => a separate process is created to service that request. ● Process creation is time consuming and resource intensive. ● Rather than creating another process, the server will create a new thread to service the request and resume listening for additional requests. ● Operating System 2011/2012
  4. 4. Single vs. Multithreaded Processes Operating System 2011/2012
  5. 5. Benefits Responsiveness ● One part of a program can continue running even if another part is blocked ● Resource Sharing ● Threads of the same process share the same memory space and resources ● Economy ● Much less time consuming to create and manage threads than processes ● Scalability ● Each thread can run in parallel on a different processor ● Operating System 2011/2012
  6. 6. User Threads Thread management done by user-level threads library is without the intervention of the kernel ● Fast to create and manager ● If the kernel is single threaded, any user-level thread performing a blocking system call will cause the entire process to block User thread libraries ● POSIX Pthreads ● Mach C-threads ● Solaris 2 UI-threads Operating System 2011/2012
  7. 7. Kernel Threads Supported by the Kernel ● Slower to create and manage than user threads ● If thread performs a blocking system call, the kernel can schedule another thread in the application for execution ● In multi-processor environments, the kernel can schedule threads on multiple processors Examples ● Windows 95/98/NT/2000 ● Solaris ● Linux Operating System 2011/2012
  8. 8. Multithreading Models ● One-to-One ● Many-to-One ● Many-to-Many Operating System 2011/2012
  9. 9. One-to-One Each user-level thread maps to kernel thread. ● Another thread can run when one thread makes a blocking call ● Multiple threads an run in parallel on a MP machine ● Overhead of creating a kernel thread for each user thread ● Most implementations limit the number of threads supported Examples ● Windows 95-2000 ● OS/2 ● Linux Operating System 2011/2012
  10. 10. Many-to-One Many user-level threads mapped to single kernel thread. ● Efficient - thread management done in user space ● Entire process will block if a thread makes a blocking system call ● Only one thread can access the kernel, no parallel processing in MP environment Operating System 2011/2012
  11. 11. Many-to-Many Allows many user level threads to be mapped to smaller or equal number of kernel threads. As many user threads as necessary can be created. Corresponding kernel threads can run in parallel on a multiprocessor. When a thread performs a blocking system call, the kernel can schedule another thread for execution. Allows true concurrency in a MP environment and does not restrict number of threads that can be created. Operating System 2011/2012
  12. 12. Two Level Model Has all the functionality of the many-to many model but also allows a user-level thread to be bound to a kernel thread Operating System 2011/2012
  13. 13. API API for creating and managing threads ● No kernel support, strictly in use space so no system calls involved ● Kernel level directly supported by the OS. All code and data structures for the library exits in kernel spacer ● An API call typically invokes a system call ● Three main libraries in use ● POSIX (Portable Operating System Interface) threads ● Win32 ● Java Operating System 2011/2012
  14. 14. Pthreads Pthreads refers to the POSIX standard (IEEE 1003.1c) deifining an API for thread creation and synchronization ● Pthreads is an IEEE and Open Group certified product ● The Open Group is a vendor-neutral and technology-neutral consortium, whose vision of Boundaryless Information Flow™ will enable access to integrated information, within and among enterprises, based on open standards and global interoperability. ● This is a specification for thread behavior not an implementation ● Implemented by Solaris, Linux, MacOS X and Tru64 ● Operating System 2011/2012
  15. 15. 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 ● A Windows XP application runs as a separate process and each process may contain one or more threads ● 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) ● Also provides support for a fiber library which implements the many-to-many model ● Operating System 2011/2012
  16. 16. Java Java threads may be created by extending the Thread class or implementing the Runnable interface ● Java threads are managed by the JVM ● Not really a user- or kernel-level thread, support provided at the language level ● No global data in Java, data is shared by passing the reference to the appropriate threads ● The JVM specification does not indicate how threads should be mapped to the underlying OS ● Windows 95/98/NT/2000 use the one-to-one model (each Java thread maps to a kernel thread) ● Solaris 2 used the many-to-many model ● Operating System 2011/2012
  17. 17. Java Thread State Operating System 2011/2012
  18. 18. Thread pools Though creating a new thread is more efficient than creating a new process, there are still some issues There is a delay in creating the thread and it will be discarded when done There is no limit put on how many threads are allowed to be created and system resources could be exhausted Create a number of threads in a pool where they await work. When needed, thread is awoken, when finished returns to pool to await more work Operating System 2011/2012
  19. 19. Thread pools – cont. Advantages: ● ● ● Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool Size of the pool can be dynamically adjusted based on usage patterns Operating System 2011/2012