Post on 29-May-2022
CHAPTER 4
1Fall 2010 - DE5
Processes & ThreadsProcesses & Threads
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 1
Thread Model Concept Thread Model Concept
Single Thread Model
User address space Kernel Stack
g
User StackPCB
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5
CHAPTER 4
2Fall 2010 - DE5
Thread Model Concept Thread Model Concept
Thread Control Thread Control
Multithread ModelThread Thread
User Stack
Kernel Stack
Thread Control Block
r add
ress
spac
ePC
B
User Stack
Kernel Stack
Thread Control Block
Dr. D. M. Akbar Hussain
Department of Electronic Systems3
Kernel Stack
Use
s Kernel Stack
Thread has what ……?Thread has what ……?
Static Storage space for locals Resources, Memory Access and Privilege
State (Ready ……. Execution Stack
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 4
CHAPTER 4
3Fall 2010 - DE5
Multithreading in Multithreading in Operating SystemsOperating Systems
MS DOS:UNIX:Windows:Linux:MACH:Solaris:
Dr. D. M. Akbar Hussain
Department of Electronic Systems
Solaris:OS-2:
DE5 5
Characteristics of a Characteristics of a Thread Thread
Asynchronous processingModular programming structureSuspension of a process suspend all threadsTermination of a process terminate all threads
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 6
CHAPTER 4
4Fall 2010 - DE5
Advantages Advantages
–Less time to create a thread in a process rather than to create a new processLess time to create a thread in a process rather than to create a new process.
–In the other extreme it takes less time to terminate a thread.
–Switching between two threads of the same process is faster.
–Communication is faster between threads because they are sharing memory,normally, kernel intervention is required for two processes to communicate.
Note: Thread synchronization is required when threads of a same process are sharingdata it is basically the same level of synchronization required for processes
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 7
data,it is basically the same level of synchronization required for processes.
Thread State Tree Thread State Tree (Functionality)(Functionality)
Process
Thread
Spawn Exit/Finish
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 8
Spawn
BlockUnblock
/ s
Spawning within a Thread
CHAPTER 4
5Fall 2010 - DE5
Performance Benefits using a Performance Benefits using a ThreadThreadConsider a program makes two remote procedure calls to two different hosts:
RPC 1 RPC 2 Request
PROCESS
Server 1 Server 2
Request Request
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 9
RPC requests have to be processed sequentially in a single thread program model
Performance Benefits using a Performance Benefits using a ThreadThread
RPC 2 Request
PROCESS THREAD A
Server 1
Server 2q
PROCESS THREAD B
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 10
RPC 1 Request
RPC requests are interleaved using a multi thread program model
CHAPTER 4
6Fall 2010 - DE5
Kernel Kernel vsvs User Level User Level ThreadsThreads
User Level
User Space
Kernel Space
Threads Library
Threads
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 11
P
User Level ThreadsUser Level Threads
Thread 1 Thread 2
Process A is executing its Thread 2
Ready Executing
Blocked
Ready Executing
Blocked
Read
Thread 1 Thread 2
Process A
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 12
Ready Executing
Blocked
CHAPTER 4
7Fall 2010 - DE5
User Level ThreadsUser Level Threads
R d R dThread 1 Thread 2
Ready Executing
Blocked
Ready Executing
Blocked
Ready Executing
Process A
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 13
Blocked
Switch to another Process
User Level ThreadsUser Level Threads
R d R dThread 1 Thread 2
Ready Executing
Blocked
Ready Executing
Blocked
Ready Executing
Process A
Mean while the
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 14
Blocked
Mean while the event has occurred so it has been moved to the ready state.
CHAPTER 4
8Fall 2010 - DE5
Th d 1 Th d 2
Thread 2 needs some thing from Thread 1 so it goes to block state .Thread 1 starts
execution
Ready Executing
Blocked
Ready Executing
Blocked
Thread 1 Thread 2
Process A
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 15
Ready Executing
Blocked Again its time of execution starts
Plus & Minus Plus & Minus of User Level Threadsof User Level Threads
Less overhead as switching takes place among the user level threads.
Suitable Scheduling scheme can be made application specific.
U l l th d (ULT ) / b f i d d t f k l l l tUser level threads (ULTs) can run on any o/s because of independent of kernel level support mechanism.
One of the short coming of ULTs is that most system calls are blocking, so when ever a system call is executed by a thread, not only the executing thread but all other threads will also be blocked.
In a ULTs scheme, a multi-threaded application cannot take advantage of multi-processing, a kernel assigns one process to only one processor at a time.
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 16
Ways to over come these disadvantages, in the first place applications should be developed as multi-processes rather than multi-threaded. One can also use jacketing (which basically converts a blocking system call to non blocking system call)
CHAPTER 4
9Fall 2010 - DE5
Kernel Level ThreadsKernel Level Threads
User Level Th d
User Space
Kernel Space
Threads
Kernel Level Threads
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 17
P
Combined ApproachCombined ApproachUser Level
Threads
User SpaceKernel Space
Threads Library
Kernel Level Threads
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 18
PP
CHAPTER 4
10Fall 2010 - DE5
Plus & MinusPlus & Minus
In a pure kernel level thread scheme all thread management is performed by the kernel. API inthe application space is linked to the kernel thread facility. W2K, Linux and O/S2 areexamples of such scheme.
All threads in an application are supported within a single process.
Context information is maintained by the kernel as a whole for process and individually forthreads.
Scheduling is performed on thread basis, so the ULTs drawbacks can be solved here as kernelcan simultaneously schedule threads on different processors.
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 19
Also, if one thread of a process is blocked it can schedule another thread of the same process.The disadvantage is that transfer of control from one thread to another thread within the sameprocess requires mode switching.
Process & Thread Process & Thread EquivalenceEquivalence
System Process: Thread
UNIX 1 : 1 Each thread is a unique process having itsown address space and
resources
Windows, Solaris 1 : M Threads can be created within the
OS/2, MACH processes having address space and dynamic resource ownership
Dr. D. M. Akbar Hussain
Department of Electronic Systems
Ra (Clouds), Emerald M : 1 In this case a thread can migrate from one process to another.
TRIX M : N Combination of M:1 & 1:M
DE5 20
CHAPTER 4
11Fall 2010 - DE5
Computer System CategoriesComputer System Categories
1. SISD Single Instruction Single Data:g g
2. SIMD Single Instruction Multiple Data:
3. MISD Multiple Instruction Single Data:
4 MIMD Multiple Instruction Multiple Data:
Dr. D. M. Akbar Hussain
Department of Electronic Systems
4. MIMD Multiple Instruction Multiple Data:
DE5 21
Symmetric ProcessingSymmetric Processing
Parallel Processing
SIMD MIMD
g
Shared Memory
Tightly Coupled
Loosely CoupledDistributed
Memory
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 22
Master-Slave SMP Clusters
CHAPTER 4
12Fall 2010 - DE5
Symmetric Symmetric vsvsMaster/Slave ProcessingMaster/Slave Processing
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 23
SMP Conceptual ViewSMP Conceptual View
ProcessorProcessor
C hC h
ProcessorProcessor
C hC h
ProcessorProcessor
C hC hCacheCache CacheCache CacheCache
Main
Memory
Main
Memory
I/OI/O
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 24
MemoryMemoryI/OI/O
I/OI/O
CHAPTER 4
13Fall 2010 - DE5
SMP Design ConsiderationSMP Design Consideration
• Simultaneous concurrent processes or threads• Simultaneous concurrent processes or threads.
• Scheduling
• Synchronization
• Memory Management
Reliabilit & Fa lt Tolerance
Dr. D. M. Akbar Hussain
Department of Electronic Systems
• Reliability & Fault Tolerance
DE5 25
MicroMicro--kernelskernels
Question: How to qualify, meaning how small the core shouldbe to qualify for a micro kernel.
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 26
CHAPTER 4
14Fall 2010 - DE5
Kernel ArchitecturesKernel Architectures
ss ryry rsrs rr
Process ManagementProcess Management
Virtual MemoryVirtual Memory
I/O ManagementI/O Management
Inter Process Com.Inter Process Com.
File SystemFile System
ApplicationsApplications
Clie
nt P
roce
ssC
lient
Pro
cess
Virt
ual M
emor
Virt
ual M
emor
Dev
ice
Driv
erD
evic
e D
river
File
Ser
ver
File
Ser
ver
Proc
ess S
erve
Proc
ess S
erve
Micro KernelMicro Kernel
Ker
nel
Mod
e
Kernel Mode
Use
r M
odeUser Mode
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 27
HardwareHardware HardwareHardware
Microkernel Advantages Microkernel Advantages
Uniform InterfaceUniform Interface
Extensibility
Flexibility
Portability
Reliability
Distributed System Support
Dr. D. M. Akbar Hussain
Department of Electronic Systems
y pp
Object Oriented Operating System (OOOS)
DE5 28
CHAPTER 4
15Fall 2010 - DE5
Windows 2000 Thread & Windows 2000 Thread & SMPSMP
Processes implemented on different operating systems maydiffer in many ways ?differ in many ways ?
• Characteristics ofW2K Processes
Processes are implemented as objects
Process may have one or more threads
P d th d h b ilt i bilit f h i ti
Dr. D. M. Akbar Hussain
Department of Electronic Systems
Processes and threads have built in capability for synchronization
DE5 29
W2K Process & ResourcesW2K Process & Resources
Access Token
Object Table
Handle 1
Process
Thread x
Virtual Address Space
Available Objects
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 30
Handle 2
Handle 3
File y
Section z
CHAPTER 4
16Fall 2010 - DE5
Windows 2000 Process & Thread ObjectsWindows 2000 Process & Thread Objects
ibut
es Process ID
Security Descriptor
Base Priority
Process
bute
s Process ID
Thread Context
Dynamic Priority
Threades
Obj
ect B
ody
Attr Default Processor affinity
Quota Limits
Execution Time
I/O Counters
VM Operational Counters
Exception Debugging Port
Exit Status
Create Process
Open Process
Query Process Information
Set Process Information
C P sO
bjec
t Bod
y A
ttrib
Base Priority
Thread Processor affinity
Thread Execution Time
Alert Status
Suspension Count
Impersonation Token
Termination Port
Thread Exit Status
Create Thread
Open Thread
Query Thread Information
S Th d I f i
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 31
Serv
ice Current Process
Terminate Process
Allocate Virtual Memory
Read/Write Virtual Memory
Protect Virtual Memory
Lock/Unlock virtual Memory
Query Virtual Memory
Flush Virtual Memory
Serv
ices Set Thread Information
Current Thread
Terminate Thread
Get Context
Set Context
Resume
Alert Thread
Test Thread Alert
Register Termination Port
W2K Processes StatesW2K Processes States
Standby Executing Terminated
Initialized
Ready Waiting
Initialize
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 32
TransitionPaged Out
CHAPTER 4
17Fall 2010 - DE5
W2K Thread StatesW2K Thread StatesRunable
Pick to Run SwitchStandby
Resource Unblock (Resource Available) Block Terminate
Available
PreemptedReady Executing
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 33
Available
Unblock (Resource Not Available)
Not Runable
TransitionWaiting
Terminated
LINUX Process / Thread LINUX Process / Thread ModelModel
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 34
Hardware Condition
I/O Operation, Signal from another Process or Resource not available
CHAPTER 4
18Fall 2010 - DE5
SOLARIS Thread StatesSOLARIS Thread States
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 35
Process & Threads in Process & Threads in SOLARISSOLARIS
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 36
CHAPTER 4
19Fall 2010 - DE5
Solaris MultiSolaris Multi--threaded threaded ArchitectureArchitecture
Process 1 Process 2 Process 3 Process 4
Kernel Space
User Space
LWP LWPLWPLWPLWPLWPLWPLWP
Threads LibraryThreads LibraryThreads LibraryThreads Library
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 37
P
Kernel Level Threads
P P P P
Process Image for Process Image for referencereference
Dr. D. M. Akbar Hussain
Department of Electronic SystemsDE5 38