0% found this document useful (0 votes)
40 views22 pages

Treadfinal

Uploaded by

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

Treadfinal

Uploaded by

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

Thread Overview

 Threads are mechanisms that permit


an application to perform multiple
tasks concurrently.
 A single program can contain
multiple threads
 Threads share with other threads
belonging to the same process
 Code, data, open files…….
Definition of Thread
 Thread
 Lightweight process (LWP)
 Threads of instructions or thread of control
 Shares address space and other global
information with its process
 Registers, stack and other thread-specific data
are local to each thread
 Threads may be managed by the
operating system or by a user application
 Examples: Win32 threads
2
Process & thread
 Both processes and threads are
independent sequences of execution.
The typical difference is that threads
(of the same process) run in a shared
memory space, while processes run
in separate memory spaces.
 Each process is started with a single
thread, often called the primary
thread, but can create additional
threads from any of its threads.
3
thread
 A thread is the entity within a process that
can be scheduled for execution. All threads
of a process share its virtual address space
and system resources
 A process contains one or more threads. In
single-threaded processes, the process
contains one thread. You can say the
thread is the process—there is one thing
going on. In multithreaded processes, the
process contains more than one thread—
there's more than one thing going on.
4
Threads and Processes

one process one process


one thread multiple threads

multiple processes multiple processes


one thread per process multiple threads per process

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

processes but only one thread per process


 Solaris and Windows 2000 support multiple

threads

8
Single Threaded and
Multithreaded Process Models

Thread Control Block contains a register


image, thread priority and thread state
9
information
Benefits of Threads vs
Processes
 Far less time to create a new thread than a
new process
 Less time to terminate a thread than a
process
 Less time to switch between two threads
within the same process than to switch
between processes
 Threads can communicate via shared
memory
 processes have to rely on kernel services

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?

 Where the entry is still


needed to allow the parent
process to read its child's exit
status.

 Zombie (process id (pid)


entry remains in system
process table)
21
Orphans
 When the parent process terminates
first?
 “Child is orphaned”
 Child is “re-parented” to special init
system process
 This operation is called re-parenting and
occurs automatically
 Child continues to consume resources

22

You might also like