![Page 1: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/1.jpg)
Processes & Threads
CSC501 Operating Systems Principles
1
![Page 2: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/2.jpg)
Survey:
Tell me your experience with the Lab facility and Xinu!
2
![Page 3: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/3.jpg)
Last Lecture
q OS structures
Question:
What are those OS structures?
3
![Page 4: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/4.jpg)
Comparison
Performance Extensibility Reliability
Monolithic
Layered
Microkernel
Best WorstIn between
4
![Page 5: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/5.jpg)
Outline
q BackgroundQ Von Neumann Model (e.g., Memory & CPU)Q Execution Mode
q Processesq Threads
5
![Page 6: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/6.jpg)
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
![Page 7: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/7.jpg)
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
![Page 8: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/8.jpg)
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
![Page 9: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/9.jpg)
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
![Page 10: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/10.jpg)
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
![Page 11: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/11.jpg)
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
![Page 12: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/12.jpg)
Process
A process is a system abstraction:illusion of being the only job in the system
12
![Page 13: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/13.jpg)
Process
q Multiple running processes
q Each running process observes an abstraction of processorQ Known only to operating
systemQ Not known by hardware
13
![Page 14: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/14.jpg)
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
![Page 15: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/15.jpg)
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
![Page 16: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/16.jpg)
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
![Page 17: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/17.jpg)
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
![Page 18: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/18.jpg)
Example PCB in XINU
18
![Page 19: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/19.jpg)
Thread
A thread is a processor abstraction:illusion of having one processor per
execution context
19
![Page 20: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/20.jpg)
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
![Page 21: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/21.jpg)
Single-threaded vs. Multithreaded
21
![Page 22: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/22.jpg)
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
![Page 23: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/23.jpg)
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
![Page 24: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/24.jpg)
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
![Page 25: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/25.jpg)
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
![Page 26: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/26.jpg)
User-Level vs. Kernel-Level Threads
process
processor
user-levelthreads
threadscheduling
processscheduling
kernel-levelthreads
threadscheduling
kernel
user
processor
threads
threads
processscheduling
![Page 27: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/27.jpg)
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
![Page 28: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/28.jpg)
Solaris Threads: hybrid
![Page 29: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/29.jpg)
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.
![Page 30: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/30.jpg)
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
![Page 31: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/31.jpg)
Threads vs. Processes
Performance Responsiveness Fexibility Security
Processes
Threads
Better Worse
31
![Page 32: lec3-process1 - Department of Computer Science at … Mode qIn user-mode, a process can directly access only QIts user-space virtual memory QProcessor resources (non-privileged registers)](https://reader031.vdocuments.mx/reader031/viewer/2022030503/5aafd9947f8b9a25088dff51/html5/thumbnails/32.jpg)
Next Lecture
q Process lifecycle and context switching
32