# OS Ch1: Introduction ###### tags: `OS` ## Bootstrap program - Bootstrap program is loaded at power-up or reboot - Bootstrap program is stored in ROM or EPROM - Called Firmware (韌體) - Bootstrap program initializes all aspects of system - Bootstrap program loads operating system kernel and starts execution ## Interrupt > https://mropengate.blogspot.com/2015/01/operating-system-ch2-os-structures.html **Device controller** informs CPU that it has finished its operation by causing an **interrupt**. - **Interrupt vector**: - Interrupt transfers control to the interrupt service routine through the interrupt vector. - Interrupt vector contains the addresses of all the service routines. - Incoming interrupts are disabled if another interrupt is being processed to prevent a **lost interrupt**. - **Trap** (Exception): a **software-generated** interrupt caused either by an error or a user request. - An operating system is **interrupt driven**. - 現在的作業系統是中斷驅動式 (interrupt driven) 。如果沒有 process 要執行,沒有 I/O 裝置要服務和沒有使用者需要回應,則作業系統將安靜的進入等待事件的發生。 > A **trap** is an **exception in a user process**. It's caused by **division by zero** or **invalid memory access**. It's also the usual way to invoke a kernel routine (a system call) because those run with a higher priority than user code. Handling is synchronous (so the user code is suspended and continues afterwards). In a sense they are "active" - most of the time, the code expects the trap to happen and relies on this fact. > > An **interrupt** is something **generated by the hardware** (devices like the hard disk, graphics card, I/O ports, etc). These are asynchronous (i.e. they don't happen at predictable places in the user code) or "passive" since the interrupt handler has to wait for them to happen eventually. > > = https://stackoverflow.com/questions/3149175/what-is-the-difference-between-trap-and-interrupt ### Interrupt Handling - The operating system preserves the state of the CPU by storing registers and the program counter. - **Polled interrupt**: is a specific type of I/O interrupt that notifies the part of the computer containing the I/O interface that a device is ready to be read or otherwise handled, **but does not indicate which device.** The interrupt controller must poll (send a signal out to) each device to determine which one made the request. - **Vectored interrupt**: In a computer, a vectored interrupt is an I/O interrupt that tells the part of the computer that handles I/O interrupts at the hardware level that a request for attention from an I/O device has been received and and also **identifies the device that sent the request**. 中斷發生時的處理步驟。 1. 令 CPU 暫停目前 process 的執行,同時保存其當時執行的狀態。 2. 根據 interrupt ID 去查詢 interrupt vector,以便可以找到相對應ISR之起始位址。 3. Jump to 相對應的 ISR (Interrupt Service Routine) 之起始位址,然後執行 ISR 。ISR 執行完畢後,將控制權交回給系統。 4. (原則上)resume 原先中斷前的 process 之執行。 ## I/O **Synchronized I/O** - After I/O starts, **control returns to user program only when I/O completion**. - At most one I/O request is outstanding at a time, **no simultaneous I/O processing**. **Asynchronized I/O** - After I/O starts, **control returns to user program without waiting for I/O completion**. - OS indexes into I/O device table to determine device status and to modify table entry to include interrupt. 必須有一個表以記錄各種裝置 I/O 的位址和使用狀況。 - Users send a request to the OS via system call to allow to wait for I/O completion. - Device-status table contains entry for each I/O device indicating its type, address, and state. ### System call System call is a programming interface to the services provided by the OS. - System call is mostly accessed by programs via a high-level API rather than direct system call use because of portability and simplicity issue. - Win32 API for Windows. - POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X). - Java API for JVM. ## Storage Structure > 硬體儲存裝置的速度 Register > Cache > Main memory > SSD > HDD。 ### Main Memory - Main memory is the only large storage media the CPU can access directly. - Main memory is random-accessed. - Main memory is **volatiled** (斷電後資料會遺失). ### Secondary Storage Secondary storage is an extension of main memory that provides large **non-volatile** storage capacity. ### Hard disks Disks are rigid metal or glass platters covered with magnetic recording material. - A disk can be divided into tracks. A track can be divided into sectors. - The disk controller determines the logical interaction between the device and the computer ### Device driver (驅動程式) - Device driver is for each device controller to manage I/O. - Device driver provides uniform interface between controller and kernel. ### Direct Memory Access Structure (DMA) - Used for high-speed I/O devices able to transmit information **at close to memory speeds**. - Device controller transfers blocks of data from buffer storage **directly to main memory** without CPU intervention. ## Multiprocessors Systems > aka Parallel Systems, aka Tightly-coupled Systems - **Asymmetric Multiprocessing**: each processor is assigned a task. - **Symmetric Multiprocessing**: each processor performs all tasks. ## Clustered Systems Like multiprocessor systems, but **multiple systems working together**. Usually sharing storage via a **storage-area network (SAN)**. - **Asymmetric clustering** has one machine in hot-standby mode. - 有一台機器處於 Hot-standby ,用於監督其他正在執行應用程式的機器,若某台機器壞了,則此監督者可以取得壞掉機器的儲存體所有權,並重新執行其正執行的應用程式。 - **Symmetric clustering** has multiple nodes running applications, monitoring each other. ## Operating System Structure Multiprogramming (Batch system) - Needed for efficiency. Multiprogramming organizes jobs (code and data) so CPU always has one to execute. When it has to wait (for I/O for example), OS switches to another job Timesharing (multitasking) - logical extension in which CPU switches jobs so frequently. ## Memory Management - All or part of the instructions must be in memory. - All or part of the data that is needed by the program must be in memory. -