我的笔记地址:https://docs.google.com/document/d/19Lfkmvq_eU-KIHKhOrx4kYBOdL8sQpxWvtaod72Oaoc/edit?usp=sharing
Architectural support for Oses
Application: written by programmer, compiled by programmer, uses function calls
Libraries: written by elves, provided pre-compiled, defined in headers, input to linker, invoked like functions, maybe resolved when program is loaded
Portable OS layer: system calls (read, open, …), all high-level code
Machine-dependent layer: bootstrap, system initialization, interrupt and exception, I/O device driver, memory management, Kernel/user mode switching, processor management
-
Types of architecture support
-
Manipulating privileged machine state
-
Generating and handling events
-
Events: interrupts, exceptions, system calls, etc
-
-
Privileged instructions
-
What are privileged instructions?
-
A subset of instructions of every CPU is restricted to use only by the OS
-
Who gets to execute them? - OS
-
How does the CPU know whether they can be executed?
– When it runs the OS code. Kernel mode or user mode is indicated by a status bit in a protected control register. CPU checks mode bit when protected instruction executes. Attempts to execute in user mode are detected and prevented.
Only the operating system can
Directly access I/O devices (disks, printers, etc.)
Manipulate memory management state
Manipulate protected control registers
Halt instruction
-
-
-
Difference between user and kernal mode
-
User programs execute in user mode
-
OS executes in kernel mode
-
-
-
Why do they need to be privileged?
-
OS must be able to protect programs from each other
-
OS must protect itself from user programs
-
-
What do they manipulate?
-
Protected control registers
-
Memory management
-
OS must be able to protect programs from each other
-
OS must protect itself from user programs
-
Memory management hardware (MMU) provides memory protection mechanisms
-
Manipulating MMU uses protected (privileged) operations
-
-
I/O devices
-
-
-
Events
An event is an “unnatural” change in control flow.
Events immediately stop current execution.
Change mode, context (machine state), or both.
-
Events
-
Synchronous: fault (exceptions), system calls
-
Asynchronous: interrupts, software interrupt
-
-
What are faults, and how are they handled?
-
What are system calls, and how are they handled?
-
How do I/O devices use interrupts?
-
-
What is the difference between exceptions and interrupts?
-
Exceptions are caused by executing instructions
-
CPU requires software intervention to handle a fault or trap
-
-
Interrupts are caused by an external event: signal asynchronous events
-
Device finishes I/O, timer expires, etc.
-
-
OS modules, interfaces, and structures
Processes
-
Processes
-
What is a process?
-
The OS abstraction for execution.
-
-
What is the difference between a process and a program?
-
What is contained in a process?
-
-
Process data structures
-
Process Control Blocks (PCBs)
-
What information does it contain?
-
How is it used in a context switch?
-
-
State queues
-
What are process states?
-
What is the process state graph?
-
When does a process change state?
-
How does the OS use queues to keep track of processes?
-
-
-
Process manipulation
-
What does fork() on Unix do?
-
What does it mean for it to “return twice”?
-
-
What does exec() on Unix do?
-
How is it different from fork?
-
-
How are fork and exec used to implement shells?
-
Threads
-
Threads
-
What is a thread?
-
What is the difference between a thread and a process?
-
How are they related?
-
-
Why are threads useful?
-
What is the difference between user-level and kernel-level threads?
-
What are the advantages/disadvantages of one over another?
-
-
-
Thread implementation
-
How are threads managed by the run-time system?
-
Thread control blocks, thread queues
-
How is this different from process management?
-
-
What operations do threads support?
-
Fork, yield, sleep, etc.
-
What does thread yield do?
-
-
What is a context switch?
-
What is the difference between non-preemptive scheduling and preemptive thread scheduling?
-
Voluntary and involuntary context switches
-
-
Synchronization
-
Synchronization
-
Why do we need synchronization?
-
Coordinate access to shared data structures
-
Coordinate thread/process execution
-
-
What can happen to shared data structures if synchronization is not used?
-
Race condition
-
Corruption
-
Bank account example
-
-
When are resources shared?
-
Global variables, static objects
-
Heap objects
-
-
-
Mutual exclusion
-
What is mutual exclusion?
-
What is a critical section?
-
What guarantees do critical sections provide?
-
What are the requirements of critical sections?
-
Mutual exclusion (safety)
-
Progress (liveness)
-
Bounded waiting (no starvation: liveness)
-
Performance
-
-
-
How does mutual exclusion relate to critical sections?
-
What are the mechanisms for building critical sections?
-
Locks, semaphores, monitors, condition variables
-
-
-
Locks
-
What does acquire do?
-
What does release do?
-
What does it mean for acquire/release to be atomic?
-
How can locks be implemented?
-
Spinlocks
-
Disable/enable interrupts
-
Blocking (Nachos)
-
-
How does test-and-set/swap work?
-
What kind of lock does it implement?
-
-
What are the limitations of using spinlocks, interrupts?
-
Inefficient, interrupts turned off too long
-
-
-
Semaphores
-
What is a semaphore?
-
What does P/Decrement do?
-
What does V/Increment do?
-
How does a semaphore differ from a lock?
-
What is the difference between a binary semaphore and a counting semaphore?
-
-
When do threads block on semaphores?
-
When are they woken up again?
-
Using semaphores to solve synchronization problems
-
Readers/writers problem
-
Bounded buffers problem
-
-
-
Monitors
-
What is a monitor?
-
Shared data
-
Procedures
-
Synchronization
-
-
In what way does a monitor provide mutual exclusion?
-
To what extent is it provided?
-
-
How does a monitor differ from a semaphore?
-
How does a monitor differ from a lock?
-
What kind of support do monitors require?
-
Language, run-time support
-
-
-
Condition variables
-
What is a condition variable used for?
-
Coordinating the execution of threads
-
Not mutual exclusion
-
-
Operations
-
What are the semantics of Wait?
-
What are the semantics of Signal?
-
What are the semantics of Broadcast?
-
-
How are condition variables different from semaphores?
-
-
Implementing monitors
-
What does the implementation of a monitor look like?
-
Shared data
-
Procedures
-
A lock for mutual exclusion to procedures (w/ a queue)
-
Queues for the condition variables
-
-
What is the difference between Hoare and Mesa monitors?
-
Semantics of signal (whether the woken up waiter gets to run immediately or not)
-
What are their tradeoffs?
-
What does Java provide?
-
-
-
Locks and condition vars
-
In nachos, we don't have monitors
-
But we want to be able to use condition variables
-
So we isolate condition variables and make them independent (not associated with a monitor)
-
Instead, we have to associate them with a lock (mutex)
-
Now, to use a condition variable …
-
Threads must first acquire the lock (mutex)
-
CV::Wait releases the lock before blocking, acquires it after waking up
-
-
Scheduling
-
-
Scheduling
-
Components
-
Scheduler (dispatcher)
-
-
When does scheduling happen?
-
Job changes state (e.g., waiting to running)
-
Interrupt, exception
-
Job creation, termination
-
-
-
Scheduling goals
-
Goals
-
Maximaize CPU utilization
-
Maximize job throughput
-
Minimize ternaround time
-
Minimize waiting time
-
Minimize response time
-
-
What is the goal of a batch system?
-
What is the goal of an interactive system?
-
-
Starvation
-
Starvation
-
Indefinite denial of a resource (CPU, lock)
-
-
Causes
-
Side effect of scheduling
-
Side effect of synchronization
-
-
Operating systems try to prevent starvation
-
-
Scheduling algorithms
-
What are the properties advantages and disadvantages of the following scheduling algorithms?
-
First Come First Serve (FCFS)/First In First Out (FIFO)
-
Shortest Job first (SJF)/shortest remaining time first
-
Priorty
-
Round Robin
-
Multilevel feedback queues
-
-
What scheduling algorithm does Unix use? Why?
-
-
Some quick clarification
-
Round Robin schedule
-
If the time slice is 10ms, it means that a thread can at most use the CPU for 10 ms a time
-
If a thread gives up the CPU because it has to wait for I/O, condition variable, etc, the scheduler will switch to another thread in the ready queue
-
-
-
Deadlocks
-
Deadlock conditions
-
Deadlock detection
-
Deadlock prevention
-
How do today’s systems handle deadlocks? Why?
-
-