Threads
Threads
2.7 THREADS
2.7.1 Overview
A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set
of registers, (and a thread ID. )
• Traditional (heavyweight ) processes have a single thread of control - There is one
program counter, and one sequence of instructions that can be carried out at any given time.
• As shown in Figure multi-threaded applications have multiple threads within a single
process, each having their own program counter, stack and set of registers, but sharing
common code, data, and certain structures such as open files.
2.7.1.1 Motivation
• Threads are very useful in modern programming whenever a process has
multiple tasks to perform independently of the others.
• This is particularly true when one of the tasks may block, and it is desired to
allow the other tasks to proceed without blocking.
• For example in a word processor, a background thread may check spelling and
grammar while a foreground thread processes user input ( keystrokes ), while yet a third
thread loads images from the hard drive, and a fourth does periodic automatic backups
of the file being edited.
• Another example is a web server - Multiple threads allow for multiple requests
to be satisfied simultaneously, without having to service requests sequentially or to fork
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
off separate processes for every incoming request. (The latter is how this sort of thing
was done before the concept of threads was developed. A daemon would listen at a port,
fork off a child for every incoming request to be processed, and then go back to listening
to the port. )
2.7.1.2 Benefits
There are four major categories of benefits to multi-threading:
1. Responsiveness - One thread may provide rapid response while other threads are blocked or
slowed down doing intensive calculations.
2. Resource sharing - By default threads share common code, data, and other resources, which
allows multiple tasks to be performed simultaneously in a single address space.
3. Economy - Creating and managing threads (and context switches between them) is much
faster than performing the same tasks for processes.
4. Scalability, i.e. Utilization of multiprocessor architectures - A single threaded process can
only run on one CPU, no matter how many may be available, whereas the execution of a multi-
threaded application may be split amongst available processors.
(Note that single threaded processes can still benefit from multi-processor architectures when
there are multiple processes contending for the CPU, i.e. when the load average is above some
certain threshold.)
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
Many-to-one model
One-to-one model
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
Many-to-many model
One popular variation of the many-to-many model is the two-tier model, which allows
either many-to-many or one-to-one operation.
IRIX, HP-UX, and Tru64 UNIX use the two-tier model, as did Solaris prior to Solaris
9.
Two-level model
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
• Transition: A thread enters this state after waiting if it is ready to run but the resources
are not available. For example, the thread’s stack may be paged out of memory. When the
resources are available, the thread goes to the Ready state.
• Terminated: A thread can be terminated by itself, by another thread, or when its parent
process terminates. Once housekeeping chores are completed, the thread is removed from the
system, or it may be retained by the Executive 6 for future reinitialization.
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
• This helps reuse data still in that processor’s memory caches from the previous
execution of the thread.
• It is possible for an application to restrict its thread execution only to certain
processors (hard affinity).
CS8493-OPERATING SYSTEMS