Memory Hierarchy • Main Memory - located on chips inside the system unit. • The program instructions and the processes data are kept in main memory during computer works. Exceptions, Interrupts & Traps • External Memory - disk. Information stored on a disk is not deleted when the computer turned off. • The main memory has less storage capacity than the hard disk. The hard disk can write and read information to and from the Operating System main memory. The access speed of main memory is much faster than a hard disk. Hebrew University • Programs are stored on the disk until they are loaded into Spring 2007 memory, and then use the disk as both the source and destination of the information for their processing. 1 2 Typical Memory Hierarchy Definitions… • When the CPU is in kernel mode , it is assumed to be executing trusted software, and thus it can execute any instructions and reference any memory addresses. • The kernel is the core of the operating system and it has complete control over everything that occurs in the system. The kernel is trusted software, but all other programs are considered untrusted software. • A system call is a request to the kernel in a Unix-like operating system by an active process for a service performed by the kernel. 3 4 More Definitions… More Definitions… • An interrupt is a signal to the operating system • A process is an executing instance of a program. An indicating that an event has occurred, and it results in changes in the sequence of instructions that is executed active process is a process that is currently advancing by the CPU. in the CPU (while other processes are waiting in • In the case of a hardware interrupt , the signal originates memory for their turns to use the CPU). from a hardware device such as a keyboard (i.e., when a • Input/output (I/O) is any program, operation or device user presses a key), mouse or system clock (used to that transfers data to or from the CPU and to or from coordinate the computer's activities). a peripheral device (such as disk drives, keyboards, • A software interrupt is an interrupt that originates in mice and printers). software, usually triggered by a program in user mode. • Processes in kernel mode can be interrupted by an • All processes initially execute in user mode, and they interrupt or an exception . switch to kernel mode only when obtaining a service provided by the kernel. 5 6 1
More Definitions… Interrupts - Motivation • User mode is a non-privileged mode in which it is • Much of the functionality embedded inside a personal forbidden for processes in this mode to access those computer is implemented by hardware devices other portions of memory that have been allocated to the than the processor. kernel or to other programs. • Since each device operates at its own pace, a method • When a user mode process wants to use a service that is needed for synchronizing the operation of the is provided by the kernel, it must switch temporarily processor with these devices. into kernel mode. • One solution is for the processor to sit in a tight loop, • Process in kernel mode has root (i.e., administrative) asking each device about its current state. privileges and access to key system resources. • When data is available in one of the devices, the • The entire kernel, which is not a process but a processor can then read and process the incoming controller of processes, executes only in kernel mode. bytes. • When the kernel has satisfied the request by a process, it returns the process to user mode. 7 8 Interrupts - Motivation Hardware Interrupts • This method works but it has two main • Instead of polling hardware devices to wait for disadvantages: their response, each device is responsible for notifying the processor about its current state. 1. Wasteful in terms of processing power - the processor is constantly busy reading the status of • When a hardware device needs the processor's the attached devices instead of executing some attention, it simply sends an electrical signal useful code. (hardware interrupt) through a dedicated pin in 2. When the rate of data transfer is extremely high, the interrupt controller chip (located on the the processor might lose data bytes arriving from computer's motherboard). the hardware devices. 9 10 Signals Software Interrupts • Signals that come from outside the process are • Most of these interrupts are synchronous asynchronous software interrupts. rather than asynchronous. • A process can install handlers to take special action when a signal arrives. • They are generated by the processor itself as a • Signals are defined and handled entirely through result of some command. software. • For example: A user at a terminal typing the interrupt key to stop a program (^C). • Signal causes: – User press certain terminal key – Exception (for example: invalid memory reference) – The kill function 11 12 2
Exceptions Exceptions – Cont. • Exceptions - special type of software interrupts. • Three types of exceptions can be generated by the processor: • They are generated by the processor itself whenever some unexpected critical event occurs. – Faults • For instance, a page fault exception is triggered when – Traps the processor attempts to access memory portion, – Aborts which is marked as not-present. • The exception handler can then reload this memory portion (page) from disk and restart the instruction which generated the exception. 13 14 Fault Fault Example • A program requests data that is not currently in • When a fault exception occurs, the registers real memory. point to the address of the instruction, which • An interrupt triggers the operating system to generated the exception. fetch the data from the disk and load it into • This gives the exception handler a chance to main memory. fix the condition which caused the exception to • The program gets its data without even know occur, before restarting the faulting instruction. that an exception has occurred. • The program is restarted at the address of the • The program continue with the same fault. instruction. 15 16 An Example Trap open(“/tmp/foo”): • USER: • The execution of an instruction that intended for user – store the system call number and the parameters in a programs and transfers control to the operating predefined kernel memory location; system. – trap(); • Trap causes branch to OS code and a switch to kernel – retrieve the response from a predefined kernel memory location; mode . – return the response to the calling application; • When in kernel mode, a trap handler is executed to • KERNEL: service the request. – trap(): • Restarted at the address following the address causing jump &int[80]; // transfer control to the gate routine the trap. – Gate routine: • Example: any System Call. switch(sys_call_num) { case OPEN: … } 17 18 3
Abort Hardware Interrupts • Aborts neglect to specify the location of the • Ideally, to perform multiple tasks (e.g., run the faulting instruction, since they are used to program, service the display, service the indicate severe errors (such as hardware errors) keyboard etc.) we would employ multiple which are not recoverable. processors. • Give no reliable restart address. • Alternatively we can allow the CPU to split its time between the main tasks and handling • Examples: requests as they occur. – Divide by zero • If done efficiently, then conceptually the CPU – Access to unallocated memory, a segmentation is performing all tasks simultaneously fault. 19 20 Dealing with Interrupts The Handler • Combination of hardware & software is • Determined by the cause that generated the necessary to deal with interrupts: interrupt and determines how it should be dealt with (e.g., an input device has some data • hardware chooses time to interrupt the ready). program and transfer control. • Must preserve the state of the previously • software in the form of the handler to deal running program. with the interrupt. • Should be fast so minimal (none?) impact on previously running program. 21 22 The Interrupt Controller Hardware Interrupt Causes • The interrupt controller serves as an • Caused by an external event which typically intermediate between the hardware devices needs routine attention. and the processor. • For example: • Its responsibility is to alert the processor when • Disk drive has data that was requested 20 ms one of the hardware devices needs its ago. immediate attention. • User pressed a key on the keyboard. • In this case, the processor stops its current • User sneezed, causing mouse to move. activity and jumps to execute a function • Timer (used by the OS as an alarm clock) (interrupt handler) which was previously expired. associated with the calling device. 23 24 4
Recommend
More recommend