Notes On Threads Subject code:CS2254 Subject Title:operating Systems Regulation:2008 Unit-1
Notes On Threads Subject code:CS2254 Subject Title:operating Systems Regulation:2008 Unit-1
www.vidyarthiplus.com
www.vidyarthiplus.com
What is a Thread????
A thread is a single sequence stream within in a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes. a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process . In a process, threads allow multiple executions of streams. In many respect, threads are popular way to improve application through parallelism. The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel. Like a traditional process i.e., process with one thread, a thread can be in any of several states (Running, Blocked, Ready or Terminated)
www.vidyarthiplus.com
www.vidyarthiplus.com
Better responsiveness to users Resource sharing within the process Economy for those that are convinced that creating a thread is more efficient than creating a process Scalability To make maximum use of the new hardware platforms, such as multiple cores. Thread management done by user-level threads library Three primary thread libraries: POSIX P threads Win32 threads Java threads Many-to-One One-to-One
User Threads
Multithreading Models
www.vidyarthiplus.com
www.vidyarthiplus.com
Many-to-Many
Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads
One-to-one
Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later
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
www.vidyarthiplus.com
www.vidyarthiplus.com
Threading Issues
Thread cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled 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) Signal handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals Signal is generated by particular event Signal is delivered to a process Signal is handled Options: Deliver the signal to the thread to which the signal applies
www.vidyarthiplus.com
www.vidyarthiplus.com
Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific threa to receive all signals for the process 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 create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool Scheduler activations Many:Many models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide up calls - a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads
Various Implementations
PThreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
www.vidyarthiplus.com
www.vidyarthiplus.com
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) Windows 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 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)
www.vidyarthiplus.com