Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Threads

Prepared by Aswathy K.R ,Asst. Professor,Dept.of CSE, CE Munnar


Threads
Each thread is a portion of a program that can execute
concurrently with other threads (multithreading)
 C and C++ are single-threaded
 Gives Java powerful capabilities not found in C and C++

Example: downloading a video clip


 Instead of having to download the entire clip then play it:
 Download a portion, play that portion, download the next
portion, play that portion... (streaming)
THREAD
A thread is contained inside a Process and threads
process and different threads in
the same process share some
resources (most commonly
memory), while different
processes do not.

Prepared by Aswathy K.R ,Asst. Professor,Dept.of


CSE, CE Munnar
Thread Overview
Threads are mechanisms that permit an application to perform
multiple tasks concurrently.

Thread sometimes called a lightweight process is a basic unit


of CPU utilization.
 Thread comprises
Thread ID
Program counter
Register set
Stack
Threads
Most software applications that run on modern computers are
multithreaded.
An application typically is implemented as a separate process
with several threads of control.
Eg: A web browser might have one thread display images or
text while another thread retrieves data from the network,.
A word processor may have a thread for displaying graphics,
another thread for responding to keystrokes from the user, and
a third thread for performing spelling and grammar checking in
the background.
Threads
A single program can contain multiple threads
Threads share with other threads belonging to the same
process
 Code, data, open files…….
Single and Multithreaded Processes

heavyweight process lightweight process

Prepared by Aswathy K.R ,Asst. Professor,Dept.of


CSE, CE Munnar
Threads
Threads specific
Threads share…. Attributes….
 Thread ID
Global memory
 Thread specific data
Process ID and parent
 Stack (local variables and function
process ID call linkage information)
Open file information  ……
Timers
………
Benefits

Responsiveness
Interactive application can delegate background functions to a thread and keep
running.

Resource Sharing
Several different threads can access the same address space

Economy
Allocating memory and resources for new processes is costly. Threads are
much ‘cheaper’ to initiate.

Scalability
Use threads to take advantage of multiprocessor architecture
Multithreaded Server Architecture

thread

thread

thread

thread
Multithreading Models
Support provided at either

User level -> user threads


Supported above the kernel and managed without kernel
support

Kernel level -> kernel threads


Supported and managed directly by the operating system

What is the relationship between user and kernel threads?


User Threads

Thread management done by user-level threads library


Thread creation, scheduling, are done in user level
Fast to create and manage
Drawback:
If kernel is single thread, then user level thread performing a
blocking system call will cause entire process to block

Three primary thread libraries:


 POSIX Pthreads
 Win32 threads
 Java threads
Kernel Threads

Supported by OS
Thread creation, scheduling, are done by kernel
Thread management is performed by os, thus kernel thread are slow.
If thread perform blocking system call, kernel can schedule another
thread in application for execution

Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
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

You might also like