Virtualizing CPU
Introduction
- Process is a running program, which includes:
- Memory (address space)
- Instructions
- Data sections
- Registers
- Program counter
- Stack pointer
- Memory (address space)
Process API
Create- create a new process to run a program
Destory- halt a runaway process
Wait- wait for a process to stop running
Miscellaneous Control- some kind of method to suspend a process and then resume it (暫停然後恢復)
Status- get some status information about a process
Process Creation
Load a program code into memory, then build up an address space for the process.
- The OS performs the loading process lazily, it loads the pieces of code or data only when they are needed during the program execution.
Allocate the program’s run-time process stack (store at the bottom of the address space)
Used for local variables, function parameters and return address.
Initialize the stack with arguments
argcand theargvarray ofmain()function.
Create the program’s heap (store at the position that after the coding)
- Used for requesting dynamically allocated data.
- Program requests such space by calling
malloc()and free it by callingfree().
Do the initialization tasks
- Input / Output setup
- each process has three open file descriptors in default
- standard input, output an error
- Input / Output setup
Start the program running at the entry point (
main())

Process Status
Running
- A process is running on a processor.
Ready
- A process is ready to run but for some reason the OS has chosen not to run it at the moment.
Blocked
- A process has performed some kind of operation.
- When a process initiates an I/O request to a disk, it becomes blocked and thus some other process can use the processor.

Data Structures
In the OS system, the key data structure
Process List
- Current running process
- Ready processes
- Blocked processes
Register Context
- the registers we need to run the process.
Process Control Block
- For each process, there is an in-memory data structure that contains information about that process.
Execution Mechanism
- The OS need to share the physical CPU by time sharing,
i.e., How we implement time sharing among multiple processes.- Performance: How can we implement virtualization without add excessive overhead to the system?
- Control: How can we run processes efficiently while retaining control over the CPU?
Direct Execution
- Execution Procedure

Problem: Restricted Operation
- if a process wishes to perform some kind of restricted operation (受限的操作), such as,
- issung an I/O request to a disk
- gaining access to more system resources like CPU or memory
- The running program should be limited, otherwise the OS would not be in control of anything (it just becomes a library).
- There should be something to isolate the applications and OS system, not allow the process touches the other processes’ address space.
- if a process wishes to perform some kind of restricted operation (受限的操作), such as,
Solution:
Using protected control transfer to control users’ behaviors.
User Mode: Applications do not have full access to hardware resources.
Kernel Mode: The OS has access to the full resources of the machine.
- There are some privileged instructions
- If users execute those privileged instructions under user mode, the system can limit the users’ behaviors.
- Users can only execute those privileged instructions under kernel mode through system call.
System Call
- Allow the users utilize the sysyem services through system calls.
- It allows the kernel to carefully expose certain key pieces of functionality to user program, such as,
- accessing the file system
- creating and destorying processes
- communicating with other processes
- allocating more memory
- Users can only call those system calls to get the services.
Trap Instuction
- switch from user mode to kernel mode (raise the privilege level to kernel mode).
Return-from-trap Instruction
- return into the calling user program (reduce the privilege level to user mode).
Limited Direction Execution
Execution Procedure

Problem: Switching between Processes
- How can the OS regain control (重新獲取控制權) of the CPU so that it can switch between the processes?
- **Cooperative Approach: Wait for system call** - Processes periodically give up the CPU by making system calls, like `yield` (i.e., wait until the running process gives up the control). - The OS decides to run other tasks. - Application also transfers control to the OS when they do something illegal, such as, divide by zero. - Problem: a process may get stuck in an infinite loop, if this happened, we need to reboot the machine.
Non-cooperative Approach: The OS takes control through a timer interrupt.
During the boot sequence, the OS starts the timer.
The timer raises an interrupt every specific milliseconds.
When the interrupt is raised
- The currect running process is halted.
- Save enough of the state of the program.
- A pre-configured interrupt handler in the OS runs.
After that, the scheduler can make a decision:
- Continue running the current process or switch to different process.
- If the decision is switch, the OS will execute Context switch.
Context Switch
Save the value of necessary registers for the current process onto its kernel stack.
Restore the register values for the soon-to-be-executing process from its kernel stack.
Switch to the kernel stack for the soon-to-be-executing process.
- Examples: If we want to switch from process A to process B
- save the necessary registers of process A onto its kernel stack
- restore the registers for process B from its kernel stack
- switch to process B kernel stack




