Module 1 2 Fundamental Concepts Interrupts System Calls

  • Slides: 19
Download presentation
Module 1. 2: Fundamental Concepts • • Interrupts System Calls K. Salah 1 Operating

Module 1. 2: Fundamental Concepts • • Interrupts System Calls K. Salah 1 Operating Systems

Dual-Mode Operation • Sharing system resources requires operating system to ensure that an incorrect

Dual-Mode Operation • Sharing system resources requires operating system to ensure that an incorrect program cannot cause other programs to execute incorrectly. • Provide hardware support to differentiate between at least two modes of operations. 1. User mode – execution done on behalf of a user. 2. Monitor mode (also supervisor mode or system mode) – execution done on behalf of operating system. K. Salah 2 Operating Systems

Dual-Mode Operation (Cont. ) • Mode bit added to computer hardware to indicate the

Dual-Mode Operation (Cont. ) • Mode bit added to computer hardware to indicate the current mode: monitor (0) or user (1). – Part of EFLAG in x 86 architecture – Part of PSW in Motorola architecture • When an interrupt or fault occurs hardware switches to monitor mode. Interrupt/fault monitor user set user mode • • • Privileged instructions can be issued only in monitor mode. All I/O instructions are privileged instructions. Must ensure that a user program could never gain control of the computer in monitor mode (I. e. , a user program that, as part of its execution, traps if it executes a privileged instruction). K. Salah 3 Operating Systems

Interrupts (fundamental concept) • • An interruption of the normal processing of processor. •

Interrupts (fundamental concept) • • An interruption of the normal processing of processor. • Reasons for interrupts (or traps): – control of asynchronous I/O devices – CPU scheduling – exceptional conditions (e. g. , div. by zero, page fault, illegal instruction) arising during execution – user process requests for OS services • Interrupts are essentially what drives an OS. We can view the OS as an event-driven system, where an interrupt is an event. • By their very nature, interrupts need to be serviced carefully and quickly by the OS. Interrupts are a mechanism for causing the CPU to suspend its current computation and take up some new task. Control may be returned to the original task at some time later. K. Salah 4 Operating Systems

Interrupt Handling • • The servicing of an interrupt is known as interrupt handling.

Interrupt Handling • • The servicing of an interrupt is known as interrupt handling. • The OS maintains a table, known as the interrupt vector, that associates each interrupt's id with the starting address of its service routine. • Example interrupt vector: An integer is associated with each type of interrupt. When an interrupt occurs, the corresponding integer is supplied to the OS usually by the hardware (in a register). Interrupt No. 0 1 2 3 4 5 K. Salah Interrupt Handler clock disk tty dev soft other 5 Operating Systems

Typical interrupt handling sequence · · · Interrupt initiated by: I/O device signaling CPU,

Typical interrupt handling sequence · · · Interrupt initiated by: I/O device signaling CPU, exceptional condition arising, execution of special instruction, etc. CPU suspends execution of current instruction stream and saves the state of the interrupted process (on hardware stack). State typically refers to contents of registers: PC, PSW, SP, general-purpose registers. The cause of the interrupt is determined (and the unit no. of the interrupt, if applicable) and the interrupt vector is consulted in order to transfer control to the appropriate interrupt handler. Interrupt handler performs whatever processing is necessary to deal with the interrupt. Previous CPU state is restored (popped) from system stack, and CPU returns control to interrupted task. K. Salah 6 Operating Systems

EXAMPLE: Servicing a Timer Interrupt • Timer device is used in CPU scheduling to

EXAMPLE: Servicing a Timer Interrupt • Timer device is used in CPU scheduling to make sure control is returned to system every so often (e. g. , 1/60 sec. ) • Typically, timer has a single register that can be loaded with an integer indicating a particular time delay (# of ticks). • Once loaded, timer counts down and when 0 is reached, an interrupt is generated. • Interrupt handler might do the following: – update time-of-day information – signal any processes that are "asleep" and awaiting this alarm – call the CPU scheduler • Control returns to user mode, possibly to a different process than the one executing when the interrupt occurred. K. Salah 7 Operating Systems

EXAMPLE: Servicing a Disk Interrupt • When disk controller completes previous transfer, it generates

EXAMPLE: Servicing a Disk Interrupt • When disk controller completes previous transfer, it generates an interrupt. • Interrupt handler changes the state of a process that was waiting for just-completed transfer from wait-state to ready-state. • It also examines queue of I/O requests to obtain next request. • I/O is initiated on next request. • CPU scheduler called. • Control returned to user mode. K. Salah 8 Operating Systems

Priority Interrupts • Some fast devices (e. g. disk, timer) must be serviced with

Priority Interrupts • Some fast devices (e. g. disk, timer) must be serviced with stringent real-time constraints. Other, slower devices (e. g. TTY) need not be serviced as quickly. • • Failure to service fast devices soon enough may result in lost interrupts. The priority interrupt mechanism allows the interrupt handler of a slow device to be interrupted by a faster device, while blocking out interrupts from slower devices during execution of interrupt handler of a fast device. Machine Errors Clock Higher priority Disk Network Devices Terminals Lower Priority Software interrupts Typical Interrupt Levels K. Salah 9 Operating Systems

Priority Interrupt Mechanism • How it works: – CPU status register (PSW) contains bits

Priority Interrupt Mechanism • How it works: – CPU status register (PSW) contains bits specifying processor priority (or execution) level. – Each device has an associated device priority level. A device may cause an interrupt only when its priority level is higher than the current processor priority level. – Interrupt handler for a device executes at processor priority equal to device priority. – Effect: An interrupt handler can only be interrupted by devices of higher priority. K. Salah 10 Operating Systems

System Calls • Provide "direct access" to operating system services (e. g. , file

System Calls • Provide "direct access" to operating system services (e. g. , file system, I/O routines, memory allocate & free routines) by user programs. • System calls execute instructions that control the resources of the computer system, e. g. , I/O instructions for devices. • We want such privileged instructions to be executed only by a system routine, under the control of the OS! • As we will see, system calls are special, and in fact, are treated as a special case of interrupts. • Programs that make system calls were traditionally called "system programs" and were traditionally implemented in assembly language. K. Salah 11 Operating Systems

System Call Scenario (cont. ) File system User program I/O devices memory Operating System

System Call Scenario (cont. ) File system User program I/O devices memory Operating System User program is confined! K. Salah 12 Operating Systems

API vs. System Calls • Mostly accessed by programs via a high-level Application Program

API vs. System Calls • Mostly accessed by programs via a high-level Application Program Interface (API) rather than direct system call use • Three most common APIs are Win 32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM) • Why use APIs rather than system calls? K. Salah 13 Operating Systems

Example of Standard API • • Consider the Read. File() function in the •

Example of Standard API • • Consider the Read. File() function in the • A description of the parameters passed to Read. File() – HANDLE file—the file to be read – LPVOID buffer—a buffer where the data will be read into and written from – DWORD bytes. To. Read—the number of bytes to be read into the buffer – LPDWORD bytes. Read—the number of bytes read during the last read – LPOVERLAPPED ovl—indicates if overlapped I/O is being used Win 32 API—a function for reading from a file K. Salah 14 Operating Systems

API – System Call – OS Relationship K. Salah 15 Operating Systems

API – System Call – OS Relationship K. Salah 15 Operating Systems

Standard C Library Example • C program invoking printf() library call, which calls write()

Standard C Library Example • C program invoking printf() library call, which calls write() system call K. Salah 16 Operating Systems

System Call Implementation • Typically, a number associated with each system call – System-call

System Call Implementation • Typically, a number associated with each system call – System-call interface maintains a table indexed according to these numbers • The system call interface invokes intended system call in OS kernel and returns status of the system call and any return values • The callers need know nothing about how the system call is implemented – Just needs to obey API and understand what OS will do as a result call – Most details of OS interface hidden from programmer by API T Managed by run-time support library (set of functions built into libraries included with compiler) K. Salah 17 Operating Systems

System Calls (cont. ) • Now, system calls can be made from high-level languages,

System Calls (cont. ) • Now, system calls can be made from high-level languages, such as C and Modula-2 (to a degree). • • Unix has about 32 system calls: read(), write(), open(), close(), fork(), … using trap instructions: – i = read(fd, 80, buffer) push buffer push 80 push fd trap read pop i • Each system call had a particular number. Instruction set has a special instruction for making system calls: SVC (IBM 360/370) trap (PDP 11) tw (Power. PC) - trap word tcc (Sparc) break (MIPS) K. Salah 18 Operating Systems

Further Reading • Why use APIs rather than system calls? K. Salah 19 Operating

Further Reading • Why use APIs rather than system calls? K. Salah 19 Operating Systems