# General Overview
# Operating System Services
* Controlling the execution of processes by allowing their creation, termination or suspension, and communication
* Scheduling processes fairly for execution on the CPU. Processes share the CPU in a time-shared manner: the CPU executes a process, the kernel suspends it when its time quantum elapses, and the kernel schedules another process to execute. The kernel later reschedules the suspended proeess.
* Allocating main memory for an executing process. The kernel allows processes to share portions of their address space under certain conditions, but protects the private address space of a process from outside tampering. If the system runs low on free memory, the kernel frees memory by writing a process temporarily to secondary memory, called a swap device.
* If the kernel writes entire processes to a swap device, the implementation of the Unix system is called a swapping system
* if it writes pages or memory to a swap device, it is called a paging system
* Allocating secondary memory for efficient storage and retrieval of user data
* This service constitutes the file system
* The kernel allocates secondary storage for user files
* reclaims unused storage
* structures the file system in a well understood manner
* protects user files from illegal access.
* Allowing processes controlled access to peripheral devices such as terminals, tape drives, disk drives, and network·devices.
General Notes
* Kernel provides it's services transparently
* Hides the distinction between regular file or a device
* formats data in a file for internal storage, but returns unformatted byte stream
* Provides necessary services so that user-level processes can support the services they must provide
* Ex: Allows shell to read terminal input, spawn processes dynamically, synchronize process execution, create pipes, redirect I/O etc.,
## File System
### Characteristics
The Unix file system is characterized by following properties
* A hierarchical structure,
* Consistent treatment of file data,
* The ability to create and delete files,
* Dynamic growth of files,
* The protection of file data.
* The treatment of peripheral devices as files.
* Ex: terminals and tape units
### General notes
* Programs in Unix system have no knowledge of the internal format in which the kernel stores file data, treating the data as an unformatted stream of bytes
* Programs may interpret the byte stream as they wish, but the interpretation has no bearing on how the operating system stores the data
* Directories are like regular files in this respect
* Access permissions control permissions
* Devices are designated by special device files, occupy node positions in the file system directory structure
* file processing system calls
* open, creat, copy, read, write
* Ex: copy /dev/tty terminalread
* Ex: copy /dev/tty /dev/tty
## Process System
### General notes
* Program is an executable file
* Process is an instance of the program in execution
* Many processes can execute simultaneously on Unix system
* Multiprogramming/Multitasking
* No logical limit on the no. of process that can be spawned
* Process system calls
* fork, exec, wait, exit
### Process Cycle
* Parent process creates child process using fork
* fork returns 0 to child process and invokes exec to execute designated child program
* exec call overlays the address space of the child process with the file content of child program
* exec call never returns because the process executes in a new address space
* child exits
* fork returns non-zero value to parent process
* parent process waits
* suspends execution until child process completes
* parent exits
* every program exits implicitly at the end of "C" main function
### Shell
Shell allows three types of commands
* Executable file
* Shell script
* Internal Commands
General Notes
* Internal commands make the shell a programming language in addition to a command interpreter and include commands for looping (for-in-do-done, while-do-done) and conditional execution (if-then-else-fi)
* Pattern matching
* Parameter processing
* Executes commands synchronously or asynchronously (&) in background
* Execution environment for the process (Current directory)
### Building block primitives
* < - Standard input
* > - Standard output
* 2> - Standard error
* | - Pipe => Standard input --> Standard ouput
### Execution Modes
* user mode
* can access their own instructions and data, but not kernel instructions and data
* kernel mode
* can access both kernel and user addresses
* virtual address space of a process may be divided between addresses that are accessible only in kernel mode and addresses that are accessible in either mode
* some machine instructions are privileged and result in an error when executed in user mode
General Notes
* Hardware views the world in terms of kernel mode and user mode, but does not distinguish among the many users executing programs in those modes
* OS keeps internal records to distinguish the many processes executing on the system
* Kernel in this context is not a separate set of processes that run in parallel to user processes, but it is part of each user process.
* Kernel allocating resources means, process executing in kernel mode allocates the resources
### Interrups and Exceptions
#### Interrupts
* Unix I/O peripherals or System clock to interrupt the CPU asynchronously
* caused not by process
* When Interrupted
* Kernel saves it's current context
* frozen image of what the process was doing
* determines the cause of interrupt
* services the interrupt
* restores it's interrupted context
* proceeds the execution as if nothing had happenned
* Hardware prioritizes devices according to the order that interrupts should be handled
* blocks out low priority interrupts, but services high priority interrupts
* Happen between the execution of two instructions
#### Exceptions
* An exception condition refers to unexpected events caused by a process
* Addressing illegal memory
* Executing privileged instructions
* Dividing by zero etc.,
* Could happen in the middle of the execution of an instruction
#### Process Execution Levels
* Kernel sometimes prevent the occurrence of interrupts during critical activity which could result in corrupt data if interrupts were allowed
* Computers typically have a set of privileged instructions that set the processor execution level in the processor status word
* Setting the processor execution level to certain values masks off interrupts from that level and lower levels allowing only higher level interrupts
* Example
* Machine Errors -> Clock -> Disk -> Network devices -> Terminals
-> Software Interrupts
* Highest to Lowest
* if Disk interrupt is masked, all interrupts except for clock and machine interrupts are prevented
### OS Bookkeeping related to user process
* Handling interrupts
* Scheduling processes
* Managing Memory
## Memory Management
* Kernel permanently resides in main memory
* Process that are currently executing also resides in main memory
* either completely or atleast parts of it
* Compiler generates a set of addresses in the program that represent addresses of variables, data structures, address of instructions such as functions
* Compiler generates the addresses for a virtual machine as if no other program will execute simulaneously on the physical machine
* When a process starts, kernel allocates space in main memory for it
* Kernel coordinates with machine hardware to setup a virtual to physical address translation that maps the compiler generated addresses to the physical machine addresses