03 VirtualizeCPU
03 VirtualizeCPU
System Calls
Process switch
Interrupt
Reference
Chapter 3 of Operating Systems, 3rd edition by Deitel et. al
Just needs to know how to use the common or standard API and
understand what OS will do / offer
Most details of OS interface hidden from programmer by API
The system call interface invokes intended system call in kernel and returns
status and results of the system call and pass back to calling program via
API
return
getpid( )
User mode
The Crux
How to transparently and temporarily stop a process and resume it?
With direct execution of application process on CPU, how can OS
regain control of the CPU?
How can OS make sure that the process can be resumed without
affecting its execution logic?
OS needs to save the register context (the contents of CPU registers) of current
running process before stopping it
To resume, OS needs to restore the register context of the soon-to-be running
process back to CPU registers before resuming it
Context Switch
Switching of the register context of one process to another
The newly scheduled process does not have any part of its address
space in cache
Context switch
Switching from one process to another
is an essential feature of multiprogramming or time-sharing systems
Kernel must
(1) suspends the progression of current process and stores its context
(2) retrieves the context of another process (B) and restores it to the CPU
(3) resumes execution of process B
May be initiated by some event that may or may not be related to the running process
Asynchronous with the operation of the current process
Unexpected event from outside the processor
e.g., a key is pressed or the mouse is moved, timer events
Software-generated interrupt
the INT instruction
Interrupt
handler
registers. process the
interrupt
• Interrupt handlers are stored in an
array of function pointers called the
interrupt v ector
• OS sets up the v ector at boot time
The interrupt handler
• CPU reads from the system bus the determines how the
P2
interrupt v ector number prov ided by system should
an interrupt controller respond
• This number is used as an index to
locate the interrupt handler
Principles of Operating Syst ems 18
CPU control
P1
Interrupt vector
Timer device
After completion of
interrupt handling, a
decision has to be made
Interrupt
handler
by the Scheduler: restore
the interrupted process or
switch to a different one.
timer interrupt
Save regs(A) to kernel stack(A)
Move to kernel mode
Jump to trap handler
Handle trap
Call switch() routine to switch from A to B
return-from-trap (into B)
Restore regs(B) to kernel stack(B)
Move to kernel mode
Jump to B’s PC
Process B…
Three mechanisms
System call
Each call represent a service provided by the OS to user application
System mode will switch from user mode to kernel mode, and after the
service, will switch back from kernel mode to user mode
Context switch
The key action of virtualizing the CPU
The whole set of register context must be saved for the interrupted process
The register context of coming process are restored
Context switch involves certain amount of performance overhead
Interrupt processing
A mechanism for hardware devices to alert OS for handling high-priority
events
This gives a chance to OS to regain control of CPUs