Advanced Operating Systems Mehdi Naghavi naghavi@iust.ac.ir Winter 1385 Course motivation and goals 66/2 Programming computer hardware directly is difficult Operating systems provide a layer between applications and computer hardware (VM) Understanding operating system concepts is essential to many advanced programming tasks Class outline 66/3 Introduction and Overview Processes and threads Operating system structures Memory management Scheduling Input/Output File Systems Security Textbook Andrew S. Tanenbaum. “Operating Systems design and implementation”, 3nd ed., Prentice Hall, 2006. Abraham Silberschatz, Galvin, and Gagne. “Operating Systems Concepts”, 7nd ed., John Wiley and Sons, 2005 William Stallings. “Operating Systems”, 4nd ed., Prentice Hall, 2001. Andrew S. Tanenbaum. “Modern Operating Systems”, 2nd ed., Prentice Hall, 2001. Andrew S. Tanenbaum. “Distributed Operating Systems”, Prentice Hall. 66/4 Points Mobile Phones, pagers and other similar devices OFF during class Collegians must be Presence in classroom Contact 66/5 naghavi@iust.ac.ir naghavi@aut.ac.ir Operating Systems Introduction to Operating Systems Mehdi Naghavi naghavi@aut.ac.ir Winter 1385 Overview: What is an operating system? Operating systems history The zoo of modern operating systems Review of computer hardware Operating system concepts System calls Operating system structure 66/7 User interface to the operating system Anatomy of a system call Samples of Operating Systems 66/8 IBSYS (IBM 7090) OS/360 (IBM 360) TSS/360 (360 mod 67) Michigan Terminal System CP/CMS & VM 370 MULTICS (GE 645) Alto (Xerox PARC) Pilot (Xerox STAR) CP/M IRIX Solaris MVS VxWorks MACH Apollo DOMAIN Unix (System V & BSD) Apple Mac (v. 1– v. 9) MS-DOS Windows NT, 2000, XP Novell Netware Linux FreeBSD PalmOS PocketPC VxWorks Samples of Operating Systems (continue…) 66/9 What is an Operating System? An Operating System is a program that acts as an intermediary/interface between a user of a computer and the computer hardware. It is an extended machine It is a resource manager 66/10 Hides the messy details which must be performed Presents user with a virtual machine, easier to use Each program gets time with the resource Each program gets space on the resource The Operating System controls the machine Compiler User Game Application Operating System Hardware 66/11 Editor Debugger OS Kernel Video Hard ware App. … Terminal emulator Browser Static View of System Components User 1 User 2 User 3 User 4 Compiler Editor Database Calculator Operating System Hardware 66/12 … User n WP Dynamic View of System Components 66/13 Layers of a Computer System End User Programmer Application Programs Utilities Operating-System Computer Hardware 66/14 OperatingSystem Designer History of Operating Systems First generation: 1945 – 1955 Second generation: 1955 – 1965 Integrated circuits, Multiprogramming Fourth generation: 1980 – present Transistors, Batch systems Third generation: 1965 – 1980 Vacuum tubes, Plug boards Large scale integration, Personal computers Next generation: ??? 66/15 Systems connected by high-speed networks? Wide area resource management? First generation: direct input Run one job at a time Problem: lots of wasted computer time! 66/16 Enter it into the computer (might require rewiring!) Run it Record the results Computer was idle during first and last steps Computers were very expensive! Goal: make better use of an expensive commodity: computer time Second generation: batch systems 66/17 Bring cards to 1401 Read cards onto input tape Put input tape on 7094 Perform the computation, writing results to output tape Put output tape on 1401, which prints output Structure of a typical 2nd generation job Data for program FORTRAN program $RUN $LOAD $FORTRAN $JOB, 10,6610802, JANE DOE 66/18 $END Third generation: multiprogramming Multiple jobs in memory Memory partitions Job 3 Job 2 Job 1 Operating system protected from each job as well Resources (time, hardware) split between jobs Still not interactive Operating system 66/19 Protected from one another User submits job Computer runs it User gets results minutes (hours, days) later Multi tasking Task 1 Job 3 Resource utilization 66/20 Multiprogramming, multitasking, and multiple users. Time sharing. Response time. CPU Job 2 Main Memory Task 2 Operating System Job 1 Timesharing Multiprogramming allowed several jobs to be active at one time Computer use got much cheaper and easier 66/21 Initially used for batch systems Cheaper hardware terminals → interactive use No more “priesthood” Quick turnaround meant quick fixes for problems Types of modern operating systems Mainframe operating systems: MVS Server operating systems: FreeBSD, Solaris Multiprocessor operating systems: Cellular IRIX Personal computer operating systems: XP, Linux PDA operating systems: PalmOS, PocketPC Real-time/embedded operating systems: VxWorks, QNX Smart card operating systems Some operating systems can fit into more than one category 66/22 Computer Hardware Review 66/23 Computer block diagram Components of a simple personal computer CPU internals Memory Storage pyramid Disk drive structure Anatomy of a device request Structure of a large Pentium system Computer Input Unit Control Unit Output Unit Keyboard Mouse Scanner Modem ALU CPU Monitor Printer Modem Registers Bank Cache Memory Main RAMMemory ROM Memory Flash HDSecondary FDMemory CD Tertiary Memory Tape 66/24 Computer Components: Top-Level View MAR - Memory Address Register address MBR for next read or write - Memory Buffer Register data to be written into memory receives I/OAR - I/O Address specifies I/OBR data read from memory a particular I/O device - I/O Buffer exchange of data between an I/O module and the processor 66/25 Components of a simple personal computer Outside world Video controller CPU Memory 66/26 Hard drive controller USB controller Network controller Computer internals (inside the “box”) CPU internals Fetch unit Fetch unit Decode unit Holding buffer Fetch unit 66/27 Decode unit Execute unit (a) A three-stage Pipelined CPU Execute unit Decode unit (b) A Superscalar CPU Execute unit Execute unit Memory Address 0x0002ffff 0x0002b000 Address User 2 program and data 0x00027fff User 1 program and data 0x00023000 0x0001dfff Limit 0x0002ffff 0x0002d000 User 2 data 0x0002bfff 0x00029000 User 1 data 0x00024fff Base 0x00023000 0x0001dfff Operating system 0x00000000 66/28 User program Operating system 0x00000000 Single base/limit pair: set for each process Two base/limit registers: one for program, one for data Limit2 Base2 Limit1 Base1 Storage pyramid Capacity Better Access latency < 1 KB Registers 1 ns 1 MB Cache (SRAM) 2–5 ns 1 GB Main memory (DRAM) 50 ns 200 GB Magnetic disk 5 ms > 1 TB Magnetic tape 50 sec Goal: really large memory with very low latency 66/29 Better Latencies are smaller at the top of the hierarchy Capacities are larger at the bottom of the hierarchy Solution: move data between levels to create illusion of large memory with low latency Disk drive structure Data stored on surfaces Up to two surfaces per platter One or more platters per disk Data in concentric tracks Tracks broken into sectors 256B-1KB per sector Cylinder: corresponding tracks on all surfaces Data read and written by heads Actuator moves heads Heads move in unison head sector platter track cylinder surfaces spindle 66/30 actuator Anatomy of a device request 3 CPU 1 5 Interrupt controller 6 66/31 1: Interrupt Disk controller 4 Left: sequence as seen by hardware 2 Instructionn Instructionn+1 Operating system Interrupt handler 3: Return 2: Process interrupt Request sent to controller, then to disk Disk responds, signals disk controller which tells interrupt controller Interrupt controller notifies CPU Right: interrupt handling (software point of view) Structure of a large Pentium system 66/32 Operating system Components 66/33 Process Management Memory Management File Management I/O Management Process Scheduler Inter Process Communication Network Unit Command Interpreter Security Management Modern Operating System Structure Application Application Application Shared Runtime Libraries user-mode supervisor-mode System Call Interface memory manager task manager file manager network manager Device Driver Components OS Kernel Hardware 66/34 Processes Process: program in execution A B C D E F G OS keeps track of all processes in a process table Processes can create other processes 66/35 Address space (memory) the program can use State (registers, including program counter & stack pointer) Process tree tracks these relationships A is the root of the tree A created three child processes: B, C, and D C created two child processes: E and F D created one child process: G Memory image of a Unix process 0x7fffffff Stack Processes have three segments Text: program code Data: program data 0x00000000 66/36 Automatic variables Procedure call information Address space growth Text Stack Data Data Statically declared variables Areas allocated by malloc() or new (heap) Text: doesn’t grow Data: grows “up” Stack: grows “down” Synchronization and deadlock (a) A Potential deadlock 66/37 (b) An Actual deadlock Hierarchical file systems File system for a university department 66/38 Mounting Before mounting, files on floppy are inaccessible After mounting floppy on b, 66/39 files on floppy are part of file hierarchy Inter-process communication Processes may want to exchange information with each other Many ways to do this, including Network Pipe (special file): A writes into pipe, and B reads from it Process Process Pipe A B Two processes connected by a pipe 66/40 Types of OS structures 66/41 Monolithic system Layered system Virtual machine Exokernel Client/Server Monolithic system Main procedure Service Procedures Utility Procedures Simple structuring model for a monolithic system 66/42 Monolithic system… Applications User space Kernel space System call interface MM = Memory Manager PS = Processor Scheduler IPC = Inter Process Communication Kernel MM PS IPC FS FS = File System I/O = Input/Output manager I/O 66/43 Net … Net = Network manager MS-DOS Layer Structure 66/44 Layered Operating System 66/45 Layered system User space Kernel space User Layer 4 User Application Layer 3 I/O Management Layer 2 Message Interpreter Layer 1 Memory Management Layer 0 Process Scheduling Hardware Layer 5 Structure of the THE operating system 66/46 UNIX System Structure 66/47 Virtual machine App1 App2 App3 System calls I/O instructions Calls to simulate I/O “Real” I/O instructions CMS (VMW) CMS (VMW) CMS (VMW) VM/370 Bare hardware Allows users to run any x86-based OS on top of Linux or NT Only virtual machine fails—rest of underlying OS is fine “Guest” OS can even use raw hardware 66/48 FreeBSD “Guest” OS can crash without harming underlying OS Windows NT First widely used in VM/370 with CMS (Conversational Monitor System) Available today in VMware Linux Virtual machine keeps things separated VMware Architecture 66/49 The Java Virtual Machine 66/50 Exokernel 66/51 Same as Virtual machine Assign one virtual computer to any user Allocate sub set of resource to any virtual machine Client/Server Client process Client process Process server Terminal server … File server Microkernel The client-server model User mode Kernel mode Client obtains service by sending messages to server processes Processes (clients and OS servers) don’t share memory Memory server Communication via message-passing Separation reduces risk of “byzantine” failures Examples include Mach, QNX, early versions of Windows NT 66/52 Client/Server… Application s System call interface File System Process Scheduler Device manager … User space Kernel space IPC Memory Management 66/53 Micro Kernel Synchronization Mac OS X Structure 66/54 Client/Server… The client-server model in a distributed system 66/55 System calls Programs want the OS to perform a service Access a file Create a process Others… Accomplished by system call Program passes relevant information to OS OS performs the service if OS checks information passed to make sure it’s OK 66/56 The OS is able to do so The service is permitted for this program at this time Don’t want programs reading data into other programs’ memory! System calls… Applications Applications Applications User space Kernel space System call interface OS Component 66/57 MM PS I/O Net IPC FS … Making a system call 0xffffffff Library (read call) read(fd,buffer,length) Return to caller Trap to kernel 3 Trap code in register User space 8 2 4 Increment SP 9 7 Call read 1 Push arguments Kernel space (OS) Dispatch 0 66/58 5 6 Sys call handler User code System call: Program pushes arguments, calls library Library sets up trap, calls OS OS handles system call Control returns to library Library returns to user program There are 9 steps in making the system call System calls for files & directories Call Description fd = open(name,how) Open a file for reading and/or writing s = close(fd) Close an open file n = read(fd,buffer,size) Read data from a file into a buffer n = write(fd,buffer,size) Write data from a buffer into a file s = lseek(fd,offset,whence) Move the “current” pointer for a file s = stat(name,&buffer) Get a file’s status information (in buffer) MINIX System calls 66/59 More system calls Call Description Create a child process identical to the parent pid = fork() 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 = chdir(dirname) Change the working directory s = chmod(name,mode) Change a file’s protection bits s = kill(pid,signal) Send a signal to a process seconds = time(&seconds) Get the elapsed time since 1 Jan 1970 MINIX System calls 66/60 A simple shell while (TRUE) { /* repeat forever */ type_prompt( ) ; /* display prompt */ read_command (command, parameters); /* input from terminal */ if (fork() != 0) { /* Parent code */ waitpid( -1, &status, 0); } else { /* Child code */ execve (command, parameters, 0); } } 66/61 /* fork off child process */ /* wait for child to exit */ /* execute command */ System Calls Some UNIX and Win32 API calls 66/62 Metric units Number Exp. Prefix Exp. Number Prefix 10-3 0.001 milli 103 1,000 10-6 0.000001 micro 106 1,000,000 10-9 0.000000001 nano 109 1,000,000,000 Giga 10-12 0.000000000001 pico 1012 1,000,000,000,000 Tera 10-15 0.000000000000001 femto 1015 1,000,000,000,000,000 Peta 10-18 0.000000000000000001 atto 1018 1,000,000,000,000,000,000 Exa 66/63 Kilo Mega Review 66/64 How does a batch operating system differ from a multiprogrammed operating system? Why is multiprogramming useful? Why are multiprogrammed systems much more difficult to implement than are batch systems? What is timesharing? How does it differ from multiprogramming? What is an online system? What is a real-time system? Which systems try to maximize throughput? Which ones try to minimize response time? What type of operating system would you use 66/65 For editing a letter? For controlling a chemical reaction? For processing payroll? In a cellphone? In a toy? For surfing the web? For air traffic control? For a scientific simulation? To play MP3? Computer architecture 66/66 What connects the processor to memory? What are the components of the storage hierarchy? What is between the processor and I/O devices? What are the components of a disk? What is an interrupt? Why are interrupts useful? What happens to program execution when an interrupt occurs? OS interaction 66/67 Does it differ if the operating system is monolithic or a microkernel? What are the steps involved when the operating system is monolithic? What additional steps happen in case of a microkernel?