Title: CS 630 Modern Operating Systems
1CS 630Modern Operating Systems
- (This material covers Chapters 1-3. Use these
slides to reinforce your readings.)
2What is an OS?
- A program that controls the execution of
application programs. - An interface between applications and HW.
- It hides all the complexities of the computer
hardware, system program and applications. - Gives the user a virtual machine that is
(hopefully) easy to use.
3Functions of an OS
- There are two key functions of an OS
- Resource Management
- User Friendliness
4Functions of an OS (cont)
- Resource Management
- Time management temporal properties
- CPU and disk transfer scheduling
- Space management
- Main and secondary storage allocation
- Synchronization and deadlock handling
- IPC, critical section and coordination
- Accounting and status information
5Functions of an OS (cont)
- User Environment. The OS layer changes bare
hardware machine into higher level abstractions. - Execution environment which includes process
management, file manipulation, interrupt
handling, I/O operations - Error detection and handling
- Security
- Fault tolerance
6Design Approaches
- An OS has to deal with the complexities of modern
systems. - Separation of Policies and Mechanisms
- Policies what should be done?
- Mechanisms how it should be done?
- Good OS separates
- Contributes to flexibility
- Change in policy doesnt necessitate change in
mechanisms - Three common approaches exist
- Layered OSs
- Kernel-based Approach
- Virtual Machine Approach
7Layered OSs
- Simplifies design, implementation and testing.
- The OS is divided into functional layers (see
Figure 4.10 in the textbook).
8Kernel-based Approach
- Kernel contains a collection of primitives which
are used to build the OS. - OS implements policy.
- Kernel implements mechanisms.
Hardware
Operating System
9Virtual Machine Approach
- Virtual software layer is placed over hardware.
- Gives illusion of multiple instances of hardware.
- Supports multiple instances of OS.
VM1
VM2
VM2
VM4
Virtual Machine Software
Hardware
10Types of OSs
- OSs can be divided into the following five
categories - General Purpose
- Distributed Operating Systems
- Multiprocessor Operating Systems
- Database Operating Systems
- Real-time Operating Systems
11General Purpose OSs
- We will spend most of the course studying what is
classically known as a general purpose OS. - Typified by desktop and portable computing
systems. - Most mechanisms we cover will equally apply to
all types of operating systems. - We will also look at policies and mechanisms
commonly attributed to distributed and real-time
operating systems.
12Distributed (aka Network) OSs
- These systems control and manage resources for a
computer network. - Manages both HW and SW resources
- Acts like a single monolithic system.
- User unaware of program or resource locations
- Design issues same as traditional systems.
- Practical issues Lack of shared memory, lack of
global clock, unpredictable communication/network
delays.
13Multiprocessor OSs
- Consists of a set of processors that
- Share a set of physical memory blocks.
- Share a common clock.
- Share over a network.
- Control and manage resources
- Hardware and software resources
- Viewed as a uniprocessor system.
- Design issues same as traditional system.
- Practical issues increased complexity of
synchronization, scheduling, memory management
and security.
14Database OSs
- Database systems place increased demands on an OS
to efficiently support - Concept of a transaction.
- Manage large volumes of data.
- Concurrency control
- System failure control.
15Real-time OSs
- Places application specific requirements onto an
OS. - Policies and mechanisms are geared to ensuring
jobs meet their deadlines. - Problem is one of resource scheduling and overall
system utilization.
16Basic Elements
- Four main structural elements exist
- Processor, which controls the operation of the
computer and performs data processing. - Memory, which stores data and programs, typically
volatile (aka real or primary memory) - I/O modules, which moves data between the
computer and peripherals (aka secondary memory
devices). - System bus, which provides the communications
among processors, memory and I/O. (for an
illustration of this interaction, see Figure 1.1
in the textbook)
17Control and Status Registers
- Used by processor to control operations of
processor. - Used by OS routines to control program execution.
- Program Counter (PC)
- Contains the address of an instruction to be
fetched. - Instruction Register (IR)
- Contains the instruction most recently fetched.
- Program Status Word (PSW)
- Condition codes
- Interrupt enable/disable
- Supervisor/user mode
18Instruction Cycle
- Processor fetches instruction from memory.
- PC contains address of next instruction to be
fetched. - PC incremented after each fetch
Fetch Cycle Execute Cycle
Fetch Next Instruction
Fetch Next Instruction
Halt
Start
Basic instruction cycle
19Modern Instruction Cycle
Execute unit
- (a)
(b) - A three-stage pipeline
- A superscalar CPU
Fetch unit
Decode unit
Fetch unit
Decode unit
Execute unit
Holding buffer
Execute unit
Fetch unit
Decode unit
Execute unit
20Typical Memory Hierarchy
- Approx. access time
- 1 nsec
- 2 nsec
- 10 nsec
- 10 msec
- 100 sec
Approx.capacity
lt1 KB
Registers
Cache
1 MB
Main memory
64-512 MB
Magnetic disk
5-50 GB
Magnetic tape
20-100 GB
As you go down the hierarchy, per bit drops,
capacity grows, access time increases and the
frequency of access of the memory by the
processor decreases.
21Disk Cache
- A portion of main memory used as a buffer to
temporarily hold data for the disk. - Disk writes are clustered.
- Some data written may be referenced again.
- The data is retrieved rapidly from SW cache
instead of slowly from disk.
22Cache Memory
- Invisible to OS.
- Increases speed of memory.
- Processor speed is faster than memory speed.
23Cache Memory (cont)
CPU
Cache
Word Transfer
Block Transfer
24Cache Design
- Write policy
- Keeping cache and main memory consistent
- Mapping function
- Determines which cache location block occupies
- Replacement algorithm
- Determines which block to replace (LRU)
- Block size
- Data unit exchanged between cache and main memory
25I/O Techniques
- Programmed I/O
- Processor does all the work. Poll for results.
- Interrupt Driven I/O
- Device tells CPU when I/O operation is complete.
- Direct Memory Access (DMA)
- DMA controller performs Programmed I/O, not
CPU. - CPU told when DMA is complete.
26I/O Operations and Interrupts
- Processes have single address space
- May consist of multiple threads
- Threads are lightweight and execute under or
within a process - Multithreaded programming allows for concurrency
- Limited amount of information saved between
context switches
27I/O Operations and Interrupts
- One of the four classes of interrupts.
- The diagram below shows the steps in starting an
I/O and receiving an interrupt.
CPU
Interrupt handler
Disk drive
Disk controller
3
2
4
1
28I/O Operations and Interrupts (cont)
- This diagram shows how the CPU is interrupted.
Current instruction
Next instruction
3. Return
1. Interrupt
2. Dispatch to handler
Interrupt handler
29Instruction Cycle with Interrupts
Interrupt cycle
Interrupts disabled
Fitch cycle
Execute cycle
Fetch next instruction
Execute instruction
Check for Process interrupt
Start
Interrupts enabled
Halt
30Processes
- A process is a program in execution process
execution must progress in sequential fashion.
Can be characterized by its trace. - A process requires resources, which are managed
by the operating system. - The OS interleaves the execution of several
processes to maximize processor utilization. - OS supports InterProcess Communication (IPC) and
user creation of processes.
31Process
Processes (cont)
32Process Traces
Processes Traces
33Running System
34Simple Two-State Model
35Queuing Diagram
Dispatch
Queue
Enter
Exit
Pause
(a) Queuing diagram
- Dispatcher
- Program that assigns the processor to one process
or another - Prevents a single process from monopolizing
processor time
365 Process States
- As a process executes, it changes state
- New The process is being created.
- Running Instructions are being executed.
- Waiting or blocked The process is waiting for
some event to occur. - Ready The process is waiting to be assigned to
a process. - Terminate or Exit The process has finished
execution.
37Diagram of Process State
38Example for 3 Processes
39Suspending Processes
40Two Suspended States
Diagram of Process State
41OS Control Structures
Memory Tables
Process Image
Memory
I/O Tables
User data User program System stack PCB
I/O
File
File Tables
Processes
Primary Table
Process 1
Process 2
Process N
42Process Control Block (PCB)
- Information associated with each process.
- Process state
- Program counter
- CPU registers
- CPU scheduling information
- Memory-management information
- Accounting information
- I/O status information
43Process Scheduling Queues
- Job queue set of all processes in the system.
- Ready queue set of all processes residing in
main memory, ready and waiting to execute. - Device queues set of processes waiting for an
I/O device. - Process migration between the various queues.
44Ready Queue, I/O Device Queues
45Process Scheduling
46Schedulers
- Long-term scheduler job scheduler
- selects which processes should be brought into
the ready queue. - invoked infrequently (seconds, minutes)
- controls the degree of multiprogramming
- Medium-term scheduler
- allocates memory for process.
- invoked periodically or as needed.
- Short-term scheduler CPU scheduler
- selects which process should be executed next and
allocates CPU. - invoked frequently (ms)
47CPU Context Switch
48Context Switch
- When CPU switches to another process, the system
must save the state of the old process and load
the saved state for the new process. - Context-switch time is overhead the system does
no useful work while switching. - Time dependent on hardware support.
49Process Creation
- Parents create children results in a tree of
processes. - Resource sharing
- Parent and children share all resources.
- Children share subset of parents resources.
- Parent and child share no resources.
- Execution
- Parent and children execute concurrently.
- Parent waits until children terminate.
50Process Creation (Cont.)
- Address space
- Child duplicate of parent.
- Child has a program loaded into it.
- UNIX examples
- fork system call creates new process
- execve system call used after a fork to replace
the process memory space with a new program.
51Process Termination
- Process executes last statement (exit).
- Output data from child to parent (via wait).
- resources deallocated by operating system.
- Parent may terminate execution of children
processes (abort). - Child has exceeded allocated resources.
- Task assigned to child is no longer required.
- Parent is exiting.
- Operating system does not allow child to continue
- Cascading termination.
52Cooperating Processes
- Independent process cannot affect or be affected
by the execution of another process. - Cooperating process can affect or be affected by
the execution of another process - Advantages of process cooperation
- Information sharing
- Computation speed-up
- Modularity
- Convenience
53Typical UNIX System
54A Traditional UNIX Kernel
execution environment
application
trap
libraries
user
System call interface
kernel
System Services
File subsystem
dispatcher
IPC
Process control subsystem
Buffer cache
Scheduler
Interrupt
Exceptions
Memory
hardware
55Basic Concepts and Terminology
- Two privilege levels
- user and system mode
- kernel space protected requires special
instruction sequence to change from user to
kernel mode - Process has "protected" address space
- all process share same kernel space
- per process state (u_area) in kernel
- per process kernel stack
- kernel is re-entrant
- system versus process context
56Mode, Space and Context
Privileged
user
kernel
mode
context
process
Application (user code)
System calls Exceptions
kernel
X not allowed
Interrupts, System tasks
system space
57The UNIX Kernel
- loaded at boot time and initializes system,
- creates initial system processes.
- remains in memory and manages the system
- Resource manager/mediator -
- process is key abstraction
- Time share (time-slice) the CPU,
- coordinate access to peripherals,
- manage virtual memory.
- Performs privileged operations.
- provides synchronization primitives.
- Well defined entry points
- syscall, exceptions or interrupts.
58Entry into the Kernel
- Synchronous - kernel performs work on behalf of
the process - System call interface (UNIX API) central
component of the UNIX API - Hardware exceptions - unusual action of process
- Asynchronous - kernel performs tasks that are
possibly unrelated to current process. - Hardware interrupts - devices assert hardware
interrupt mechanism to notify kernel of events
which require attention (I/O completion, status
change, real-time clock etc) - System processes - scheduled by OS
- swapper and pagedaemon.
59Trap or Interrupt Processing
- Hardware switches to kernel mode, using the
per/process kernel stack. - HW saves PC, Status word and possibly other state
on kernel stack. - Assembly routine saves any other information
necessary and dispatches event. - On completion a return from interrupt (RFI)
instruction is performed.
60Interrupt handling - some details
- Multiple interrupt priority levels (ipl),
traditionally 0-7. - User processes and kernel operate at the lowest
ipl level. - Higher level Interrupts can preempt lower
priority ones. - The current ipl level may be set while executing
critical code in order to block (or mask) higher
priority interrupts.
61Exception handling
- Synchronous to the currently running process.
- Must run in the current processes context.
- Interrupt may occur during the processing of an
exception or trap.
62Software Interrupts
- Interrupts typically have the highest priority in
a system. - Software interrupts are assigned priorities above
that of user processes but below that of
interrupts. - Software interrupts are typically implemented in
software. - Examples are callout queue processing and network
packet processing.
63System Call Interface
- System call API
- Why do we need one?
- Implemented as a set of assembly language stubs
- Trap into kernel dispatch routine which invokes a
high-level system call. - User privileges are verified and any data is
copied into kernel (copyin()). - On return, copy out any user data (copyout()),
check for an Asynchronous System Trap (signal,
preemption, etc).
64Traditional UNIX Synchronization
- Kernel is re-entrant.
- only one thread/processes active at any given
time (others are blocked). - Non-preemptive.
- Blocking operations
- Masking interrupts
65Blocking Operations
- When resource is unavailable (possibly locked),
process sets flag and calls sleep() - sleep places process on a blocked queue, sets
state to asleep and calls swtch() - when resource released wakeup() is called
- all sleeping processes are woken and state set to
runnable (placed on the runnable queues). - When running, process must verify resource is
available.
66Process Scheduling
- Preemptive round-robin scheduling
- fixed time quantum
- priority is adjusted by nice value and usage
factor. - Processes in the kernel are assigned a kernel
priority (sleep priority) which is higher than
user priorities. - Kernel 0-49, user 50-127.
67Signals
- Asynchronous events and exceptions
- Signal generation using the kill() system call
- Default operation or user specific handlers
- sets a bit in the pending signals mask in the
proc structure - All signals are handled before return to normal
processing - System calls can be restarted
68The Process
- Fundamental abstraction
- Executes a sequence of instructions
- Competes for resources
- Address space - memory locations accessible to
process - virtual address space memory, disk, swap or
remote devices - System provides features of a virtual machine
- shared resources (I/O, CPU etc)
- dedicated registers and memory
69The UNIX Process
- Most created by fork or vfork
- well defined hierarchy one parent and zero or
more child processes. The init process is at the
root of this tree - different programs may be run during the life of
a process by calling exec - processes typically terminate by calling exit
70UNIX Process States
fork
system call, interrupt
return
fork
swtch()
exit
swtch()
wait
sleep()
continue
stop
wakeup
stop
stop
continue
wakeup
71Simpler State Diagram
72Process Context
- Address Space
- text, data, stack, shared memory ...
- Control information (u area, proc)
- u area, proc structure, maps
- kernel stack
- Address translation maps
- Credentials
- user and group ids
- Environment variables
- variablevalue
- typically stored at bottom of stack
73Process Context (cont)
- Hardware context
- program counter
- stack pointer
- processor status word
- memory management registers
- FPU registers
- Machine registers saved in u area's process
control block (PCB) during context switch to
another process
74Process Address Space
0xffffffff
Kernel address space
0x7fffffff
75Big Picture
The Big Picture
kernel memory
Kernel stack/u area
Kernel stack/u area
Kernel stack/u area
Data
Data
Data
Text (shared)
Text (shared)
Text (shared)
76Control Information
- U area.
- Part of user space (above stack).
- typically mapped to a fixed address.
- contains info needed with running.
- Can be swapped
- Proc
- contains info needed when not running.
- Not swapped out.
- traditionally fixed size table
77U area and Proc structures
- U Area
- PCB - HW context
- pointer to proc
- real/effective ids
- args, return values or errors to current syscall.
- Signal info
- file descriptor table
- controlling terminal vnode
- Proc structure
- process ID and group
- u area pointer
- process state
- queue pointers - scheduler, sleep, etc.
- Priority
- memory management info
- flags
78User Credentials
- Every user is assigned a unique user id (uid) and
group id (gid). - Superuser (root) has uid 0 and gid 0
- every process both a real and effective pair of
Ids. - effective id gt file creation and access,
- real id gt sending signals.
- Senders real/effective receivers real
79Process creation
- fork()
- create a new process
- copy of parents virtual memory
- parent return value is childs PID
- child return value is 0
- exec()
- overwrite with new program
80Process Termination
- exit() is called
- closes open files
- releases other resources
- saves resource usage statistics and exit status
in proc structure - wakeup parent
- calls swtch
- Process is in zombie state
- parent collects, via wait(), exit status and usage