Threads
Definition
A thread is called a lightweight process
lightweight process – easy to create and destroy
Thread comprises of:
Thread ID
Program Counter – keeps track of which instruction to execute next
Register Set – holds current working variables
Stack – execution history
Examples
Word processor
A word processor may have a thread for displaying graphics, another thread
for reading keystrokes from the user, a third thread for performing spelling
and grammar checking in the background and a fourth thread for saving
Web Browser
A web browser might have one thread for displaying images or text while
another thread retrieves data from the network
List of Items shared by all List of Items private to each
threads in a process thread
Address Space Program Counter
Global Variables Registers
Child Process Stack
Signals and Signal Handlers State
Benefits of Threads
1) Responsiveness: Multithreading an interactive application may allow a program to
continue running even a part of it is blocked or is performing a lengthy operation, thereby
increasing responsiveness to the user
2) Resource Sharing: threads share the memory and the resources of the process to which
they belong. The benefit of code sharing is that it allows an application to have several different
threads of activity all within the same address space
3) Economy: As the threads share resources of the process to which they belong, it is more
economical to create and context switch threads. It is much more time consuming to create and
manage processes than threads
4) Utilization of Multiprocessor Architectures: running in parallel or different
processors, increases concurrency
User and Kernel Threads
User Threads:
implemented by a thread library
Library provides- thread creation, scheduling
Management with no support from the kernel
Advantage - As no support is need from the kernel and all work is done in
the user space threads are generally fast to create and manage
Disadvantage – any user–level thread performing a blocking system call will
cause the entire process to block, even if other threads are available to run
within the application
User and Kernel Threads
Kernel Threads:
Supported directly by the operating system
Kernel performs thread creation, scheduling and management
Advantage – if a thread performs a blocking system call, the kernel can
schedule another thread in the application for execution
Disadvantage – because thread management is done by the operating
system, kernel threads are generally slower to create and manage than are
user threads
Multithreading Models
Many-to-One Model
Many user-level threads
mapped to single kernel thread
Thread management is done
in the user space
Because only one thread can
access the kernel at time,
multiple threads are unable to
run in parallel on
multiprocessors
Multithreading Models
One-to-One Model
The one-to-one model maps each user thread to a kernel thread
It allows multiple threads to run in parallel on multiprocessors
The only drawback to this model is that creating a user thread requires
creating the corresponding kernel thread
Multithreading Models
Many-to-Many Model
The many-to-many 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
Developers can create as many user
threads as necessary, and the
corresponding kernel threads can
run in parallel on a multiprocessor
Difference between Process and Thread
• Process is program in execution, ‘heavyweight process’
• Threads are used for small tasks, ‘lightweight process’
Threads Processes
Will by default share memory Will by default not share memory
Will share file descriptors Most file descriptors not shared
Will share file system context Don't share file system context
Will share signal handling Don't share signal handling