Thread
Soma Hazra
Assistant Professor
Dept of Computer Science
1
Outline
Introduction
Process ~Threads
User threads ~ Kernel threads
Multithreading Models
Threading Issues
Some examples of threads
2
Introduction
• A thread is also called light weight process (LWP), is a
basic unit of CPU utilization.
• It comprises a thread id, a program counter, a register
set and a stack (pointer).
• It shares with other threads belonging to the same
process its code section, data section, and other OS
resources (such as open files and signals).
• A traditional (or heavyweight) process has a single
thread of control.
• If the process has multiple threads of control, it can
do more than one task at a time.
3
Single ~ Multithreaded Processes
4
Benefits
• Responsiveness:
– Even if part of a application is blocked, it still allow the
application to continue. (e.g. web browser)
• Resource Sharing
– By default, threads share the memory and the resources of the
process to which they belong.
• Economy
– Allocating memory and resources for process creation is costly.
• Utilization of Multiprocessor Architectures
– The benefits of multithreading can be greatly increased in a
multiprocessor architecture, where each thread may be
running in parallel on a different processor.
5
Process -- Thread
• Process can’t share the • Threads can share
same address space. address space and files.
• Process creation is • Thread creation is
slower as compared to faster as compared to
threads. process.
• Process takes more time • Thread takes less time
to complete execution. to complete execution.
• Processes takes more • Threads takes less time
time to switch among to switch among them.
them. • Threads are tightly
• Processes are loosely coupled.
coupled.
6
User Thread
• Thread management is done at user-level by using a
thread library.
• The library provides support for thread creation,
scheduling, and management with no support from
the kernel (No intervention of kernel).
• Therefore, user-level threads are generally fast to
create and manage.
• User-level threads can run on any OS.
• If the kernel is single-threaded, then any user-level
thread performing a blocking system call will cause
the entire process to block, even if the other threads
are available to run with in the application.
• Examples
- POSIX Pthreads
- Solaris 2 UI-threads and Mach C-threads.
7
Kernel Thread
• Kernel threads are directly supported by the operating
system.
• The kernel performs thread creation, scheduling and
management in kernel space.
• As thread management is done by O.S, kernel threads are
generally slower to create & manage than the user thread.
• But, if a thread performs a blocking system call, unlike user-
thread, it will not block the entire process.
• Examples
- Windows 95/98/NT/2000
- Solaris 2
- Tru64 UNIX/Linux
8
Multithreading Model
• Many system provide support for both user and kernel
threads, resulting different multithreading models.
• There are three common types of multithreading
models:
– Many-to-One
– One-to-One
– Many-to-Many
9
Many-to-One
• Many user-level threads
mapped to a single kernel
thread.
• Thread management is done
in user space. So, it is fast and
efficient.
• But the entire process will
block if a single thread will
make a blocking system call.
• Used on systems that do not
support kernel threads.
Example: green threads a thread library available for solaris
2 uses this model
10
One-to-One
• Each user-level thread maps to kernel thread.
• It provides more concurrency than the many-to-one
model by allowing another thread to run when a thread
makes a blocking system call.
• It also allows multiple threads to run in parallel on
multiprocessor
• The only drawback is that creating a user thread requires
creating the corresponding kernel threads
•Examples
Windows
98/NT/2000
OS/2
11
Many-to-Many
• This model multiplexes many user-level threads to a
smaller or equal number of kernel threads.
• The number of kernel threads may be specific to
either a particular application or a particular
machine.
•Examples
Solaris 2
IRIX
True64 UNIX
OS/2
12
Threading Issues
• The fork() and exec() system calls.
• Thread cancellation
– A thread that is to be cancelled is often referred to as target thread.
– Cancellation of target thread may occur in two different scenarios:
Asynchronous cancellation : immediately terminates the other.
Deferred cancellation : periodically check if it should terminate.
• Thread pools
– If we allow all concurrent request to be serviced in a new thread (no
bound in number of threads concurrently active in the system), it
could exhaust system resources such as CPU time or memory.
– The general idea behind a thread pool is to create a number of
threads at process startup and place them into a pool, where they
can sit and wait for work.
• Thread specific data
13
Pthreads
• Pthreads refers to the POSIX standard (IEEE
1003.1c) defining an API for thread creation
and synchronization.
• This is a specification for thread behavior, not
an implementation. i.e. API specifies
behavior of the thread library,
implementation is up to development of the
library.
• Common in UNIX operating systems such as Solaris
2.
14
Solaris 2 Threads
• Solaris 2 is a version of UNIX with support for
threads at the kernel and user levels.
15
Solaris Process
16
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
17
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
18
Java Threads
• Java threads may be created by:
– Extending Thread class
– Implementing the Runnable interface
• Java threads are managed by the JVM.
19
Java Thread States
20