Treadfinal
Treadfinal
5
Thread Relationship to
Processes
6
Thread/example
A process is an executing instance of an application. What does
that mean? Well, for example, when you double-click the Microsoft
Word icon, you start a process that runs Word. A thread is a path
of execution within a process. Also, a process can contain multiple
threads. When you start Word, the operating system creates a
process and begins executing the primary thread of that process.
It’s important to note that a thread can do anything a process can
do. But since a process can consist of multiple threads, a thread
could be considered a ‘lightweight’ process. Thus, the essential
difference between a thread and a process is the work that each
one is used to accomplish. Threads are used for small tasks,
whereas processes are used for more ‘heavyweight’ tasks –
basically the execution of applications.
7
Multithreading vs. Single threading
Multithreading: The OS supports multiple
threads of execution within a single process
Single threading: The OS does not recognize
the separate concept of thread
MS-DOS supports a single user process and a
single thread
Traditional UNIX supports multiple user
threads
8
Single Threaded and
Multithreaded Process Models
for IPC
10
Multithreaded Server
Architecture
thread
thread
thread
thread
Benefits of Threads
Example 1: File Server on a LAN
Needs to handle many file requests
over a short period
Threads can be created (and later
destroyed) for each request
If multiple processors: different
threads could execute simultaneously
on different processors
Example 2: Spreadsheet on a single
processor machine:
One thread displays menu and reads
user input while the other executes
the commands12and updates display
Thread States: Life Cycle of a
Thread
Thread states
Born state
Ready state (runnable state)
Running state
Dead state
Blocked state
Waiting state
Sleeping state
Sleep interval specifies for how long a thread will
sleep
13
Thread States: Life Cycle of a Thread
14
Kernel Threads
A kernel thread is responsible for
executing a specific function.
It shares the kernel text and
global data, and has its own
kernel stack.
Independently scheduled.
Inexpensive
User Threads(User
Application Thread)
All thread management is done by
the application
The kernel is not aware of the
existence of threads
Thread switching does not require
kernel mode privileges
Scheduling is application specific
User Threads
Lightweight Processes
LWP is a kernel-supported user
thread.
It belongs to a user process.
Independently scheduled.
Share the address space and other
resources of the process.
LWP should be synchronized on
shared data.
Blocking an LWP is expensive.
Lightweight processes
Thread Termination
Thread termination (cancellation)
Differs between thread implementations
Prematurely terminating a thread can
cause subtle errors in processes
because multiple threads share the
same address space
Some thread implementations allow a
thread to determine when it can be
terminated to prevent process from
entering inconsistent state 20
Dead Children may become
Zombies
When a process terminates
but its parent does not wait
for it?
22