
The Operating system runs in kernel mode. In this mode, it has complete access to all the hardware and can execute all instructions. All the other softwares runs in user mode.
What is an operating system?
From a top-down view, the operating system is an extended machine which hides the ugly hardware and provides abstractions to applications.

In the bottom-up view, the opearting system works as a resource manager.
- Allocation: allocate finite resources to various programs
- Protection: prevent users from interfering with one another
- Reclamation: reclaim resources
- Virtualization: illusion of infinite private resources
The operating system multiplexs (shares) resources in two different ways: in time and in space.
- When a resource is time multiplexed, different users take turns using it, eg: cpu, printer.
- When a resource is space multiplexed, each user gets part of it, eg: main memory.
Booting the computer
When the computer is booted:
- BIOS (Basic Input Output System) starts
- checks how much RAM is installed and whether basic devices are responding correctly
- determines the boot device
- The first sector from the boot device is read into memory and executed to determine the active partition
- The secondary boot loader is read from that parition
- The loader reads in the opearting system from the active partition and starts it
Computer hardware

Conceptually, CPU and hadrwares are all connected by a system bus and communicate with each otehr over it. But actually, it has a more complicated structure, invovling multiple buses.
Processors
All CPUs contains some registers inside:
- General registers: hold key variables and temporary results
- Special registers
- Program Counter: contains the memory address of the next instuction
- Stack pointer: points to the top of the current stack in memory
- PSW(Program Status Word): contains the condition code bits, the cpu priority, the mode (user/kernel) various control bits.
The basic cycle of CPU is to fetch, decode, execute instruction.
A pipeline CPU have separate fetch, decode, execute units, a superscalar CPU have multiple units.

Memory
The memory system is constructed as a hierarchy of layers.

The registers internal to CPU are as fast as the CPU.
Main Memory is divided up into cache lines, and the most heavily used cache lines are kept in high-spped cache.
When the program needs to read a memory word, the cache checks if the line is in the cache. If so, it’s called a cache hit, and no memory access is needed. Otherwise, it’s called cache miss, and have to go to the memory.
Data can be in read state in multiple caches but only in one cache when in write state.
OS Major Components
Process and Thread Management
A process is basically a program in excution associated with:
Address space: a list of memory locations which the process can read and write
Defines where several segments are located
Defines protection of those sections: ReadOnly, ReadWrite, Execute
Process table entry: an array of structures holding all the other information about the process
System call
To obtain services from the opreating system, a user program must make a system call, which switches to the kernel mode. There are three ways to transfer from user mode into kernel mode:
- Trap: TRAP instruction for system call
- Interrupt: triggered by an event from device
- Execption: triggered by fault
The interrupt handler (_entry in the kernel) identifies them and then jumps to their respective handlers.
System call examples

Fork creates an exact duplicate of the original process. After the fork, the parent and child process go their separate ways. As shown in the follwing example, the fork call returns a value, which is zero in the child and equal to the child’s PID (Process Identifier) in the parent.

Clone also creates a new process. One difference is that it allows the child process to share parts of its execution context with the parent process, such as the memory space.
The kernel uses signals to call into a process. There are a few examples of signals.

When a signal is delivered,
How are syscalls implemented(user & kernel)
- Agreed on calling code convention: typically aruguments are passed through regesiters(sometimes on the stack)
- Agreed on definition of system call numbers
- Kernel defines a syscall table, the complier help associate the syscall number with the kernel internal function
After the TRAP instruction switches from user mode to kernel mode, the interrupt handler:
- checks the syscall number
- change the stack to kernel
- call to responding function in syscall table
- switch back to user stack and return from kernel mode
Steps in making a system call

Operating system structure
Monolithic systems
In the monolithic approach, the entire opearting system runs as a single program in kernel mode. The operating system is written as a collection of procedures linked together. This organization suggests a basic structure for the operating system:
A main program that invokes the requested services
A set of services procedures that carry out the system calls
A set of utility procedures that help the service procedure

Layered Systems
A generalization is to organize the operating systems as a hierarchy of layers, each one constructed upon the one below it.

Microkernels
Tranditionally, with the layered approach, all the layers went in the kernel. However, a strong case can be made for putting as little as possible in the kernel mode since bugs in kernel can bring down the system instantly.
The microkernel design split the operating system up into small, well defined modules, only one of which - the microkernel - runs in kernel mode. Outside the kernel, the system is structed as three layers of processes all running in user mode.

Client-Sever Model
The client-server model is a slight variation of the microkernel idea, which distinguish two classes of processes, the server, which provides some services, and the clients, which use these services. Communication between servers and clients is via message passing.

Virtual Machine
VM/370
The essence of VM/370 is to completely separate the two functions of the timesharing system:
- multiprogramming
- an extended machine with more convenient interface than the bare hardware
The virtual machine monitor runs on the bare hardware, does multiprogramming, providing several virtual machines to the next layer up.
These virtual machines are exact copies of the bare hardware, so each one can run any opearting system that will run directly on the bare hardware.

Type 1&2 hypervisor
In type 1 hypervisor, privileged instructions are trapped to the virtual machine monitor, and emulated in software.
Type 2 hypervisor makes user of a host opearting system
