Threads
Thread Overview
• Threads are mechanisms that permit an application to perform
multiple tasks concurrently
• Thread is a basic unit of CPU utilization
– Thread ID
– Program counter
– Register set
– Stack
• A single program can contain multiple threads
– Threads share with other threads belonging to the same
process
• Code, data, and other resources such as open files and
signals
Process Vs Thread
Pro Thread
cess
1 Process is heavy weight or resource Thread is light weight, taking lesser
intensive. resources than a process.
2 Process switching needs interaction with Thread switching does not need to interact
operating system. with operating system.
3 In multiple processing environments, All threads can share same set of open files,
each process executes the same code but child processes.
has its own memory and file resources.
4 If one process is blocked, then no other While one thread is blocked and waiting, a
process can execute until the first process second thread in the same task can run.
is unblocked.
5 Multiple processes without using threads Multiple threaded processes use fewer
use more resources. resources.
6 In multiple processes each process One thread can read, write or change
operates independently of the others. another thread's data.
Single and Multithreaded Processes
Traditional
(heavyweight)
process has a
single thread
of control
heavyweight process lightweight process
Threads
Threads share…. Threads specific
Attributes….
• Global memory
Thread ID
• Process ID and parent
process ID Thread specific data
• Controlling terminal CPU affinity
• Process credentials (user ) Stack (local variables and
• Open file information function call linkage
• Timers information)
• ……… ……
Thread control block
Thread ID (TID)
PC
Registers
State
Priority
Event information
Scheduling related information
Pointer to owner process
TCB Pointer
Benefits
• Low context switch overhead
In thread switching, the info to be saved and retrieved is less as
compared to process switching.
• Sharing of Resources
All threads share the resources allocated to their owner process
• No need of IPC
Threads do not required system call to communicate with each
other. As they share code and data section of process.
• High Computation speed up
Due to less information in TCB and low context switch overhead,
there is tremendous increase in computation speed
• Decreased response time
As a consequence of increased computation speed, the
response time to a user decreases in spite of execution of
many other tasks.
Types of thread
User level -> user threads
Supported above the kernel and are managed without kernel
support
Kernel level -> kernel threads
Supported and managed directly by the operating system
User threads
User threads
The user threads are those threads which are managed by the
application in user space only.
User threads are created and further managed through a thread
library provided by the OS.
A thread table is maintained for user threads. The thread table has
the entries to point the TCB of each thread.
User threads
Creating and managing the user threads is easy and much faster
as compared to a process and a kernel thread.
User threads are more portable also due to their nature as they
do not require the support from kernel making them independent
of a particular operating system.
User level thread implementations are known as many-to-one
mappings as the operating system maps all threads in a
multithreaded process to a single execution context.
User Threads
• Thread management done by user-level threads library
• Library provide support for creation, scheduling and
management
• Kernel unaware of user level threads
• Generally fast to create and manage
• Three primary thread libraries:
– POSIX Pthreads
– Win32 threads
– Java threads
Kernel threads
Kernel threads implemented in the kernel space are managed
through system calls, there is no library routine facility
Kernel threads are managed through a single thread table
maintained in kernel space.
There is one-to-one mapping in kernel threads that provides each
user thread with a kernel thread that it can dispatch.
The user process requests a kernel thread using a system call and
in turn the OS creates a kernel thread that executes user thread’s
instruction
Kernel threads
Kernel Threads
• Supported directly by the operating system
• Kernel performs creation, scheduling and management in
kernel space
• Generally slower to create and manage than are user threads
• Examples
– Windows XP/2000
– Solaris
– Linux
– Tru64 UNIX
– Mac OS X
Hybrid threads
Multithreading Models
User Thread – to - Kernel Thread
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
Many user-level threads
mapped to single kernel
thread
One-to-One
Each user-level thread maps to kernel thread
Examples
Windows NT/XP/2000
Linux
Many-to-Many Model
Allows many user level
threads to be mapped to
many kernel threads
Allows the operating
system to create a sufficient
number of kernel threads
Example
Windows NT/2000 with the
ThreadFiber package
User Level vs Kernel Level Thread
USER LEVEL THREAD KERNEL LEVEL THREAD
User thread are implemented by users. kernel threads are implemented by OS.
OS doesn’t recognized user level threads. Kernel threads are recognized by OS.
Implementation of Kernel thread is
Implementation of User threads is easy.
complicated.
Context switch time is less. Context switch time is more.
Context switch requires no hardware support. Hardware support is needed.
If one kernel thread perform blocking
If one user level thread perform blocking
operation then another thread can continue
operation then entire process will be blocked.
execution.
Example : Java thread, POSIX threads. Example : Window Solaris.
Pthreads
• a POSIX standard (IEEE 1003.1c) API for thread
creation and synchronization.
• Specification not implementation
• API specifies behavior of the thread library,
implementation is up to development of the
library.
• Common in UNIX operating systems.
Operating System Concepts
Solaris 2 Threads
Operating System Concepts
Windows 2000 Threads
• Implements the one-to-one mapping.
• Each thread contains
- a thread id
- register set
- separate user and kernel stacks
- private data storage area
Operating System Concepts
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)
Operating System Concepts
Java Threads
• Java threads may be created by:
– Extending Thread class
– Implementing the Runnable interface
• Java threads are managed by the JVM.
Operating System Concepts
Java Thread States
Operating System Concepts