You are on page 1of 32

THREADS

SUSHMA RAWAL

THREAD
A thread of execution is the smallest unit
of processing that can be scheduled by an
operating system.
OR
A thread is the basic unit of CPU
utilization.

SUSHMA RAWAL

THREAD
 A thread is the primitive that can execute code
 A thread is a single sequential flow of control within a
process
 A process can have many threads of execution
 Different threads, which are part of a process, share the
same address space; meaning they share the data memory,
code memory and heap memory area
 Threads maintain their own thread status (CPU register
values), Program Counter (PC) and stack

SUSHMA RAWAL

Threads
Threads are called light weight processes.
Processes are called heavy weight
processes.

SUSHMA RAWAL

Threads
Examples
A web browser
A word processor
A web server

SUSHMA RAWAL

Single and Multithreaded Processes

SUSHMA RAWAL

Benefits of multithreading
Responsiveness
Resource Sharing
Economy
Utilization of Multiprocessor Architectures

SUSHMA RAWAL

Similarities between Processes and


threads

Start a new process or thread


Terminate
Wait for each other to terminate
Terminate each other
Share data and communicate
Run independently
SUSHMA RAWAL

Difference between process and thread


Threads share the address space of the process that
created it; processes have their own address.
Threads have direct access to the data segment of
its process; processes have their own copy of the
data segment of the parent process.
Threads can directly communicate with other
threads of its process; processes must use
interprocess communication to communicate with
sibling processes.

SUSHMA RAWAL

Difference between process and thread


Threads have almost no overhead; processes have
considerable overhead.
New threads are easily created; new processes
require duplication of the parent process.
Threads can exercise considerable control over
threads of the same process; processes can only
exercise control over child processes.

SUSHMA RAWAL

10

Difference between process and thread


Changes to the main thread (cancellation, priority
change, etc.) may affect the behavior of the other
threads of the process; changes to the parent
process does not affect child processes.

SUSHMA RAWAL

11

Why Threads?

Threads can share common data, so they do not


need to use interprocess communication.
Because of the very nature, threads can take
advantage of multiprocessors.

SUSHMA RAWAL

12

Threads

Threads are cheap in the sense that,


They only need a stack and storage for registers
therefore, threads are cheap to create.
Threads use very little resources of an operating
system in which they are working. That is,
threads do not need new address space, global
data, program code or operating system
resources.

SUSHMA RAWAL

13

Threads
Context switching is fast when working with
threads. The reason is that we only have to save
and/or restore PC, SP and registers.

SUSHMA RAWAL

14

Threads
Threads are of two types
User level threads
Kernel level threads

SUSHMA RAWAL

15

Threads
User Level Thread:
User level threads do not have kernel/Operating System
support and they exist solely in the running process.
Even if a process contains multiple user level threads, the
OS treats it as single thread and will not switch the
execution among the different threads of it.
It is the responsibility of the process to schedule each
thread as and when required.
In summary, user level threads of a process are nonpreemptive at thread level from OS perspective.

SUSHMA RAWAL

16

User-Level Threads
User-level threads implement in user-level libraries, rather
than via systems calls, so thread switching does not need to
call operating system and to cause interrupt to the kernel.
Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads

SUSHMA RAWAL

17

User-Level Threads
Advantages:
Simple Representation:
Each thread is represented simply by a PC,
registers, stack and a small control block, all
stored in the user process address space.
Simple Management:
This simply means that creating a thread,
switching between threads and synchronization
between threads can all be done without
intervention of the kernel.
SUSHMA RAWAL

18

User-Level Threads
Fast and Efficient:
Thread switching is not much more expensive
than a procedure call.

SUSHMA RAWAL

19

User-Level Threads
Disadvantages:

There is a lack of coordination between threads


and operating system kernel.

Therefore, process as whole gets one time slice


irrespective of whether process has one thread or
1000 threads within.

It is up to each thread to relinquish control to


other threads.

SUSHMA RAWAL

20

User-Level Threads

User-level threads requires non-blocking systems


call i.e., a multithreaded kernel. Otherwise, entire
process will be blocked in the kernel, even if there
are runable threads left in the processes.
For example, if one thread causes a page fault, the
process blocks.

SUSHMA RAWAL

21

Kernel-Level Threads
In this method, the kernel knows about threads
and manages them.
Instead of thread table in each process, the kernel
has a thread table that keeps track of all threads in
the system.
In addition, the kernel also maintains the
traditional process table to keep track of processes.
Operating Systems kernel provides system call to
create and manage threads.

SUSHMA RAWAL

22

Kernel-Level Threads
Advantages:
Because kernel has full knowledge of all threads,
Scheduler may decide to give more time to a
process having large number of threads than
process having small number of threads.
Kernel-level threads are especially good for
applications that frequently block.

SUSHMA RAWAL

23

Kernel-Level Threads
Disadvantages:
The kernel-level threads are slow and inefficient.
For instance, threads operations are hundreds of
times slower than that of user-level threads.
Since kernel must manage and schedule threads as
well as processes. It require a full thread control
block (TCB) for each thread to maintain
information about threads. As a result there is
significant overhead and increase in kernel
complexity.
SUSHMA RAWAL

24

Kernel-Level Threads
Examples
- Windows 95/98/NT/2000
- Solaris
- Linux

SUSHMA RAWAL

25

User and Kernel level threads


Some operating system provide a combined user
level thread and Kernel level thread facility.
Solaris is a good example of this combined
approach.
In a combined system, multiple threads within the
same application can run in parallel on multiple
processors and a blocking system call need not
block the entire process.
Relationship must exist between user and kernel
threads.
SUSHMA RAWAL

26

Thread binding Models


Many-to-One Model:
Many user level threads are mapped to a single kernel thread.
Thread management is done by the thread library in user
space.
The kernel treats all user level threads as single thread and
the execution switching among the user level threads happens
when a currently executing user level thread voluntarily
blocks itself or relinquishes the CPU.
The entire process will block if a thread makes a blocking
system call.
Solaris Green threads and GNU Portable Threads are
examples for this.
SUSHMA RAWAL

27

Thread binding Models


One-to-One Model:
Each user level thread is bonded to a kernel/system level
thread.
It provides concurrency, by allowing another thread to run
when a thread makes a blocking system call.
Drawback in this model is that creating a user thread
requires creating corresponding kernel thread.
This model restricts the number of threads supported by
the system, as kernel threads have to be created.
Windows XP/NT/2000 and Linux threads are examples of
One-to-One thread models.

SUSHMA RAWAL

28

Thread binding Models


Many-to-Many Model:
In this model many user level threads are allowed to be
mapped to many kernel threads.
The number of kernel threads may be specific to either a
particular application or a particular machine.
When a thread performs a blocking system call, the kernel
can schedule another thread for execution.
Windows NT/2000 with ThreadFiber package is an
example for this.

SUSHMA RAWAL

29

Advantages of Threads over Multiple Processes

Context Switching Threads are inexpensive to


create and destroy, and they are inexpensive to
represent.
For example, they require space to store the PC,
SP, and the general-purpose registers, but they do
not require space to share memory information,
Information about open files of I/O devices in use,
etc.
With so little context, it is much faster to switch
between threads. In other words, it is relatively
easier for a context switch using threads.
SUSHMA RAWAL

30

Advantages of Threads over Multiple


Processes

Sharing : Threads allow the sharing of a lot


resources that cannot be shared in process, for
example, sharing code section, data section,
Operating System resources like open file etc.

SUSHMA RAWAL

31

Disadvantages of Threads over Multiprocesses


Blocking The major disadvantage is that if the
kernel is single threaded, a system call of one
thread will block the whole process and CPU may
be idle during the blocking period.
Security Since there is, an extensive sharing
among threads there is a potential problem of
security. It is quite possible that one thread over
writes the stack of another thread (or damage
shared data) although it is very unlikely since
threads are meant to cooperate on a single task.
SUSHMA RAWAL

32

You might also like