Week 4
Week 4
THREADS
Operating Systems
A Hypothetical Video Player 2
Operating Systems
Solution 1 – Single Process 3
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
Operating Systems
Example: Multithreaded Server Architecture 7
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
▪ 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
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
Operating Systems
POSIX Threads 17
Operating Systems
User Level Threads 19
Operating Systems
Switching a User Level Thread 21
▪ 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
▪ 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
• The latency between message arrival and the start of processing can be
made very short.
Operating Systems