You are on page 1of 10

8/11/2021

Chapter 4
Threads

Based on Operating Systems:


Internals and Design Principles, William Stallings – 8th Edition

What is a Thread?

• Individual and separate unit of execution that is part of a process


• multiple threads can work together to accomplish a common goal
• Video Game example
• one thread for graphics
• one thread for user interaction
• one thread for networking

1
8/11/2021

What is a Thread?

Video Game
Process

video networking

interaction

Processes and Threads


•Multithreading - The ability of an OS to
support multiple, concurrent paths of
execution within a single process
•The unit of resource ownership is referred to
as a process or task
•The unit of dispatching is referred to as a
thread or lightweight process

2
8/11/2021

One or More Threads in a Process

Each thread has:

• an execution state (Running, Ready, etc.)


• saved thread context when not running (TCB)
• an execution stack
• some per-thread static storage for local
variables
• access to the shared memory and resources of
its process (all threads of a process share this)

Benefits of Threads
• Takes less time to create a new thread than a process
• Less time to terminate a thread than a process
• Switching between two threads takes less time than
switching between processes
• Threads enhance efficiency in communication between
programs

Disadvantages???

3
8/11/2021

Threads
•an OS that supports threads, scheduling and
dispatching is done on a thread basis
•Most of the state information dealing with
execution is maintained in thread-level data
structures
•suspending a process involves suspending all
threads of the process
•termination of a process terminates all threads
within the process

The key states for a thread are:


• Running
• Ready
• Spawn (create)
• Block
• Unblock
• Finish

4
8/11/2021

• A key issue with threads is whether or not they


can be scheduled independently of the process
to which they belong

• Or, is it possible to block one thread in a process


without blocking the entire process?
• If not, then much of the flexibility of
threads is lost

Multithreading on a Uniprocessor

10

5
8/11/2021

Thread Synchronization
•It is necessary to synchronize the activities of the
various threads
•all threads of a process share the same
address space and other resources
•any alteration of a resource by one thread
affects the other threads in the same
process
•For example, if two threads each try to
add an element to a doubly linked list at
the same time, one element may be lost
or the list may end up malformed.

11

Types of Threads

User Level
Thread (ULT)
Kernel level Thread
(KLT)

NOTE: we are talking about threads for user


processes. Both ULT & KLT execute in user
mode. An OS may also have threads but that
is not what we are discussing here.

12

6
8/11/2021

User-Level Threads (ULTs)


• Thread management
is done by the
application
• The kernel is not
aware of the existence
of threads

13

Trace The Execution Of Process B

14

7
8/11/2021

Advantages Vs Disadvantages of ULTs

• Thread switching does not • In a typical OS many system calls


require kernel mode privileges are blocking
(no mode switches) • as a result, when a ULT executes
• Scheduling can be application a system call, not only is that
specific thread blocked, but all of the
• ULTs can run on any OS threads within the process are
blocked
• In a pure ULT strategy, a
multithreaded application
cannot take advantage of
multiprocessing

15

Kernel-Level Threads (KLTs)


Thread management is
done by the kernel (could
call them KMT)

Advantages of KLTs

16

8
8/11/2021

Combined Approaches
• Thread creation is done in the
user space
• Bulk of scheduling and
synchronization of threads is
by the application
• Solaris is an example

17

Relationship Between
Threads and Processes

Table 4.2 Relationship between Threads and Processes

18

9
8/11/2021

Multiple Cores & Multithreading


• Multithreading and multicore chips have the
potential to improve performance of
applications that have large amounts of
parallelism
• Gaming, simulations, etc. are examples
• Performance doesn’t necessarily scale
linearly with the number of cores …

19

Checkpoint
• Give four general examples of the use of threads in a single-user multiprocessing
system.
• What resources are typically shared by all of the threads of a process?
• List three advantages of ULTs over KLTs.
• List two disadvantages of ULTs compared to KLTs.
• It was pointed out that two advantages of using multiple threads within a process
are that (1) less work is involved in creating a new thread within an existing process
than in creating a new process, and (2) communication among threads within the
same process is simplified. Is it also the case that a mode switch between two threads
within the same process involves less work than a mode switch between two threads
in different processes?
• If a process exits and there are still threads of that process running, will they continue
to run?

20

10

You might also like