0% found this document useful (0 votes)
8 views

Operating Systems -Threads

The document discusses the concepts of processes and threads in operating systems, highlighting their differences, advantages, and disadvantages. It emphasizes the importance of threads in modern applications for efficient execution and resource sharing, as well as the challenges of multithreaded programming. Additionally, it covers user-level and kernel-level thread management, threading models, and typical usage scenarios.

Uploaded by

106123122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Operating Systems -Threads

The document discusses the concepts of processes and threads in operating systems, highlighting their differences, advantages, and disadvantages. It emphasizes the importance of threads in modern applications for efficient execution and resource sharing, as well as the challenges of multithreaded programming. Additionally, it covers user-level and kernel-level thread management, threading models, and typical usage scenarios.

Uploaded by

106123122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Operating Systems – Threads

Dr. M. Brindha
Associate Professor
Department of CSE
NIT, Trichy-15
Processes
• Separate streams of execution
• Each process isolated from the
other
• Process state contains
– Process ID
– Environment
– Working directory.
– Program instructions
– Registers
– Stack
– Heap
– File descriptors
• Created by the OS using fork
– Significant overheads

2
3
4
5
6
7
8
9
10
11
Threads
• Separate streams of execution
within a single process
• Threads in a process not isolated
from each other
• Each thread state (thread control
block) contains
– Registers (including EIP, ESP)
– stack

12
Why threads?
• Lightweight

Cost of creating 50,000 processes / threads


(https://computing.llnl.gov/tutorials/pthreads/)
• Efficient communication between entities
• Efficient context switching

13
Threads vs Processes
• A thread has no data • A process has code, heap,
segment or heap stack, other segments
• A thread cannot live on its • A process has at-least one
own. It needs to be thread.
attached to a process
• There can be more than • Threads within a process
one thread in a process. share the same I/O, code,
Each thread has its own files.
stack
• If a thread dies, its stack
• If a process dies, all threads
is reclaimed
die.

14
Other thread libraries
• Windows threads
• Boost (in C++)
• LinuxThreads
• etc.

17
Motivation
• Most modern applications are multithreaded
• Threads run within application
• Multiple tasks with the application can be
implemented by separate threads
• Update display
• Fetch data
• Spell checking
• Answer a network request
• Process creation is heavy-weight while thread
creation is light-weight
• Can simplify code, increase efficiency
• Kernels are generally multithreaded
Single and Multithreaded Processes
Multithreaded Server Architecture
Benefits
• Responsiveness – may allow continued execution
if part of process is blocked, especially important
for user interfaces
• Resource Sharing – threads share resources of
process, easier than shared memory or message
passing
• Economy – cheaper than process creation, thread
switching lower overhead than context switching
• Scalability – process can take advantage of
multicore architectures
Multicore Programming
• Multicore or multiprocessor systems putting pressure
on programmers, challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
• Parallelism implies a system can perform more than
one task simultaneously
• Concurrency supports more than one task making
progress
• Single processor / core, scheduler providing concurrency
 Concurrent execution on single-core system:

 Parallelism on a multi-core system:


Multicore Programming
• Types of parallelism
• Data parallelism – distributes subsets of the same
data across multiple cores, same operation on
each
• Task parallelism – distributing threads across cores,
each thread performing unique operation
Data and Task Parallelism
Who manages threads?
• Two strategies
– User threads
• Thread management done by user level thread
library. Kernel knows nothing about the threads.
– Kernel threads
• Threads directly supported by the kernel.
• Known as light weight processes.

27
User level threads
• Advantages:
– Fast (really lightweight)
(no system call to manage threads. The thread library
does everything).
– Can be implemented on an OS that does not support
threading.
– Switching is fast. No, switch from user to protected
mode.

• Disadvantages:
– Scheduling can be an issue. (Consider, one thread
that is blocked on an IO and another runnable.)
– Lack of coordination between kernel and threads. (A
process with 1000 threads competes for a timeslice
with a process having just 1 thread.)
– Requires non-blocking system calls. (If one thread
invokes a system call, all threads need to wait)

28
Kernel level threads
• Advantages:
• – Scheduler can 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.

• Disadvantages:
– The kernel-level threads are slow (they involve kernel
invocations.)
– Overheads in the kernel. (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.)

29
User Threads and Kernel Threads

• User threads - management done by user-level threads


library
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
• Kernel threads - Supported by the Kernel
• Examples – virtually all general -purpose operating systems,
including:
• Windows
• Linux
• Mac OS X
• iOS
• Android
Multithreading Models

• Many-to-One

• One-to-One

• Many-to-Many
Many-to-one model
user thread

• Many user level threads map to a single


kernel thread

• Pros:
– Fast. No system calls to manage threads.
– No mode change for switching threads

• Cons:
– No parallel execution of threads. All
threads block when one has a system call.
– Not suited for multi-processor systems.
kernel thread

32
One-to-one model
user thread

• Each user thread associated with


one kernel thread.
• Pros.
– Better suited for multiprocessor
environments.
– When one thread blocks, the other
threads can continue to execute.
• Cons.
– Expensive. Kernel is involved.

kernel thread

33
Many-to-Many model
• Many user threads mapped to many
kernel threads
– Supported by some unix and windows
versions
• Pros: flexible
– OS creates kernel threads as required
– Process creates user threads as needed
• Cons: Complex
– Double management

34
Threading issues
• What happens when a thread invokes fork?
– Duplicate all threads?
• Not easily done… other threads may be running or blocked
in a system call
– Duplicate only the caller thread?
• More feasible.
• Segmentation fault in a thread. Should only the
thread terminate or the entire process?

35
Typical usage of threads

event occurred
Event?

No event occurred
create
thread

Service
event

terminate
thread

Creating and terminating thread lead to overheads

36
Thread pools

event occurred
Event?

No event occurred Thread pool

assign job to thread


from pool

service
event

Block
Number of threads in pool is critical!
thread

37
Thank You!!!

You might also like