CS 471 Operating Systems Jim X. Chen, Ph.D. George Mason University Summer 2009 Overview Textbook • Required: Operating System Concepts (7th edition or latest), by Silberschatz, Galvin and Gagne. John Wiley & Sons. • Recommended: Distributed Systems: Concept and Design (4th Edition, 2005), by Coulouris, Dollimore and Kindberg. Modern Operating Systems (2nd Edition, 2001), by A. S. Tanenbaum Prerequisites • Computer Architecture (CS 365) • Working knowledge of C/C++/Java Grading • Midterm (25%), Final exam (35 %) • Programming projects/Assignments (3 or 4) (30 %) • Homeworks (10%) GMU – CS 471 1.2 Operational Information Office: Engineering, Room 4446 Office Hours: • Office hour: M 5:00pm-6:00pm; F: 4:30pm-5:30pm • E-mail: jchen@gmu.edu Teaching Assistant (TA): Changwei Liu(Coco) <cliu6@gmu.edu> TA Office Hours: Tuesday, 2:00– 4:00pm; Thursday, 3:00 – 5:00pm, in Room 4456. Computer Accounts on mason.gmu.edu or zeus.ite.gmu.edu Class Web Page: http://cs.gmu.edu/~jchen/cs471 Slides available at • http://www.cs.gmu.edu/~jchen/cs471/ 1.3 Course Objectives Understand the principles behind the design of centralized and distributed operating systems Observe how these principles are put into practice in real operating systems Gain experience in • Multithreaded programming • Distributed system programming and design 1.4 Topics Introduction, Threads and Processes Inter-process Communication, Synchronization, Deadlocks CPU Scheduling Memory Management File and I/O Systems Protection and Security Distributed System Structures, Communication Distributed Coordination Fault Tolerance and Real-Time Computing 1.5 Lecture 1 (Ch 1, Ch 2) Introduction • What is an Operating System? • Co-evolution of Computer Systems and Operating System Concepts Computer System Structures Operating System Structures GMU – CS 571 1.6 What is an Operating System? A program that acts as an intermediary between the user of a computer and the computer hardware. Operating system goals • Convenience: Make the computer system convenient to use. • Efficiency: Manage the computer system resources in an efficient manner “Everything a vendor ships when you order an operating system” is good approximation • But varies wildly “The one program running at all times on the computer” is the kernel. Everything else is either a system program (ships with the operating system) or an application program GMU – CS 571 1.7 Computer System Components 1. Hardware – provides basic computing resources (CPU, memory, I/O devices). 2. Operating system – controls and coordinates the use of the hardware among the various application programs for the various users. 3. Application programs – define the ways in which the system resources are used to solve the computing problems of the users (compilers, database systems, video games, business programs). 4. Users – people, other computers GMU – CS 571 1.8 Computer System Components User 1 compiler User 2 User 3 assembler Text editor . . . Database system System and Application Programs Operating System Hardware GMU – CS 571 1.9 User n Operating System Definitions: System View Resource allocator – manages and allocates resources. Control program – controls the execution of user programs and operations of I/O devices. Kernel – the one program running at all times (all else being application programs). GMU – CS 571 1.10 Co-evolution of Computer Systems and Operating System Concepts Mainframe Systems • Batch Systems • Multi-programmed Systems • Time-sharing Systems Desktop Systems Modern Variants • Parallel Systems • Distributed Systems • Real-time and Embedded Systems • Handheld Systems GMU – CS 571 1.11 Operating Systems Evolution GMU – CS 571 1.12 Mainframe Systems First computers to tackle many commercial and scientific applications Mainframes evolved through batch, multiprogrammed and time- shared systems Early systems were afforded only by major government agencies or universities: • physically enormous machines run from a console. • the user submitted the job to the human operator in the form of • punched cards. The operator collects the output and returns it to the user. Batch systems: To speed up processing, operators batched together jobs with similar needs and ran them through the computer as a group. GMU – CS 571 1.13 Batch Systems GMU – CS 571 1.14 Multiprogrammed Systems Several (and not necessarily similar) jobs are kept in the main memory at the same time, and the CPU is switched to another job when I/O takes place. Objective: Avoid CPU idle time GMU – CS 571 1.15 OS Features Needed for Multiprogramming Job Scheduling – must choose the processes that will be brought to memory Memory Management – must allocate the memory to several jobs CPU Scheduling – must choose among several jobs ready to run OS/360, developed by IBM to run on its System/360 series, was the first multiprogrammed operating system. GMU – CS 571 1.16 Time-Sharing Systems: Interactive Computing Extension of multiprogrammed systems to allow on-line interaction with users. Each user is provided with an on-line terminal. Objective: Response time for each user should be short. The CPU is multiplexed among several jobs that are kept in memory and on disk. A job swapped in and out of memory to the disk. CPU is allocated to another job when I/O takes place. All active users must have a fair share of the CPU time (e.g. 10 ms for each user). GMU – CS 571 1.17 Desktop Systems Personal computers – computer system originally dedicated to a single user. I/O devices – keyboard, mouse, printers, … Objective: User convenience and responsiveness. • Individuals have sole use of computers • A single user may not need advanced features of mainframe OS (maximize utilization, protection). Today, may run several different types of operating systems (Windows, MacOS, Linux) GMU – CS 571 1.18 Parallel Systems Multiprocessor systems with more than one CPU in close communication. Tightly coupled system – processors share memory and a clock; communication usually takes place through the shared memory. Advantages of parallel system • Increased throughput • Economy of scale • Increased reliability graceful degradation fault-tolerant systems GMU – CS 571 1.19 Parallel Systems (Cont.) Symmetric multiprocessing (SMP) • Each processor runs an identical copy of the operating system. • All processors are peers Asymmetric multiprocessing GMU – CS 571 1.20 Distributed Systems Distribute the computation among several physical processors. Loosely coupled system – each processor has its own local memory; processors communicate with one another through various communications lines Advantages of distributed systems • Resource and Load Sharing • Reliability • Communications GMU – CS 571 1.21 Real-Time and Embedded Systems A real-time system is used when rigid time requirements have been placed on the operation of a processor or the flow of data. An embedded system is a component of a more complex system • Control of a nuclear plant • Missile guidance • Control of home and car appliances (microwave oven, DVD players, car engines, …) Real-time systems • have well-defined time constraints. • may be either hard or soft real-time. GMU – CS 571 1.22 Real-Time Systems (Cont.) Hard real-time • Critical tasks must be completed on time • Secondary storage limited or absent, data stored in short term memory, or read-only memory (ROM) Soft real-time • No absolute timing guarantees (e.g. “best-effort scheduling”) • Limited utility in industrial control of robotics • Useful in applications (multimedia, virtual reality) requiring advanced operating-system features. GMU – CS 571 1.23 Handheld Systems Personal Digital Assistants (PDAs) Cellular telephones Issues • Limited memory • Limited battery power • Slow processors • Small display screens GMU – CS 571 1.24 Computer-System Structures Computer System Organization Instruction Execution Computer System Operation Interrupt Processing Storage Structure Storage Hierarchy GMU – CS 571 1.25 Computer-System Organization GMU – CS 571 1.26 Instruction Execution While executing a program, the CPU: • fetches the next instruction from memory (loading into IR) • decodes it to determine its type and operands • executes it May take multiple clock cycles to execute an instruction Examples: • LOAD R1, #3 • LOAD R2, M2 • STORE M3, R4 • ADD R1, R2, R3 Each CPU has a specific set of instructions that it can execute (instruction-set architecture). GMU – CS 571 1.27 Instruction Execution Registers • General registers (data/address) • Program Counter (PC): contains the memory address of the next instruction to be fetched. • Stack Pointer (SP): points to the top of the current stack in memory. The stack contains one frame for each procedure that has been entered but not yet exited. • Program Status Word (PSW): contains the condition code bits and various other control bits. • … When time multiplexing the CPU, the operating system will often stop the running program to (re)start another one. In these cases, it must save the “state information” (e.g. values of the registers). GMU – CS 571 1.28 Computer-System Operation I/O devices and CPU can execute concurrently. Each device controller has local buffer(s). CPU moves data from/to main memory to/from local GMU – CS 571 buffers I/O is from/to the device to/from local buffer of controller. The device driver is special operating system software that interacts with the device controller. Typically, the device controller informs CPU that it has finished its operation by causing an interrupt. The Device Controller has an equivalent device driver through which the device controller communicates with the Operating Systems through Interrupts. 1.29 Classes of Interrupts I/O Interrupts: Generated by an I/O controller, to signal normal completion of an operation or to signal a variety of error conditions. Timer Interrupts: Generated by a timer within the processor. This allows the operating system to perform certain functions on a regular basis. Hardware Failure Interrupts: Generated by a failure (e.g. power failure or memory parity error). Traps (Software Interrupts): Generated by some condition that occurs as a result of an instruction execution • Errors • User request for an operating system service GMU – CS 571 1.30 Interrupt Mechanism Interrupt transfers control to the interrupt service routine generally through the interrupt vector which contains the addresses of all the service routines. Interrupt Service Routines (ISRs): Different segments of code determine what action should be taken for each type of interrupt. Once the interrupt has been serviced by the ISR, the control is returned to the interrupted program. Need to save the “process state” (registers, PC, …) before ISR takes over. A trap is a software-generated interrupt caused either by an error or a user request. Modern operating systems are interrupt-driven. GMU – CS 571 1.31 Interrupt Timeline (single process doing output) User process ISR move data from buffer to user process ----------------Data xfr GMU – CS 571 1.32 Basic Interrupt Processing 1. 2. 3. 4. 5. 6. The interrupt is issued Processor finishes execution of current instruction Processor signals acknowledgement of interrupt Processor pushes PSW (Program Status Word) and PC (Program Counter) onto control stack Processor loads new PC value through the interrupt vector ISR (Interrupt Service Routine) saves the process state information ISR executes ISR restores process state information Old PSW and PC values are restored from the control stack What if another interrupt occurs during interrupt processing? 7. 8. 9. Incoming interrupts are disabled while another interrupt is being processed to prevent a lost interrupt. GMU – CS 571 1.33 I/O Structure After I/O starts, control returns to user program only upon I/O completion. • Wait instruction idles the CPU until the next interrupt • Wait loop (contention for memory access). • At most one I/O request is outstanding at a time, no simultaneous I/O processing. After I/O starts, control returns to user program without waiting for I/O completion. • System call – request to the operating system to allow • • GMU – CS 571 user to wait for I/O completion. Device-status table contains entry for each I/O device indicating its type, address, and state. Operating system indexes into I/O device table to determine device status and to modify table entry to include interrupt. 1.34 Two I/O Methods Synchronous GMU – CS 571 Asynchronous 1.35 Device-Status Table GMU – CS 571 1.36 Direct Memory Access Structure 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. Only one interrupt is generated per block, rather than one interrupt per byte. GMU – CS 571 1.37 Dual-Mode Operation Operating System must protect itself and all other programs (and their data) from any malfunctioning program. Provide hardware support to differentiate between at least two modes of operations. 1. User mode – execution done on behalf of a user. 2. Kernel mode (also monitor mode or system mode) – execution done on behalf of operating system. GMU – CS 571 1.38 Dual-Mode Operation (Cont.) Mode bit added to computer hardware to indicate the current mode: kernel (0) or user (1). When an interrupt occurs hardware switches to kernel mode. Interrupt kernel user set user mode Privileged instructions can be issued only in kernel mode. GMU – CS 571 1.39 Transition From User to Kernel Mode The system call can be executed by a generic trap instruction (or in some systems, by an instruction such as syscall). GMU – CS 571 1.40 Storage Structure Main memory – only large storage media that the CPU can access directly. Secondary storage – extension of main memory that provides large nonvolatile storage capacity. Magnetic disks – rigid metal or glass platters covered with magnetic recording material • Disk surface is logically divided into tracks, which are • GMU – CS 571 subdivided into sectors. The disk controller determines the logical interaction between the device and the computer. 1.41 Memory Protection GMU – CS 571 1.42 Storage Hierarchy Storage systems organized in hierarchy • Speed • Cost • Volatility Faster access time, greater cost per bit Greater capacity, lower cost per bit Greater capacity, slower access speed GMU – CS 571 1.43 Storage-Device Hierarchy GMU – CS 571 1.44 Performance of Various Levels of Storage Movement between levels of storage hierarchy can be explicit or implicit GMU – CS 571 1.45 Caching Important principle, performed at many levels in a computer (in hardware, operating system, software) Information in use copied from slower to faster storage temporarily Faster storage (cache) checked first to determine if information is there • If it is, information used directly from the cache (fast) • If not, data copied to cache and used there Cache smaller than storage being cached • Cache management important design problem • Cache size and replacement policy GMU – CS 571 1.46 Migration of Integer A from Disk to Register Multitasking environments must be careful to use most recent value, no matter where it is stored in the storage hierarchy Multiprocessor environment must provide cache coherency in hardware such that all CPUs have the most recent value in their cache Distributed environment situation even more complex GMU – CS 571 • Several copies of a datum can exist • Various solutions covered in Chapter 17 1.47 Operating-System Structures System Components • Process Management • Main Memory Management • File Management • Secondary-Storage Management • I/O System Management • Protection and Security • User Operating-System Interface System Calls System Programs Operating System Design Approaches GMU – CS 571 1.48 Process Management A process is a program in execution. It is a unit of work within the system. Program is a passive entity, process is an active entity. Process needs resources to accomplish its task • CPU, memory, I/O, files • Initialization data Process termination requires reclaim of any reusable resources Single-threaded process has one program counter specifying location of next instruction to execute • Process executes instructions sequentially, one at a time, until completion Multi-threaded process has one program counter per thread Typically system has many processes, some user, some operating system running concurrently on one or more CPUs • Concurrency by multiplexing the CPUs among the processes / threads GMU – CS 571 1.49 Process Management A process tree A created two child processes, B and C B created three child processes, D, E, and F GMU – CS 571 1.50 Process Management The operating system is responsible for the following activities in connection with process management: Creating and deleting both user and system processes Suspending and resuming processes Providing mechanisms for process synchronization Providing mechanisms for process communication Providing mechanisms for deadlock handling GMU – CS 571 1.51 Memory Management All data in memory before and after processing All instructions in memory in order to execute Memory management determines what is in memory when • Optimizing CPU utilization and computer response to users Memory management activities • Keeping track of which parts of memory are currently being used and by whom • Deciding which processes (or parts thereof) and data to move into and out of memory • Allocating and deallocating memory space as needed GMU – CS 571 1.52 Storage Management OS provides uniform, logical view of information storage • Abstracts physical properties to logical storage unit - file • Each medium is controlled by device (i.e., disk drive, tape drive) Varying properties include access speed, capacity, datatransfer rate, access method (sequential or random) File-System management • Files usually organized into directories • Access control on most systems to determine who can access what • OS activities include Creating and deleting files and directories Primitives to manipulate files and dirs Mapping files onto secondary storage Backup files onto stable (non-volatile) storage media GMU – CS 571 1.53 File Management A file is a collection of related information defined by its creator Commonly, files represent programs (both source and object forms) and data The operating system responsibilities • File creation and deletion • Directory creation and deletion • Support of primitives for manipulating files and • • GMU – CS 571 directories Mapping files onto secondary storage File backup on stable (non-volatile) storage media 1.54 File Systems Management GMU – CS 571 1.55 Secondary-Storage Management The secondary storage backs up main memory and provides additional storage. Most common secondary storage type: disks The operating system is responsible for • Free space management • Storage allocation • Disk scheduling GMU – CS 571 1.56 Mass-Storage Management Usually disks used to store data that does not fit in main memory or data that must be kept for a “long” period of time. Proper management is of central importance Entire speed of computer operation hinges on disk subsystem and its algorithms OS activities • Free-space management • Storage allocation • Disk scheduling Some storage need not be fast • Tertiary storage includes optical storage, magnetic tape • Still must be managed • Varies between WORM (write-once, read-many-times) and RW (read-write) GMU – CS 571 1.57 I/O System Management The Operating System will hide the peculiarities of specific hardware from the user. In Unix, the I/O subsystem consists of: • A buffering, caching and spooling system • A general device-driver interface • Drivers for specific hardware devices Interrupt handlers and device drivers are crucial in the design of efficient I/O subsystems. GMU – CS 571 1.58 I/O Subsystem One purpose of OS is to hide peculiarities of hardware devices from the user I/O subsystem responsible for • Memory management of I/O including buffering (storing • • GMU – CS 571 data temporarily while it is being transferred), caching (storing parts of data in faster storage for performance), spooling (the overlapping of output of one job with input of other jobs) General device-driver interface Drivers for specific hardware devices 1.59 Protection and Security Protection – any mechanism for controlling access of processes or users to resources defined by the OS Security – defense of the system against internal and external attacks • Huge range, including denial-of-service, worms, viruses, identity theft, theft of service Systems generally first distinguish among users, to determine who can do what • User identities (user IDs, security IDs) include name and • • • GMU – CS 571 associated number, one per user User ID then associated with all files, processes of that user to determine access control Group identifier (group ID) allows set of users to be defined and controls managed, then also associated with each process, file Privilege escalation allows user to change to effective ID with more rights 1.60 User Operating-System Interface Two main approaches • Command-line interpreter (a.k.a command interpreter, or shell) • Graphical User Interfaces (GUI) The shell • allows users to directly enter commands that are to be performed by the operating system • is usually a system program (not part of the kernel) GUI allows a mouse-based window-and-menu system Some systems allow both (e.g. X-Windows in Unix) GMU – CS 571 1.61 System Calls System calls provide the interface between a running program and the operating system. • Generally available in routines written in C and C++ • Certain low-level tasks may have to be written using assembly language. Typically, application programmers design programs using an application programming interface (API). The run-time support system (run-time libraries) provides a system-call interface, that intercepts function calls in the API and invokes the necessary system call within the operating system. GMU – CS 571 1.62 Example System-Call Processing GMU – CS 571 1.63 Major System Calls in Unix : Process Management pid = fork() • Create a child process identical to the parent pid = waitpid( pid, &statloc, options) • Wait for a child to terminate s = execve(name, argv, environp) • Replace a process’ core image exit(status) • Terminate process execution and return status s = kill (pid, signal) • Send a signal to a process GMU – CS 571 1.64 Major System Calls in Unix : File Management fd = open (file, how, …) • Open a file for reading, writing or both s = close (fd) • Close an open file n = read (fd, buffer, nbytes) • Read data from a file into a buffer n = write (fd, buffer, nbytes) • Write data from a buffer into a file position = lseek(fd, offset, whence) • Move the file pointer s = stat(name, &buf) • Get a file’s status information GMU – CS 571 1.65 Major System Calls in Unix : Directory and File System Management s = mkdir(name, mode) • Create a new directory s = rmdir (name) • Remove an empty directory s = link (name1, name2) • Create a new directory, name2, pointing to name1 s = unlink (name) • Remove a directory entry s = mount (special, name, flag) • Mount a file system s = umount(special) • Unmount a file system GMU – CS 571 1.66 System Programs System programs provide a convenient environment for program development. They can provide various services • • • • • Status information File modification Programming language support Program loading and execution Communications Most users’ view of the operating system is defined by system programs, not by the actual system calls. GMU – CS 571 1.67 Operating System Design Approaches Simple Structure Layered Approach Microkernels Modular Approach Virtual Machines -- (July 6, 2009) GMU – CS 571 1.68 Simple System Structure Some operating systems do not have well-defined structures. Often, these started as simple systems and grew beyond their original scope. MS-DOS – written to provide the most functionality in the least space • not divided into modules • Although MS-DOS has some structure, its interfaces and levels of functionality are not well separated GMU – CS 571 1.69 MS-DOS Structure GMU – CS 571 1.70 UNIX System Structure UNIX – limited by hardware functionality, the original UNIX operating system had limited structure. The UNIX OS consists of two separable parts. • System programs • The kernel (everything below the system-call interface and above the physical hardware) Provides the file system, CPU scheduling, memory management, and other operating-system functions A large number of functions for one level. GMU – CS 571 1.71 UNIX System Structure GMU – CS 571 1.72 Layered Approach The operating system is divided into a number of layers (levels), each built on top of lower layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface. With modularity, layers are selected such that each uses functions (operations) and services of only lower-level layers. Simplifies debugging and system verification Disadvantages? GMU – CS 571 1.73 Microkernels Moves as much as possible from the kernel into the “user” space. Communication takes place between user modules using message passing (e.g. Mach operating system) GMU – CS 571 1.74 Microkernels (cont.) Benefits • easier to extend • more reliable (less code is running in kernel mode) • convenient for distributed architectures Disadvantages • Increased system function overhead (message passing) GMU – CS 571 1.75 Modular Approach Modular kernel • The kernel has a set of core components • Dynamically links in additional services either during boot time or during run-time • Common in modern implementations of Unix such as Linux and Solaris GMU – CS 571 1.76 Virtual Machines Originally proposed and implemented for VM Operating System (IBM) A virtual machine provides an interface identical to the underlying bare hardware Each user is given its own virtual machine The operating system creates the illusion of multiple processes, each executing on its own processor with its own (virtual) memory GMU – CS 571 1.77 Virtual Machines (Cont.) Non-virtual Machine GMU – CS 571 Virtual Machine 1.78 Virtual Machines (Evaluation) The virtual-machine concept provides complete protection (because of complete isolation). This isolation, however, permits no direct sharing of resources. A virtual-machine system is a perfect vehicle for operating-systems research and development. The virtual machine concept is difficult to implement due to the effort required to provide an exact duplicate of the underlying machine. GMU – CS 571 1.79 VMware Abstracts Intel 80x86 hardware into multiple virtual machines VMware is an application running on top of a host operating system (e.g. Windows or Linux). Several different guest operating systems (e.g. Free BSD, Windows NT, Windows XP) can run on top of VMware GMU – CS 571 1.80 Java Virtual Machine Compiled Java programs are platform-neutral bytecodes executed by a Java Virtual Machine (JVM). JVM consists of - class loader - class verifier - runtime interpreter Just-In-Time (JIT) compilers increase performance GMU – CS 571 1.81