Threads
Processes and Threads
Types of Threads
Multicore and Multithreading
Concepts of a Process
Resource Ownership Process Execution
OS
P1 R1
P2 R2
P1 P2 P3
Resources defined in the process. States and dispatch processes.
For instance, the allocation of I/O Unit of dispatching.
devices.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Process vs. Threads
• Process
• An abstraction of a running program.
• A process also has an independent
resource.
• Threads
• refers to a subset of a process. These
are mini-processes which is conjoined
THREADS
with other mini-process to a shared
resource.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Word Processor
https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR880Vm3IZ3BBcArwnB7Zou3XuW5BX9xp-
0nvZ3OBXyX01PDN8BZw
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Word Processor
One example of the usage of threads are in a word processor program wherein this big
blue circle refers to a specific process and those three squiggly lines represent threads.
For example, we are typing in a word processor program.
• When we type characters, each character that is tracked in an Input Device (keyboard) represents
one thread.
• Each character that is shown in the monitor represents another thread.
• when there is an auto-save feature in the software a data is stored in the secondary storage device
which is represented as another thread.
There is a necessity of implementing threads nowadays. We are benefiting from it that
processes would be executing more efficient and faster with less time allotted
processing.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Process vs. Threads
R1 P1 P1-T1
P2-T3 P1
P1-T3 T1 T3
R2 P2 T2
P2-T2
PA-PB
P3
P2
R3
T1 T3
PR-PB T2
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Process vs. Threads
From the figures, P is represented as a process while T is
represented for a thread. In each given process it has its own
independent resource. It means to say that if P1 is holding
only resource intended for P1, P2 cannot get the resource
unless P1 is finished executing a process.
Form the other figure, two processes with multiple threads
are sharing over a resource. Threads are easier to be
executed and dispatched.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Key benefits of threads
It takes far less time to create a new thread in an
existing process than to create a brand-new process.
It takes less time to terminate a thread than a
process.
It takes less time to switch between two threads within
the same process than to switch between processes.
Threads enhance efficiency in communication between
different executing programs.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Two aspects of Thread Functionality
• Thread States
http://www.programering.com/images/remote/ZnJvbT1jc2RuJnVybD1RM2NoVkVhMFYzYlQ5U2UwbG1kaEozWnZBek52VW1kczkyY3psR1p2MFRQQjEwUUd0V1VDcEV
NSjlDYnNsbVp2QURNMDhTWjZsMmMwNTJibTlDVnl3VU5NWlRZMThDZHU5bVp2Z1hRVTVVTXM1bVl3Qm5NYVZuUnlRbWRSaGxXMVZUYWl0bVR6a1ZkakpqWXpwa01
NWjNiRU5HTVNoVVl2UUhlbFIzTHk4eWF5RldieVZHZGhkM1B3a3pNeE1UTXpFVE0yRVROd1FUTXdJekwwVm1idTRHWnpObUxuOUdiaTV5WnRsMkx2b0RjMFJIYQ.jpg
https://t-l.ru/i/n/037/236037/236037_72a8d651ee70a.jpg
Spawn Block
https://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Multithreade
https://www.obovsyom.ru/pic/images/post/75550-1488815648.jpg
d_process.svg/1200px-Multithreaded_process.svg.png
Finish Unblock
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Functionality
• Thread States
Spawn: When a new process is spawned, a thread for that process is also
spawned. Subsequently, a thread within a process may spawn another thread
within the same process, providing an instruction pointer and arguments for
the new thread. The new thread is provided with its own register context and
stack space and placed on the ready queue.
Block: When a thread needs to wait for an event, it will block (saving its user
registers, program counter, and stack pointers). The processor may now turn
to the execution of another ready thread in the same or a different process.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Functionality
• Thread States
Unblock: When the event for which a thread is blocked occurs, the thread is
moved to the Ready queue.
Finish: When a thread completes, its register context and stacks are
deallocated.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Functionality
• Thread Synchronization
P1-T1
P2-T3 P1
P1-T3 T1 T3
T2
P2-T2
PA-PB
P2
T1 T3
PR-PB T2
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Functionality
• Thread Synchronization
All of the threads of a process share the same address space and other
resources, such as open files.
Any alteration of a resource by one thread affects the environment of the
other threads in the same process.
It is therefore necessary to synchronize the activities of the various threads so
that they do not interfere with each other or corrupt data structures.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Design Issues on Threads
• OS Dependent https://img00.deviantart.net/22c5/i/2010/085/c/4/i_love_apple_wallpaper_by_xlivr.jpg
• System Call Blocking
• Resource Sharing
https://www.insomnia.gr/applications/core/interface/imageproxy/imageproxy.php?img=https://static.spiceworks.com/shar
ed/post/0011/1880/edge-microsoft-ram-google-chrome-browsers-
https://encrypted-
2339772.png&key=cd3eb5fadb60a62b0fc212c493f49a83f00e438bf988bef76ff493300c781554
tbn0.gstatic.com/images?q=tbn:ANd9GcT3Wz0s7PrRuK2R_BymhX_GyJoHxDb0PJRlD0tlHvGszew5eAYD9g
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Design Issues on Threads
• OS Dependent
threads rely heavily on the Operating System through dispatched processes in the
processing queue.
• System Call Blocking
One system call, if executed, can disrupt threads in execution, blocking all
possibility. E.g. interrupts.
• Resource Sharing
different threads tend to get the same resource. Higher concurrency, higher chance
of getting deadlocks.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Types of Threads
• User-Level Threads (ULT)
• Kernel-Level Threads (KLT)
• Combined Approaches (ULT/KLT)
(Stallings, 2014)
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Types of Threads
• User-Level Threads (ULT)
• Work of thread mgmt. is done by
the application
• kernel is not aware of the
existence of threads.
• using a threads library, a package
of routines.
(Stallings, 2014)
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Types of Threads
• User-Level Threads (ULT)
• Disadvantages:
• ULT executes a system call, the
threads within the process are
blocked.
• A kernel assigns one process to
only one processor at a time, only
a single thread within a process (Stallings, 2014)
can execute at a time.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Types of Threads
• Kernel-Level Threads (KLT)
• All of the work of thread mgmt. is done
by the kernel.
• No thread mgmt. code in the
application level, simply an API
• Disadvantage:
• The transfer of control requires a
mode switch to the kernel. (Stallings, 2014)
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Types of Threads
• Combined Approaches (ULT/KLT)
• Multiple threads within the same application can
run in parallel on multiple processors, and a
blocking system call need not block the entire
process.
• Same with pure ULT and KLT approaches while
minimizing the disadvantages.
(Stallings, 2014)
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Multicore
• It is the presence of a multiple cores in a single or
more processors in a given system.
• To support a single application with multiple threads
https://www.howtogeek.com/wp-content/uploads/2014/08/xcpu-on-
motherboard.jpg.pagespeed.gp+jp+jw+pj+ws+js+rj+rp+rw+ri+cp+md.ic.tL1N0E5yoS.jpg
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Processors with multiple cores
http://www.guru3d.com/miraserver/images/2009/corei5/turbo.jpg
2.6 2.6 GHz
GHz X4
https://www.howtogeek.com/wp-content/uploads/2017/06/xcpuc_2-
650x482.png.pagespeed.gp+jp+jw+pj+ws+js+rj+rp+rw+ri+cp+md.ic.AFQmpsgeyA.png
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Servers with multiple processors/core
https://lh5.googleusercontent.com/-
CGA74X7khro/TYtyYHc5unI/AAAAAAAACYk/Gb6ZhmPCRzM/s912/IMG_4212.JPG https://www.extremetech.com/wp-content/uploads/2015/01/Servers.jpg
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Multithreading
• The ability of an OS to support multiple,
concurrent paths of execution within a
single process.
T2
• Single-threaded approach / execution per
process.
T1 T3
Process
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Some examples
• Multiprocess applications:
• Characterized by the presence of many single-threaded processes.
• Java applications:
• JVM provides scheduling and memory management for Java
applications.
• Multithreaded native applications:
• Characterized by having a small number of highly threaded
processes.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Application Example: Game Software
RENDER
SKYBOX MAIN VIEW MONITOR ETC.
SCENE LIST HUD Display
OBJECTS
PARTICLES
SIM & DRAW
MODELS
BONE SETUP
DRAW
*Property of STI
ETC.
IT1806
Property of STI IT1806 Weeks 6-7
Application Example: Game Software
https://answers.unrealengine.com/storage/attachments/123883-
capture%20d%E2%80%99%C3%A9cran%20(1).png
http://i.imgur.com/Nvdgeu6.png
https://pre00.deviantart.net/ad5d/th/pre/f/2015/170/b/0/cam006_by
_giovannimanili-d8xw3f5.jpg
https://forum.unity.com/attachments/showcase2_05-jpg.116601/
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Management
• Windows
• o Supports Multithreading.
• o Threads are Interruptible. OS in
windows, Threads are easily disposable
as is. When a system call is inbound and
depending on the application, threads
are easily set aside either disposing or http://3.bp.blogspot.com/-
UBrNnZyZl1Q/Vf9YWXjN7nI/AAAAAAAAAWI/E_N19OKblrQ/
swapping.
s1600/multithreading-06212013.jpg
• o Object-Oriented - Everything Threads.
Every application in
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Management
• Linux
• No multithreading. Linux OS usually replicates
processes avoiding deadlocks in as much.
• Processes cloned for sharing. If certain threads
require the same resources, the OS duplicates
the process in order that the same thread
doesn’t overlap the other resources.
• Processes share VM – threaded as one.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Management
• Mac OS X
• Uses BLOCKS = commands
• # of blocks encapsulated in a thread
• Executed after encapsulation.
blocks
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Thread Management
• Android
• One process = many threads
• Threads easily removed
depending on hierarchy.
https://cdn-images-1.medium.com/max/1120/0*yGCYcQQsVmaT3Ukf.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
Seatwork – by pair
• Students must provide ½ crosswise.
• Name 5 apps from Windows OS Platform.
• State a specific process with that app and state its
needed resource, its input command and output.
• E.g. Microsoft Word
• Process: Opening a *.docx file
• Resources needed: Hardisk, RAM, Processor, Keyboard,
Monitor, etc.
• Input: command from double-clicking the .docx file.
• Output: Opening the file in MS-Word
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7
For the notes, please
download the handouts
in your eLMS account.
*Property of STI IT1806
Property of STI IT1806 Weeks 6-7