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

Week 4

Uploaded by

sananazirf21
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)
10 views

Week 4

Uploaded by

sananazirf21
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/ 28

1

THREADS
Operating Systems
A Hypothetical Video Player 2

▪ Let us start by trying to design a video player, which should be


able to play a compressed video stream taking following steps
in playing the video:
o Reading the video from disk

o decompressing the video

o decode the video and display on the screen

Operating Systems
Solution 1 – Single Process 3

▪ If the video is very small, say a few Megabytes, we can read


the entire video into memory, decompress it and then display it
on the screen.
▪ However, consider
o what will happen when the video is larger than the actual memory
your computer has? and
o what if the video is downloaded from the Internet, and you want to
start watching it before the download completes (e.g., YouTube)?

Operating Systems
Solution 2 – Multiple Processes 4

▪ The task of playing the movie is easily decomposed into several independent
tasks, we can have
o one process read the movie,
o one to decode it and
o one which displays the movie on the screen.
▪ This solution will allow reading the bytes while decoding the frames and
decoding the frames while showing the previous ones on screen which will allow
for a much smoother experience!

▪ Issue - how will all of these processes communicate with each other?
▪ Interprocess communication is possible but that introduces a high cost in terms
of performance and requires a fair amount of coding, debugging and
maintaining.)

Operating Systems
Solution 3 – Multiple Threads 5

▪ Multiple threads can execute at the same time in a process


▪ Threads share resources allocated to the process
▪ They communicate using built-in constructs in modern languages
▪ Threads in the same process share the same memory space
▪ Each thread has its own stack
▪ They also share the same opened files and access rights
▪ When switching between threads, not all steps of a regular context
switch are needed
▪ No need to restore the process context, only the stack needs to be
switched
Operating Systems
Single-threaded and multithreaded processes 6

Operating Systems
Example: Multithreaded Server Architecture 7

▪ If the web server ran as a traditional single-


threaded process, it would be able to service
only one client at a time, and a client might
have to wait a very long time for its request to
be serviced.
▪ One solution: When the server receives a
request, it creates a separate process to
service that request. Process creation is time
consuming and resource intensive.
▪ 2nd Solution: If the web-server process is
multithreaded, the server will create a
separate thread that listens for client requests.
When a request is made, rather than creating
another process, the server creates a new
thread to service the request and resumes
listening for additional requests.

Operating Systems
Processes vs Threads 8

PROCESS THREAD
Refers to any program in execution Refers to a segment of a process
Takes more time to terminate Takes less time to terminate
Takes more time for creation less time for creation
Takes more time for context switching Takes less time for context switching
Less efficient in terms of communication More efficient in terms of communication
Consumes more resources Consumes fewer resources
Is isolated Shares memory
Considered heavy-weight Considered lightweight
Switching uses interface in the operating system Switching doesn't require a system call
Has its own Process Control Block, Stack, and Has the parent's PCB, its own Thread Control
Address Space Block, Stack, and common Address space
Changes to the parent process don't affect Changes to the main thread can affect the
child processes
Operating Systems behavior of other threads in the same process
Why Use Threads? 9

1. The programming model becomes 1. Responsiveness: Multithreading an


simpler interactive application may allow a
program to continue running even if
2. They are lighter weight than part of it is blocked or is performing a
processes, they are easier (i.e., lengthy operation
faster) to create and destroy than
2. Resource sharing: Threads share the
processes. memory and the resources of the
3. When there is substantial process to which they belong by
computing and also substantial I/O, default. Don’t need shared memory
having threads allows these and message passing.
activities to overlap, thus speeding 3. Economy: Thread creation consumes
up the application. less time and memory than process
creation.
4. Threads are useful on systems with
multiple CPUs, where real 4. Scalability: Threads may be running in
parallelism is possible parallel on different processing cores.
Operating Systems
Software Threads vs CPU Threads 10

▪ Software Threads
o One hardware thread can run many software threads.
o In modern operating systems, this is often done by time-
slicing
o Each thread gets a few milliseconds to execute before the OS
schedules another thread to run on that CPU.
o Since the OS switches back and forth between the threads
quickly, it appears as if one CPU is doing more than one
thing at once
▪ Hyper-threading
o is a process by which a CPU divides up its physical cores into
virtual cores that are treated as if they are actually physical
cores by the operating system.
o These virtual cores are also called threads.
o Most of Intel’s CPUs with 2 cores use this process to create 4
threads or 4 virtual cores
o Hyper-threading works by allowing each core in your CPU to
do two actions at the same time.
Operating Systems
Thread Usage 11

• One thread interacts with the


user
• Second thread handles
reformatting in the background
• Third thread - automatically
saving the entire file to disk
every few minutes

Figure 2-7. A word processor with three threads.


Operating Systems .
Thread Usage 12

Figure 2-8. A multithreaded Web server.


Operating Systems
Figure 2-8. A Multithreaded Web server. 1313

Dispatcher Thread: Worker Thread:


▪ Continuously reads incoming requests for ▪ Upon being awakened by the dispatcher, the
work from the network. worker thread checks the web page cache to
see if it contains the requested resource.
▪ Analyzes each request to determine the
▪ If the requested webpage resides in the cache
required action. (a shared resource accessible by all threads), the
▪ Selects an idle worker thread to handle worker thread retrieves it and transmits it back to
the request. the client.
▪ If the webpage isn't found in the cache, the
▪ Passes the request to the chosen worker worker thread initiates a disk read operation to
thread, potentially by placing a reference fetch the webpage from storage.
to it in a designated memory location ▪ The worker thread then enters a blocked state,
associated with that thread. waiting for the disk operation to complete.
▪ Awakens the selected worker thread, ▪ Once the disk I/O finishes, another thread
transitioning it from a blocked state to a (potentially the dispatcher) is chosen to run.
ready state.

Operating Systems
Thread Usage 14

A rough outline of the code for Fig. 2-8. (a) Dispatcher thread. (b)
Worker thread.
Operating Systems
The Classical Thread Model (Linux Thread 15
Model)
▪ The process model is based on two independent concepts:
resource grouping and execution.
o A process has an address space containing program text and data, as well as other resources. These
resources may include open files, child processes, pending alarms, signal handlers, accounting
information, and more. By putting them together in the form of a process, they can be managed more
easily.
o The other concept a process has is a thread of execution, usually shortened to just thread. The thread
has a program counter that keeps track of which instruction to execute next. It has registers, which hold
its current working variables. It has a stack, which contains the execution history
o Although a thread must execute in some process, the thread and its process are different concepts and
can be treated separately.

o Processes are used to group resources together; threads are the entities
scheduled for execution on the CPU.
Operating Systems
The Classical Thread Model 16

•One process with three threads


•Three processes each with one thread
• One process running on the computer
• Three separate processes running on the computer
• Three threads within the process
• Each process has only one thread
• Threads can share resources and communicate direct
• Independent of each other
• Easier to coordinate activities and achieve parallelism
• Cannot share resources or communicate directly

Operating Systems
POSIX Threads 17

•IEEE has defined a standard for threads in IEEE standard 1003.1c.


• The threads package it defines is called Pthreads.
•The standard defines over 60 function calls.

•Here the main program loops NUMBER OF THREADS times,


creating a new thread on each iteration, after announcing its
intention. If the thread creation fails, it prints an error message and
then exits. After creating all the threads, the main program exits.
An example program using threads.
Operating Systems
Types of Thread 18

▪ Threads are implemented in following two ways −


o User Level Threads − User managed threads.

o Kernel Level Threads − Operating System managed threads acting on


kernel

Operating Systems
User Level Threads 19

▪ Thread implementation can be done through a


library, not the kernel, allowing for greater control
over thread management.
▪ Thread library handles creating, destroying, passing
messages and data, scheduling, and context
switching.
▪ User-level threads exist within a process and are
scheduled within the process's time slices.
▪ Operating system treats the entire process as a
single unit of execution.
▪ This provides flexibility, but can also lead to issues
such as poor performance due to inefficient
scheduling or difficulty in scaling to multiple
processors.
Operating Systems
Implementing Threads in User Space 20

▪ In a system where threads are implemented as a library and


managed by the runtime system:
o Each process needs its own private thread table to keep
track of the threads in that process.
o The thread table is analogous to the kernel's process table
but only stores per-thread properties, such as program
counter, stack pointer, registers, and state.
o When a thread is moved to the ready or blocked state,
information needed to restart it is stored in the thread
table.
o The thread table allows for quick and efficient switching
between threads to provide concurrency within the
A user-level threads package. process.

Operating Systems
Switching a User Level Thread 21

▪ User-level threads table is managed by a runtime system


▪ When a thread may block, runtime system handles blocking
▪ Registers stored in thread table; looks for ready thread
▪ If ready thread found, machine registers loaded with saved
values
▪ Execution resumes automatically for new thread
▪ Runtime system manages thread table, schedules threads, and
handles blocking operations
▪ Efficient context switching between threads achieved
Operating Systems
A Key Difference from Processes 22

▪ When a thread is finished running for the moment, for example,


when it calls thread yield, the code of thread yield can save the
thread’s information in the thread table itself.
▪ Furthermore, it can then call the thread scheduler to pick another
thread to run.
▪ The procedure that saves the thread’s state and the scheduler are
just local procedures, so invoking them is much more efficient than
making a kernel call.
▪ Among other issues, no trap is needed, no context switch is needed,
the memory cache need not be flushed, and so on.
▪ This makes thread scheduling very fast.
Operating Systems
Advantages and Disadvantages 23

▪ Advantages
o can be implemented on an operating system that does not support threads
o Thread switching does not require Kernel mode privileges.
o User level thread can run on any operating system.
o Scheduling can be application specific in the user level thread.
o User level threads are fast to create and manage.
▪ Disadvantages
o In a typical operating system, most system calls are blocking.
o with user-level thread packages is that if a thread starts running, no other thread
in that process will ever run unless the first thread voluntarily gives up the CPU.
o Multithreaded application cannot take advantage of multiprocessing.

Operating Systems
Implementing Threads in Kernel Space 24

▪ Kernel level threads are managed by the operating system


kernel itself.
▪ Kernel is responsible for creating, scheduling, and
managing threads in a process.
▪ Applications don't have to worry about managing threads
as all thread management is done by the kernel.
▪ Scheduling by the kernel is done on a thread basis, based
on priority and other factors.
▪ The kernel maintains context information for the process as
a whole and for individual threads within the process.
▪ Kernel level threads can take advantage of
multiprocessing but can be slower to create and manage
A threads package compared to user-level threads.
managed by the kernel.
Operating Systems
Advantages and Disadvantages 25

▪ Advantages
o Kernel can simultaneously schedule multiple threads from the same process
on multiple processes.
o If one thread in a process is blocked, the Kernel can schedule another thread
of the same process.
o Kernel routines themselves can be multithreaded.
▪ Disadvantages
o Kernel threads are generally slower to create and manage than the user
threads.
o Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.

Operating Systems
Hybrid Implementations 26

• Hybrid thread implementation


combines user-level and kernel-level
threads to take advantage of their
respective benefits.
• Multiple user-level threads are
mapped to a single kernel-level
thread, which can then be scheduled
by the kernel.
• The kernel-level thread is responsible
for blocking and unblocking the entire
group of user-level threads.
• This approach provides the flexibility
and speed of user-level threads while
also allowing for the benefits of kernel-
level threads.
• Hybrid thread implementation can be
found in some operating systems, such
Multiplexing user-level threads as Windows, Linux, and Solaris.
onto kernel-level threads.
Operating Systems Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Pop-Up Threads 27

• The latency between message arrival and the start of processing can be
made very short.

Creation of a new thread when a message arrives.


(a) Before the message arrives. (b) After the message arrives.
Operating Systems
Advantages of Thread 28

▪ Threads minimize the context switching time.


▪ Use of threads provides concurrency within a process.
▪ Efficient communication.
▪ It is more economical to create and context switch threads.
▪ Threads allow utilization of multiprocessor architectures to a
greater scale and efficiency.

Operating Systems

You might also like