Overview of today’s lecture
Process definition
What are Address spaces
Components of an address space
Methods of altering control flow of a CPU
Interrupts, Traps and faults
How does a process enter into the operating system
Context switching
Introduction to process management models and
state machines
Re-cap of the lecture
Process
A program in execution
An instance of a program running on a computer
The entity that can be assigned to and executed on
a processor
A unit of activity characterized by the execution of a
sequence of instructions, a current state, and an
associated set of system instructions
Private Address Spaces
Each process has its own private address space.
0xffffffff
0xc0000000
0x40000000
0x08048000
0
kernel virtual memory
(code, data, heap, stack)
user stack
(created at runtime)
memory
invisible to
user code
%esp (stack pointer)
memory mapped region for
shared libraries
run-time heap
(managed by malloc)
read/write segment
(.data, .bss)
read-only segment
(.init, .text, .rodata)
unused
brk
loaded from the
executable file
Implementing the Process Abstraction
Pj
CPU
PPi Executable
i Executable
Memory
Memory
PPj Executable
j Executable
Memory
Memory
Pk
CPU
… PPExecutable
Executable
k
k
Memory
Memory
OS interface
CPU
ALU
Control
Unit
OS
OS Address
Address
Space
Space
PPi Address
i Address
Space
Space
PPk Address
k Address
Space
Space
…
P Address
Pj j Address
Space
Space
Machine Executable Memory
Pi
CPU
The Address Space
Address
Space
Process
Process
Address
Binding
Executable
Memory
Files
Other objects
Execution of the Operating System
Non-process Kernel
Execute kernel outside of any process
Operating system code is executed as a separate entity
that operates in privileged mode
Execution Within User Processes
Operating system software within context of a user process
Process executes in privileged mode when executing
operating system code
Execution of the Operating System
Process-Based Operating System
Implement operating system as a collection of
system processes
Useful in multi-processor or multi-computer
environment
Control Flow
Computers do Only One Thing
From startup to shutdown, a CPU simply reads and executes (interprets) a
sequence of instructions, one at a time.
This sequence is the system’s physical control flow (or flow of control).
Physical control flow
Time
<startup>
inst1
inst2
inst3
…
instn
<shutdown>
Altering the Control Flow
Two basic mechanisms available to the programmer for
changing control flow:
Jumps and branches
Function call and return using the stack discipline.
Both react to changes in program state.
Insufficient for a useful system
Difficult for the CPU to react to changes in system state.
data arrives from a disk or a network adapter.
Instruction divides by zero
User hits ctl-c at the keyboard
System timer expires
System needs mechanisms for “exceptional control flow”
Exceptional Control Flow
Mechanisms for exceptional control flow exists at all levels of a
computer system.
Low level Mechanism
exceptions
change in control flow in response to a system event (i.e.,change in
system state)
Combination of hardware and OS software
Higher Level Mechanisms
Process context switch
Signals
Nonlocal jumps (setjmp/longjmp)
Implemented by either:
OS software (context switch and signals).
C language runtime library: nonlocal jumps.
System context for exceptions
Keyboard
Keyboard
Processor
Processor
Interrupt
Interrupt
controller
controller
Mouse
Mouse Modem
Modem
Keyboard
Keyboard
controller
controller
Serial
Serialport
port
controller
controller
Printer
Printer
Parallel
Parallelport
port
controller
controller
Local/IO
Local/IOBus
Bus
Memory
Memory
IDE
IDEdisk
disk
controller
controller
disk
SCSI
SCSI
controller
controller
SCSI
SCSIbus
bus
disk
CDROM
Video
Video
adapter
adapter
Network
Network
adapter
adapter
Display
Display
Network
Network
Exception
s
An exception is a transfer of control to the OS in
response to some event (i.e., change in processor
state)
User Process
OS
event
current
next
exception
exception processing
by exception handler
exception
return (optional)
Asynchronous Exceptions (Interrupts)
Caused by events external to the processor
Indicated by setting the processor’s interrupt pin
handler returns to “next” instruction.
Examples:
I/O interrupts
hitting ctl-c at the keyboard
arrival of a packet from a network
arrival of a data sector from a disk
Hard reset interrupt
hitting the reset button
Soft reset interrupt
hitting ctl-alt-delete on a PC
Interrupt Vectors
Exception
numbers
interrupt
vector
0
1
2
n-1
...
code
codefor
for
exception
exceptionhandler
handler00
code
codefor
for
exception
handler
exception handler11
code
codefor
for
exception
exceptionhandler
handler22
...
code
codefor
for
exception
handler
exception handlern-1
n-1
Each type of event has a
unique exception number k
Index into jump table
(a.k.a., interrupt vector)
Jump table entry k points
to a function (exception
handler).
Handler for k is called each
time exception k occurs.
Synchronous Exceptions
Caused by events that occur as a result of executing an
instruction:
Traps
Intentional
Examples: system calls, breakpoint traps, special
instructions
Returns control to “next” instruction
Faults
Unintentional but possibly recoverable
Examples: page faults (recoverable), protection faults
(unrecoverable).
Either re-executes faulting (“current”) instruction or
aborts.
Aborts
unintentional and unrecoverable
Examples: parity error, machine check.
Aborts current program
Trap Example
Opening a File
User calls open(filename, options)
0804d070 <__libc_open>:
. . .
804d082:
cd 80
804d084:
5b
. . .
int
pop
$0x80
%ebx
Function open executes system call instruction int
OS must find or create file, get it ready for reading or writing
Returns integer file descriptor
User Process
int
pop
OS
exception
return
Open file
Fault Example # 1
Memory Reference
User writes to memory location
That portion (page) of user’s memory
is currently on disk
80483b7:
c7 05 10 9d 04 08 0d
$0xd,0x8049d10
Page handler must load page into
physical memory
Returns to faulting instruction
Successful on second try
User Process
event
movl
int a[1000];
main ()
{
a[500] = 13;
}
movl
OS
page fault
return
Create page and load
into memory
Fault Example # 2
int a[1000];
main ()
{
a[5000] = 13;
}
Memory Reference
80483b7:
User writes to memory location
Address is not valid
c7 05 60 e3 04 08 0d
$0xd,0x804e360
Page handler detects invalid address
Sends SIGSEG signal to user process
User process exits with “segmentation fault”
User Process
event
movl
movl
OS
page fault
Detect invalid address
Signal process
The Abstract Machine Interface
Application Program
Abstract Machine Instructions
Trap
Instruction
User Mode
Instructions
fork()
open()
OS
User Mode
User Mode
Instructions
Instructions
Supervisor Mode
Supervisor Mode
Instructions
Instructions
create()
Context Switching
Executable Memory
Initialization
Interrupt
1
Process
Manager
7
Interrupt
Handler
2
4
9
P1
P2
6
Pn
8
3
5
When to Switch a Process
Clock interrupt
process has executed for the maximum allowable
time slice
I/O interrupt
Memory fault
memory address is in virtual memory so it must be
brought into main memory
When to Switch a Process
Trap
error or exception occurred
may cause process to be moved to Exit state
Supervisor call
such as file open
Process Creation
Assign a unique process identifier
Allocate space for the process
Initialize process control block
Set up appropriate linkages
Ex: add new process to linked list used for
scheduling queue
Create or expand other data structures
Ex: maintain an accounting file
Change of Process State
Save context of processor including program
counter and other registers
Update the process control block of the
process that is currently in the Running state
Move process control block to appropriate
queue – ready; blocked; ready/suspend
Select another process for execution
Change of Process State
Update the process control block of the
process selected
Update memory-management data structures
Restore context of the selected process