Operating System Review
- resource management on computers
- scheduling: managing the CPU time
- processes and threads, inter-process communication, deadlock
- protection: managing access to memory, disk, other devices
- kernel mode and kernel entry points: system calls, context switches,
interrupts, delayed processing
- input/output: managing devices
- virtual memory: managing physical memory and backups to disk (overlays)
- disks and file systems: managing persistent storage, caching
Operating Systems as Resource Managers for Computers
- lots of resources on a computer: installed software, configuration,
data, ability to access a network, hardware including CPU and memory
- managing the resources means using them in controlled ways
for maximum benefit
- for example, resources should be protected against accidents and
against malicious programs (malware)
- also, limited resources must be shared among different uses:
real-time programs, supervisory programs, user applications, responding
to outside devices such as networks
Scheduling
- the resource managed in scheduling is the CPU time
- ``slices'' of CPU time are assigned to each thread
- a thread loses its time slice and stops executing when:
- the timeslice ends, or
- the thread blocks, e.g. waiting for I/O, synchronization, or a timer, or
- an interrupt causes the scheduling of a higher priority thread
- at such time, the scheduler must choose the next thread to
execute, usually based on priority
- long timeslices give high throughput for CPU-intensive processes,
short time slices give better response time
- Linux schedulers; give higher priority to I/O intensive
processes where response time is more important
- a thread join the ready list when:
- the thread is rescheduled for a new timeslice, or
- the thread is awakened after blocking, e.g. after an I/O completes
Processes, Threads, Contexts, IPC, deadlock
- a thread is simply the execution of a program
- a process is associated with all the resources used by one or more
threads (processes with zero threads are terminated), especially virtual
memory
- the registers of the CPU and MMU hold the context of a
computation: stack pointer, virtual memory tables
- on a thread switch, only need to save and restore
the CPU computation registers and the stack pointer
- on a process switch, also need to save and restore
the MMU state and the process descriptor, and may have to flush the cache
- both process switch and thread switch are referred to as
context switch, but a thread switch is much more lightweight
- thread switches can be done in user space or in kernel space,
process switches must be done in kernel space
- thread switches done in user space must face the issue of
what happens if a thread blocks -- does the entire set of
threads block, or is there a mechanism (usually, requiring that I/O
be done through the threading system) to allow other threads to be
started?
- inter-process communication uses mutexes, semaphores, and other
mechanisms to insure at most one thread at a time is accessing a given
resource
- semaphores can also be used to safely count resources used by
different threads, e.g. the producer and the consumer
- pipes provide a simple mechanism to connect a producer and
a consumer, even in different processes
- different threads trying to acquire each other's resources
may lead to deadlock. Simplest solution for avoiding deadlock is
to always acquire multiple resources in the same order
Protection for Memory, Disks, and Devices
- most operating systems prevent one process
from affecting other processes, except in controlled ways
- to do this, different processes run in different virtual address spaces,
so a pointer error in one program cannot modify memory in another
- the kernel has access to all the memory spaces
- disk access is also usually reserved for the kernel or the root
user
- as a result, data on disk has an "owner" (UID) and "permissions"
- devices usually also are protected, e.g. to prevent most processes
from writing directly to the frame buffer for the display
Kernel Mode, Kernel Entry Points
- the kernel is the part of the system that operates in protected
mode after the boot process is complete
- leaving protected mode is relatively easy (in assembly) for the kernel
- entering protected mode is only allowed at specific kernel entry
points, specifically:
- system calls
- interrupts handlers
- if the kernel data structures are mapped to the process's virtual
memory, the context switch on entering the system call does not need to
change the MMU, otherwise, the context switch may have to do a lot of work
- delayed processing is also a kernel entry point, but usually from
inside the kernel itself
Input and Output
- the performance of many system is judged more on I/O speed than
on CPU speed
- it would be good to do high-priority I/O before low-priority I/O, but:
- that information is not usually available to the kernel
- even low-priority I/O should not starve
- I/O priority may vary dynamically, e.g. writing back dirty pages
may become more important if free memory is all allocated
- external considerations (e.g. disk geometry, packet sequencing) may
suggest I/O sequencing that does not directly reflect external priority
- optimizing disk access is important because disk access is many orders
of magnitude slower than memory access
Virtual Memory
- an address (pointer) in a program (virtual address)
does not reflect the address in memory (physical address)
- the low-order bits of the virtual address are the same as the
low-order bits of the physical address
- the high-order bits of the virtual address are translated through
an arbitrary page table maintained by the operating system, and
cached within the memory management unit (MMU)'s translation
lookaside buffer (TLB)
- one of the main jobs of the OS is to assign physical pages
to processes for their virtual pages
- another one of the main jobs of the OS is to assign physical pages
to files to cache their disk blocks
- when physical pages are in short supply, some of the virtual
memory for one or more processes may be saved to swap space
(or swap files) on disk
- this means very large processes can run on machines with small
physical memory, as long as they don't need all their address space
at the same time: the different virtual address pages form
overlays on the same physical pages
Disks and File Systems
- current practice is to store file systems on disks, though flash
is beginning to make inroads, and storing a file on a computer across
a network is well established (NFS, SMB, etc)
- the most common file access pattern is sequential, but random
access must also be supported, and most file systems also support
file ``holes'' where nothing has been written
- files are organized hierarchically (using directories or folders)
with optional cross-links or back links, including both hard links and
soft links
- opening a file is used to tell the OS that it is advisable to cache
information for the file, closing the file suggests writing back or
discarding the cache
Operating Systems
- interesting, concurrent, influential, useful programs