Upload
phungdien
View
218
Download
4
Embed Size (px)
Citation preview
Processes & Threads
CSC501 Operating Systems Principles
1
Survey:
Tell me your experience with the Lab facility and Xinu!
2
Last Lecture
q OS structures
Question:
What are those OS structures?
3
Comparison
Performance Extensibility Reliability
Monolithic
Layered
Microkernel
Best WorstIn between
4
Outline
q BackgroundQ Von Neumann Model (e.g., Memory & CPU)Q Execution Mode
q Processesq Threads
5
Introduction: Von Neumann Model
q Memory contains both text (program) and dataq CPU executes several stages:Q Fetch instructionQ Decode instructionQ Execute instructionQ Write back result(s)
Memory
CPU
§ OS is just a program§ OS text and data reside in memory too§ It invokes various functionalities through procedure calls
Execution Mode
q Kernel mode -- privilegedQ Run OS kernel codeQ Access hardware resourcesQ Protected from interference by user programs
q User mode – non-privilegedQ Run user programs
Question: Can we move certain OS
functionality to user-mode?
7
User Mode
q In user-mode, a process can directly access onlyQ Its user-space virtual memoryQ Processor resources (non-privileged registers)
q All other resourcesQ Can only be accessed through the kernelQ Via various system call interfaces
8
System Call
q It is a call because Q It looks like a procedure call
q It's a system call becauseQ It is a software trap
Question: Why is a system call a trap instead
of a procedure call?
9
Mode Switching
q Processor Status Word (PSW):Q Certain bit indicates the current mode of executionQ What are other bits?v Interrupt enabled/disabled flags, alignment check bit,
arithmetic overflow bit, parity bit, carry bit, etc.q Mode bits can be changed explicitly while in
kernel-mode but not in user-modeQ Setting of kernel mode bit in the interrupt vector
allows interrupt and trap handlers to be "automatically" executed in kernel mode
10
System Call In Monolithic OS
kernel mode
user mode
read(....)
PC PSW
code for read system call
trap
interrupt vector for trap instruction
iret
system call routine
Process
A process is a system abstraction:illusion of being the only job in the system
12
Process
q Multiple running processes
q Each running process observes an abstraction of processorQ Known only to operating
systemQ Not known by hardware
13
Processq An "instantiation" of a programq OS abstraction
Q Unknown to hardwareQ Created dynamically
q Pertinent information kept by OSQ Saved in a data structure called process control
block (PCB)
14
Process Control Block (PCB)
q For each process, process control block includes Q Identification information:v Process ID, parent process ID, user ID
Q Control information:v Scheduling (state, priority)v Resources (memory, opened files)v IPC facilities used
Q An address spacev Code, data, and stack segments
Q Execution contexts – threadsv machine state, thread execution stack
15
Process Address Space
OS
CodeGlobals
Heap
Stack
A's activationframe
B
C
A:...B()...
B:...C()...
C:
0x00…00
0xFF…FF
Is Linux designed differently16
Virtual Memory
q Process addresses are virtual memory addresses
q Mapping from virtual memory to physical memoryQ Details in later lecturesQ Translation: v Translation Look-aside Buffer (TLB)v Page table
q Will cover more later ...
17
Example PCB in XINU
18
Thread
A thread is a processor abstraction:illusion of having one processor per
execution context
19
Threadq Multiple execution contexts à threadsq All the threads of a process share the same address
space and the same resourcesq Each thread contains àQ An execution state: running, ready, etc..Q An execution context: PC, SP, other registersQ A per-thread stack
20
Single-threaded vs. Multithreaded
21
Process Address Space Revisited
OS
Code
Globals
Heap
Stack
OS
Code
GlobalsHeap
Stack
Stack
(a) Single-threaded address space (b) Multi-threaded address space
22
Threads vs. Processesq Why multiple threads? àQ Can't we use multiple processes instead?v Sure, but we need shared memory/other resources
between multiple processes Q Operations on threads are cheaper than the
corresponding operations on processesv Thread operations do not involve manipulations of
other resources associated with processesv Inter-thread communication through shared memory
without kernel intervention
23
Thread Implementation
q Kernel-level threads (lightweight processes)Q Kernel sees multiple execution contextsQ Thread management done by the kernel
q User-level threadsQ Implemented as a thread library which contains
the code for thread creation, termination, scheduling and switching
Q Kernel sees one execution context and is unaware of thread activity
Q Can be pre-emptive or not
24
User-Level vs. Kernel-Level Threads
q Advantages of user-level threads àQ Performance: low-cost thread operations and context
switching, since it is not necessary to go into the kernelQ Flexibility: scheduling can be application-specificQ Portability: user-level thread library easy to port
q Disadvantages of user-level threads àQ If a user-level thread is blocked in the kernel, the entire
process (all threads of that process) are blockedQ Cannot take advantage of multiprocessing (the kernel
assigns one process to only one processor)
25
User-Level vs. Kernel-Level Threads
process
processor
user-levelthreads
threadscheduling
processscheduling
kernel-levelthreads
threadscheduling
kernel
user
processor
threads
threads
processscheduling
User-Level vs. Kernel-Level Threads
q No reason why we shouldn't have both (e.g. Solaris)
q Most systems now support kernel threads
q User-level threads are available as linkable libraries
kernel-levelthreads
processor
user-levelthreads
threadscheduling
threadscheduling
kernel
user
processscheduling
Solaris Threads: hybrid
Thread/Process Operation Latencies
1,84044137Signal-wait
11,30094834Null fork
Processes (µs)
Kernel Threads
(µs)
User-level Threads
(µs)
Operation
VAX uniprocessor running UNIX-like OS, 1992.
Threads vs. Processes
q Why NOT multiple threads? àQ Are operations on threads really cheaper than the
corresponding operations on processes?v Memory fragmentation
Q If a process is overloaded with too many threads, how about responsiveness, reliability, and security?
q Check out the Google Chrome Comic bookQ http://www.google.com/googlebooks/chrome/
30
Threads vs. Processes
Performance Responsiveness Fexibility Security
Processes
Threads
Better Worse
31
Next Lecture
q Process lifecycle and context switching
32