Threads
in operating systems
By
Syeda Farheen Naz
Haji Gul
Sajid
Mudassir
What are Threads?
• Thread is an execution unit which consists of its own program
counter, a stack, and a set of registers. Threads are also known
as Lightweight processes.
• Threads are popular way to improve application through
parallelism.
• As each thread has its own independent resource for process
execution, multiple processes can be executed parallel by
increasing number of threads.
Why use Threads?
• Large multiprocessors need many computing entities (one per
CPU)
• Switching between processes incurs high overhead
• With threads, an application can avoid per-process overheads
– Thread creation, deletion, switching cheaper than processes
• Threads have full access to address space (easy sharing)
• Threads can execute in parallel on multiprocessors
Single Threaded Approaches
• A single execution path per process, in which the concept of a
thread is not recognized, is referred to as a single-threaded
approach
• MS-DOS is an example of an OS that supports a single user
process and a single thread.
• some versions of UNIX also supported this type of process.
Multithreaded Approaches
• A Java run-time environment is a system of one process with
multiple threads; Windows also support multiple multithreaded
processes
Concurrent Execution on a Single-core System
Parallel Execution on a Multi core System
Types of Thread
There are two types of threads :
1. User level Thread management done by user-level threads
library(Pthreads,Win32 threads, Java threads)
2. Kernel level Supported by the Kernel/OS(Linux ,Windows
XP/2000)
8
Advantages and disadvantages of ULT
Advantages
• Thread switching does not
involve the kernel: no
mode switching, therefore
fast
• Scheduling can be
application specific:
choose the best algorithm
for the situation.
• Can run on any OS. We
only need a thread library
Disadvantages
• Most system calls are
blocking for processes. So
all threads within a process
will be implicitly blocked
• The kernel can only assign
processors to processes.
Two threads within the
same process cannot run
simultaneously on two
processors
9
Advantages and disadvantages of KLT
Advantages
• The kernel can schedule
multiple threads of the same
process on multiple
processors
• Blocking at thread level, not
process level
– If a thread blocks, the
CPU can be assigned to
another thread in the
same process
• Even the kernel routines can
be multithreaded
Disadvantages
• Thread switching always
involves the kernel. This
means 2 mode switches
per thread switch
• So it is slower compared to
User Level Threads
– (But faster than a full
process switch)
10
Few Implementation
Libraries
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
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
11
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)
Java Threads
• Java threads may be created by:
– Extending Thread class
– Implementing the Run able interface
• Java threads are managed by the JVM.
Cooperative Threads
• Cooperative threads use non pre-emptive scheduling
• Advantages:
– Simple
• Scientific apps
• Disadvantages:
– For badly written code
• Scheduler gets invoked only when Yield is called
• A thread could yield the processor when it blocks for I/O
Non-Cooperative Threads
• No explicit control passing among threads
• Rely on a scheduler to decide which thread to run
• A thread can be pre-empted at any point
• Often called pre-emptive threads
• Most modern thread packages use this approach.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the
following strategies.
• Many-To-One Model
• One-To-One Model
• Many-To-Many Model
Many-To-One Model
• In the many-to-one model, many user-level threads are all
mapped onto a single kernel thread.
• Thread management is handled by the thread library in user space,
which is efficient in nature.
One-To-One Model
• The one-to-one model creates a separate kernel thread to
handle each and every user thread.
• Most implementations of this model place a limit on how
many threads can be created.
• Linux and Windows from 95 to XP implement the one-to-one
model for threads.
Many-To-Many Model
• The many-to-many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads,
combining the best features of the one-to-one and many-to-one
models.
• Users can create any number of the threads.
• Blocking the kernel system calls does not block the entire
process.
• Processes can be split across multiple processors.
Thread Life Cycle in JAVA
The Life cycle of thread in Java is controlled by JVM. The Java thread states are as
follows.
 New
The thread is in new state if you create an instance of Thread class but before
the invocation of start() method.
 Runnable
The thread is in runnable state after invocation of start() method, but the
thread scheduler has not selected it to be the running thread.
 Running
The thread is in running state if the thread scheduler has selected it.
 Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to
run.
 Terminated
A thread is terminated when run() method terminates.
Non
Runnable
(Bocked)
New
Runnable
Running
Terminated
Start()
Sleep, block on
I/O, suspend,
wait
Sleep done, I/O
complete,
resume, notify
Thread Control Block
• Thread Control Block (TCB) is a data structure in the operating system kernel
which contains thread-specific information needed to manage it. The TCB is "the
manifestation of a thread in an operating system".
• An example of information contained within a TCB is:
• Stack pointer: Points to thread's stack in the process
• Program counter
• State of the thread (running, ready, waiting, start, done)
• Thread's register values
• Pointer to the Process control block (PCB) of the process that the thread lives on
The Thread Control Block acts as a library of information about the threads in a
system. Specific information is stored in the thread control block highlighting
important information about each thread.
Thread

Thread

  • 1.
    Threads in operating systems By SyedaFarheen Naz Haji Gul Sajid Mudassir
  • 2.
    What are Threads? •Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Threads are also known as Lightweight processes. • Threads are popular way to improve application through parallelism. • As each thread has its own independent resource for process execution, multiple processes can be executed parallel by increasing number of threads.
  • 3.
    Why use Threads? •Large multiprocessors need many computing entities (one per CPU) • Switching between processes incurs high overhead • With threads, an application can avoid per-process overheads – Thread creation, deletion, switching cheaper than processes • Threads have full access to address space (easy sharing) • Threads can execute in parallel on multiprocessors
  • 4.
    Single Threaded Approaches •A single execution path per process, in which the concept of a thread is not recognized, is referred to as a single-threaded approach • MS-DOS is an example of an OS that supports a single user process and a single thread. • some versions of UNIX also supported this type of process.
  • 5.
    Multithreaded Approaches • AJava run-time environment is a system of one process with multiple threads; Windows also support multiple multithreaded processes
  • 6.
    Concurrent Execution ona Single-core System Parallel Execution on a Multi core System
  • 7.
    Types of Thread Thereare two types of threads : 1. User level Thread management done by user-level threads library(Pthreads,Win32 threads, Java threads) 2. Kernel level Supported by the Kernel/OS(Linux ,Windows XP/2000)
  • 8.
    8 Advantages and disadvantagesof ULT Advantages • Thread switching does not involve the kernel: no mode switching, therefore fast • Scheduling can be application specific: choose the best algorithm for the situation. • Can run on any OS. We only need a thread library Disadvantages • Most system calls are blocking for processes. So all threads within a process will be implicitly blocked • The kernel can only assign processors to processes. Two threads within the same process cannot run simultaneously on two processors
  • 9.
    9 Advantages and disadvantagesof KLT Advantages • The kernel can schedule multiple threads of the same process on multiple processors • Blocking at thread level, not process level – If a thread blocks, the CPU can be assigned to another thread in the same process • Even the kernel routines can be multithreaded Disadvantages • Thread switching always involves the kernel. This means 2 mode switches per thread switch • So it is slower compared to User Level Threads – (But faster than a full process switch)
  • 10.
    10 Few Implementation Libraries PThreads • APOSIX 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 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
  • 11.
    11 Linux Threads • Linuxrefers 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) Java Threads • Java threads may be created by: – Extending Thread class – Implementing the Run able interface • Java threads are managed by the JVM.
  • 12.
    Cooperative Threads • Cooperativethreads use non pre-emptive scheduling • Advantages: – Simple • Scientific apps • Disadvantages: – For badly written code • Scheduler gets invoked only when Yield is called • A thread could yield the processor when it blocks for I/O
  • 13.
    Non-Cooperative Threads • Noexplicit control passing among threads • Rely on a scheduler to decide which thread to run • A thread can be pre-empted at any point • Often called pre-emptive threads • Most modern thread packages use this approach.
  • 14.
    Multithreading Models The userthreads must be mapped to kernel threads, by one of the following strategies. • Many-To-One Model • One-To-One Model • Many-To-Many Model
  • 15.
    Many-To-One Model • Inthe many-to-one model, many user-level threads are all mapped onto a single kernel thread. • Thread management is handled by the thread library in user space, which is efficient in nature.
  • 16.
    One-To-One Model • Theone-to-one model creates a separate kernel thread to handle each and every user thread. • Most implementations of this model place a limit on how many threads can be created. • Linux and Windows from 95 to XP implement the one-to-one model for threads.
  • 17.
    Many-To-Many Model • Themany-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models. • Users can create any number of the threads. • Blocking the kernel system calls does not block the entire process. • Processes can be split across multiple processors.
  • 18.
    Thread Life Cyclein JAVA The Life cycle of thread in Java is controlled by JVM. The Java thread states are as follows.  New The thread is in new state if you create an instance of Thread class but before the invocation of start() method.  Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.  Running The thread is in running state if the thread scheduler has selected it.  Non-Runnable (Blocked) This is the state when the thread is still alive, but is currently not eligible to run.  Terminated A thread is terminated when run() method terminates.
  • 19.
    Non Runnable (Bocked) New Runnable Running Terminated Start() Sleep, block on I/O,suspend, wait Sleep done, I/O complete, resume, notify
  • 20.
    Thread Control Block •Thread Control Block (TCB) is a data structure in the operating system kernel which contains thread-specific information needed to manage it. The TCB is "the manifestation of a thread in an operating system". • An example of information contained within a TCB is: • Stack pointer: Points to thread's stack in the process • Program counter • State of the thread (running, ready, waiting, start, done) • Thread's register values • Pointer to the Process control block (PCB) of the process that the thread lives on The Thread Control Block acts as a library of information about the threads in a system. Specific information is stored in the thread control block highlighting important information about each thread.