Advanced Operating Systems - Lecture 4: Process. This lecture will cover the following: 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;...
Trang 1 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
Trang 2 A program in execution
An instance of a program running on a computer
The entity that can be assigned to and executed on
Trang 3 Each process has its own private address space
kernel virtual memory (code, data, heap, stack)
memory mapped region for
shared libraries
run-time heap (managed by malloc)
user stack (created at runtime)
unused 0
%esp (stack pointer)
memory invisible to user code
loaded from the executable file
0xffffffff
Trang 4OS Address Space
OS Address Space
Control Unit
Pi Address Space
Pi CPU
Pi Executable Memory
Pi Executable Memory
Pk Address Space
Pk Address Space
…
Pk CPU
Pk Executable Memory
Pk Executable Memory
Pj Address Space
Pj Address Space
Pj CPU
Pj Executable Memory
Pj Executable Memory
Trang 5Process
Address Space AddressBinding
Executable Memory
Other objects
Files
Trang 6 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
Trang 7 Process-Based Operating System
Implement operating system as a collection of system processes
Useful in multi-processor or multi-computer
environment
Trang 8<startup>
inst 1 inst 2 inst 3
… inst n
<shutdown>
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
Trang 9 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”
Trang 10 Combination of hardware and OS software
Higher Level Mechanisms
Process context switch
Signals
Nonlocal jumps (setjmp/longjmp)
Implemented by either:
Trang 11IDE disk controller
IDE disk controller
Video adapter
Video adapter
Display Network
Processor Interrupt
controller
Interrupt controller
SCSI controller
SCSI controller
SCSI bus
Serial port controller
Serial port controller Parallel port controller
Parallel port controller
Keyboard controller
Keyboard controller
Keyboard Mouse Modem Printer
disk
disk CDROM
Trang 12s
An exception is a transfer of control to the OS in
response to some event (i.e., change in processor
exception
exception processing
by exception handler
exception return (optional) event current
next
Trang 13 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
Trang 14 Handler for k is called each time exception k occurs
code for exception handler 0
code for exception handler 1
code for exception handler 1
code for exception handler 2
code for exception handler 2
code for exception handler n-1
code for exception handler n-1
Exception
numbers
Trang 15 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
Trang 16Open file
exception return
int pop
Opening a File
User calls open(filename, options)
Function open executes system call instruction int
OS must find or create file, get it ready for reading or writing
Returns integer file descriptor
Trang 17 User writes to memory location
That portion (page) of user’s memory
is currently on disk
Page handler must load page into
physical memory
Returns to faulting instruction
Successful on second try
int a[1000];
main () {
a[500] = 13; }
Fault Example # 1
Trang 18 User writes to memory location
Address is not valid
Page handler detects invalid address
Sends SIGSEG signal to user process
User process exits with “segmentation fault”
int a[1000];
main () {
a[5000] = 13; }
Signal process
Fault Example # 2
Trang 19User Mode Instructions
User Mode Instructions
Supervisor Mode Instructions
Supervisor Mode Instructions
fork() create() open()
OS
Trang 20Process Manager
Interrupt Handler
P1
P2
Pn
Executable Memory Initialization 1
Trang 22 Trap
error or exception occurred
may cause process to be moved to Exit state
Supervisor call
such as file open
Trang 23 Ex: add new process to linked list used for scheduling queue
Ex: maintain an accounting file
Trang 24counter and other registers
process that is currently in the Running state
queue – ready; blocked; ready/suspend
Trang 25process selected
Trang 26 Process may be in one of two states
Running
Not-running
Trang 27NotRunning Process in a Queue
Trang 28Fivestate Model
Trang 29 Null New – Process is created
New Ready – O.S is ready to handle another process (Memory, CPU)
Ready Running – Select another process to run
Running Exit – Process has terminated
Running Ready – End of time slice or higher-priority process is ready
Running Blocked – Process is waiting for an event (I/O,
Synchronization)
Blocked Ready – The event a process is waiting for has
occurred, can continue
Ready Exit – Process terminated by O.S or parent
Blocked Exit – Same reasons