1 Lecture 3: OS Functions and Design Approaches OS duties process management memory management disk/file management protection & security interaction with OS dual-mode operation system calls API, system programs, UI OS design approaches: monolithic kernel microkernel virtual machine
2 Process Management OS manages many kinds of activities: user programs system programs: printer spoolers, name servers, file servers, etc. a running program is called a process a process includes the complete execution context (code, data, PC, registers, OS resources in use, etc.) a process is not a program F program - a sequence of instructions (passive) F process - one instance of a program in execution (active); many processes can be running the same program and one program may cause to create multiple processes from OS viewpoint process is a unit of work; OS must: create, delete, suspend, resume, and schedule processes support inter-process communication and synchronization, handle deadlocks
3 Memory Management primary (main) memory (RAM) provides direct access storage for CPU processes must be in main memory to execute OS must: mechanics F keep track of memory in use F keep track of unused (“free”) memory F protect memory space F allocate, deallocate space for processes F swap processes: memory disk policies F decide when to load each process into memory F decide how much memory space to allocate to each process F decide when a process should be removed from memory
4 Disk Management the size of the disk is much greater than main memory and, unlike main memory, disk is persistent (survives system failures and power outages) OS hides peculiarities of disk usage by managing disk space at low level: keeps track of used spaces keeps track of unused (free) space keeps track of “bad blocks” OS handles low-level disk functions, such as: schedules of disk operations and head movement
5 File Management disks provide long-term storage, but are awkward to use directly file - a logical named persistent collection of data maintained by OS file system - a logical structure that that is maintained by OS to simplify file manipulation; usually directory based OS must: create and delete files and directories manipulate files and directories - read, write, extend, rename, copy, protect provide general higher-level services - backups, accounting, quotas note the difference between disk management and file system management
6 Protection & Security protection – any mechanism for controlling access of processes or users to resources disk, memory, CPU, security – defense of the system against internal and external attacks systems generally first distinguish among users, to determine who can do what F user identities and privileges associated with this identifier F user ID then associated with all files, processes of that user to determine access control
7 Dual-Mode Operation to allow protection OS operates in dual-mode user mode and kernel mode mode bit provided by hardware user (1), kernel (0) enables OS to distinguish when system is running user code or kernel code some instructions designated as privileged, only executable in kernel mode changing modes modifying timers modifying interrupt service routines I/O device access
8 System Call Definition app. program can ask the OS to carry out service for it by invoking a system call to the application the invocation is similar to an ordinary function call example: Unix write system call description prompt% man -S 2 write WRITE(2) Linux Programmer's Manual WRITE(2) NAME write - write to a file descriptor SYNOPSIS #include ssize_t write(int fd, const void *buf, size_t count); DESCRIPTION write() writes up to count bytes to the file referenced by the file descriptor fd from the buffer starting at buf. … RETURN VALUE On success, the number of bytes written are returned …
9 Mode Switch mode switch – transition from user to kernel mode system call generates a trap (software generated interrupt) physical interrupt occurs (e.g. a timer interrupt) control is transferred to the interrupt service routine, which may pass control to the OS (in kernel mode) return it back to the user process in case OS needs to do extensive work, it needs to save the context (state) of the user program
10 Function Invocation Trace Across Modes Example function invocation in Solaris (Unix-like OS from Sun Microsystems) node the mode (User or Kernel) for function invocation
11 System Call Example a typical app. program invokes system calls repeatedly example: copying a file
12 Application Program Interface “raw” system calls tend to be difficult to use application program interface (API) defines functions that are more convenient to use API functions can be invoked by app. programmer run in user mode directly invoke system calls implemented in system libraries that come with the OS and are linked to the app. program API examples: POSIX API (implemented by most Unix systems, MacOS X), Java API, Win 32 API – windows ex: printf (C function that prints formatted output, part of POSIX API) repeatedly invokes write
13 System Programs and UIs system programs come with the OS they are designed for end users – the person for whom the computer/OS/app. programs are designed F cf. application programmers, OS programmers operate in user mode typical tasks: file manipulation, status info, file modification (editors), programming language support (compilers/assemblers), configuration, communication, etc. user interface (UI) is a way the end-user interacts with system programs (and through them with the OS), F command-line interface (CLI) – interaction through command interpreter (shell) – a text-based system program F graphical-user interface (GUI) – mouse-heavy with a lot of graphics most popular OSes support both kinds of UI
14 Monolithic Kernel OS Design advantages: speed and ease of operation (everything is at hand) disadvantages: hard to develop, maintain, modify and debug kernel gets bigger as the OS develops critical OS data structures and device registers are protected from user programs system processes system calls user processes device controllers signals terminals character I/O files swapping disk, tape CPU scheduling page replacement virtual memory terminal drivers device drivers memory drivers kernel machine- independent machine - dependent hardware app. processes commands/interrupts kernel mode user mode
15 Modular Kernel classic monolithic kernel requires all functionality to be inbuilt into kernel and loaded at boot-time – waste of RAM, limited flexibility loadable kernel module – object file to be loaded at boot time or, possibly compile time extends base kernel functionality, common functions of kernel modules F device drivers/bus drivers, filesystems, system calls, CPU scheduling classes, executable file formats loaded to kernel address space has full access to all kernel memory has defined programming interface, possibly protected Linuxes, Windows, Solaris, Mac OS X support kernel modules
16 Microkernel advantages: reliability, ease of development, modularity - parts can be replaced and tailored to the architecture, user requirements etc. disadvantages: slow? examples: MacOS X, Windows XP small kernel implements communication (usually messages) when system services are required microkernell calls other parts of OS running in user modes and passes the request there user processes file system CPU scheduling thread system network support paging system processes micro- kernel user mode kernel mode communication protection low-level VM processor control device controllers hardware commands/interrupts
17 Virtual Machine OS’s system calls are considered an enhancement of hardware’s instruction set extend further – virtual machine each user task is provided with an abstract (virtual machine) which OS + hardware implement IBM – pioneered modern examples: F Java VM – next slide F VMware Player – extra guest/host bit, guest OS executes limited set of instructions, if different instruction – trap to host OS adv. – portability at binary-level, security, greater language flexibility dis. – speed(?)
18 Java Virtual Machine (JVM) Java source code is translated into an architecture independent java bytecode bytecode is executed by JVM JVM can be implemented purely in software or in hardware JVM verifies bytecode’s correctness and then either interprets (translates the code into machines instructions one by one) or just-in-time (JIT) compiles to optimize, or both adv. – portability at binary-level, security, greater language flexibility dis. – speed(?)
19 Lecture Review major OS duties are process management memory management disk/file management OS interacts with users through system calls, to ease interaction API - for app. programmers GUI or CLI for end-users OS is a big and complex program; traditional monolithic kernel design approach yields OSes that are fast but hard to develop, modify and debug; other approaches have been suggested: microkernel virtual machine
20 Future: network computers? JavaOS no disk OS is only capable of running Java Virtual Machine all the programs (including OS components) are downloaded over the network advantages: ease of administration and rollout (installation), platform independence disadvantages: ??
21 Monolithic Kernel OS Design advantages: speed and ease of operation (everything is at hand) disadvantages: hard to develop, maintain, modify and debug kernel gets bigger as the OS develops critical OS data structures and device registers are protected from user programs can use privileged instructions system services: shells, compilers, printing, network access system calls user programs hardware: terminals, I/O devices, memory signals terminals character I/O files swapping disk, tape CPU scheduling page replacement virtual memory terminal controllers device controllers memory controllers kernel machine- independent machine - dependent
22 Layered Design divide OS into layers each layer uses services provided by next lower layer yet the implementation of these services are hidden from the upper layer THE Operating system layer structure: user programs buffering for input and output devices operator-console device driver memory management CPU scheduling hardware advantages: easier development and implementation disadvantages: not always easy to break down on layers, slower (each level adds overhead) ex: CPU scheduler is lower than virtual memory driver (driver may need to wait for I/O) yet the scheduler may have more info than can fit in memory examples: THE, OS/2