Lecture1: OS Overview CS 5523: Operating Systems Instructor: Dr Tongping Liu Midterm Exam: Oct 2, 2017, Monday 7:20pm 8:45pm Operating System: what is it?! Evolution of Computer Systems and OS Concepts Different types/variations of Systems/OS Ø Parallel/distributed/real-time/embedded OS etc OS as a resource manager Ø How does OS provide service? interrupt/system calls OS Structures and basic components Ø Process/memory/IO device managers Basic design approaches Ø Monolithic/layered/microkernel/virtual machine etc CS5523: Operating Systems @ UTSA 1 CS5523: Operating Systems @ UTSA 2 Components in Operating System Process/thread Management Ø CPU (processors): most precious resource Memory Management Ø Main memory File Management à data /program Secondary-Storage Management à disk I/O System Management à I/O devices Protection and Security à access management I/O devices 3 OS Interface: APIs and System Calls For application programmers 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 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 4 1
Types of System Calls Process control" File management" Device management" Information maintenance" Communications" Protection" " Interrupt Mechanisms Save the current process state Interrupt transfers control to the interrupt service routine (ISR) generally through interrupt vector containing the addresses of all the service routines ISR: Separate 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 6 Basic Interrupt Processing 1 The interrupt is issued 2 Processor finishes execution of current instruction 3 Processor signals acknowledgement of interrupt 4 Processor pushes PSW(Program Status Word) and PC to control stack 5 Processor loads new PC value through the interrupt vector 6 ISR saves remainder of the process state information 7 ISR executes 8 ISR restores process state information 7 9 Old PSW and PC values are restored from the control stack Lecture2: Process Management Basic concepts of process Ø Process control block (PCB) and address space Basic operations for process management Ø Process creation/termination States of process: different queues Ø ready, running, or wait etc Ø Context switch: multiple hardware running contexts Scheduling of process: CPU scheduling Ø Basic scheduling algorithms: FIFO, SJF etc Inter process communication Ø shared memory and message CS5523: Operating Systems @ UTSA 8 2
Process vs Program Program: a set of functions Ø a passive entity Ø stored as files on disk Process: a program in execution Ø Dynamic concept: running of a program How do we run a program? What are steps to create a process? Process: Running Context Registers: in addition to general registers Ø Program Counter (PC): contains the memory address of the next instruction to be executed Ø 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): is an IBM System/360 architecture and successors control register, which performs the function of a Status register and Program counter in other architectures Higher level resources: open files etc Synchronization and communication resource: semaphores and sockets 9 10 Example: PCB in Linux (task_struct) Context Switch Process Management Registers Program Counter Stack Pointers Process State Priority Scheduling Parameters (slice) Process ID Parent process Process group Time when process started CPU time used Memory Management Pointer to text (code) segment Pointer to data segment Pointer to stack segment File Management Root directory Working directory User Id Group Id List of open files When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch! Context of a process represented in the PCB" Context-switch time is overhead" Ø 1 ~ 1000 ms" Hardware support" Ø Multiple set of registers" Other performance issues/problems" Ø Cache content: locality is lost" Ø TLB content: may need to flush" 11 3
Process Creation An Example: Unix fork( ) Address space" Ø Child duplicate its parent (data, program)" Ø Child has a program loaded into it (exec())" Execution" Ø Parent and children execute concurrently" Ø Parent waits until children terminate" Resource sharing: complicated" Ø Shared open files, but not descriptors" Ø Different page tables" Text pid = 25 Data Stack Process Status < > int cpid = fork( ); if (cpid = = 0) { <child code> exit(0); } <parent code> wait(cpid); File File Resources Resources Text pid = 26 Data Stack Process Status cpid = 26 < > int cpid = fork( ); if (cpid = = 0) { <child code> exit(0); } <parent code> wait(cpid); cpid = 0 UNIX kernel 14 Activities in Processes Bursts of CPU usage alternate with periods of I/O wait CPU-bound: high CPU utilization, interrupts are processed slowly I/O-bound: more time is spending on requesting data than processing it Process 1: CPU bound" Process 2:" I/O bound" Time" CPU bursts" Total CPU usage" I/O waits" Total CPU usage" 15 Classical Scheduling Algorithms FCFS: non-preemptive, based on arrival time Ø Long waiting time, eg long process before SSH console? SJF(shortest job first): preemptive & non-preemptive Ø Optimal in term of waiting time RR (Round-robin): preemptive Ø Processes take turns with fixed time quantum eg, 10ms Priority-based scheduling Ø Real-time systems: earliest deadline first (EDF) Multi-level queue (priority classes) Ø System processes >faculty processes >student processes Multi-level feedback queues: short à long quantum 16 4
Inter-Process Communication (IPC) Processes within a system may be independent or cooperating! Cooperating process can affect or be affected by other processes" Reasons for cooperating processes:" Ø Information sharing, eg, sharing a file" Ø Computation speedup, eg, subtasks for parallelism" Ø Modularity & Convenience ( eg, editing, printing in the same time)" Cooperating processes need inter-process communication (IPC)" Ø Shared memory" Ø Pipe and Named Pipe" Ø Message passing" Lecture 3: Memory Management Outline Simple memory management: swap etc Virtual memory and paging Ø Page table and address translation Ø Translation lookaside buffer (TLB) Multi-level page table Page replacement algorithms and modeling Working set of processes " Department of Computer Science @ UTSA 18 Motivations for Virtual Memory Use physical DRAM as cache for the disk" Ø Virtual pages of processes can exceed physical memory size" Simplify memory management" Ø Multiple processes resident in main memory" ü Each with its own address space" Ø Only active code and data is actually in memory" Provide protection" Ø One process can t interfere with another" ü Because they operate in different address spaces" Ø User process cannot access privileged information" ü Different sections of address spaces have different permissions" Address Translation Architecture CPU Virtual address page number page offset p d page table 0 1 p-1 p f frame number f d physical address How big the page table is? 0 1 f-1 f f+1 f+2 physical memory 20 5
Page Table Size Memory Requirement of Page Tables Modern Systems/Applications Ø 32 bits virtual address Ø System with 1GB physical memory à 30 bits physical address Ø Suppose the size of one page/frame is 4KB (12 bits) Page table size Ø # of virtual pages: 32 12 = 20 bits à 2 20 PTEs Ø Page table size = PTE size * 2 20 = 4 MB per process à 2 10 frames If there are 128 processes Ø Page tables occupy 128 * 4MB = 512 MB Ø 50% of memory will be used by page tables? How can we get smaller page table?! Only the 1 st level page table and the required 2 nd level page tables need to be in memory Example: a process with working-set size of 32 MB (recall that 1GB memory and 32 bits virtual address) Ø 4KB / page à process has 8KB (8*2 10 ) virtual pages Ø One 2 nd level page table maps 2 10 pages; Ø Number (minimum) of 2 nd level page table needed: 8 Ø Total (minimum) memory for page table: 1 st level page table + 8; in total of 9 page tables à 9 X 4KB = 36 KB Department of Computer Science @ UTSA 21 Department of Computer Science @ UTSA 22 Page table size 32bit machine, page size 4k, each entry 4 bytes, one level page table (full 4GB linear address) Page table size = 2^20 pages = 2^22 = 4M 32bit machine, page size 4k, each entry 4 bytes, two level page table (two apges:0x0000000, and 0xFFFFF000) Page table size = (2^10 level-0 entries) *4bytes + (2^10 level-1 entries * 4 bytes) * 2 = 12 Kbytes Size of Page/Frame: How Big? Determined by number of bits in offset (12Bità4KB) Smaller pages have advantages Ø Less internal fragmentation Ø Better fit for various data structures, code sections Larger pages are better because Ø Less overhead to keep track of them Ø More efficient to transfer larger pages to and from disk One principle: page table à fit into one frame 32bits machine, 10 bits for each level How can we make the address translation faster? 23 Department of Computer Science @ UTSA 24 6
Integrating TLB and Cache Translation Lookaside Buffer (TLB)" CPU hit VA PA miss TLB Cache Lookup miss Translation hit data Main Memory Working-Set Model Δ working-set window a fixed number of page references, example: 10,000 instruction" WSS i (working set of Process P i ) = total number of pages referenced in the most recent Δ " Ø if Δ too small, will not encompass entire locality" Ø if Δ too large, will encompass localities" Ø if Δ = will encompass entire program" D = Σ WSS i total demand frames " if D > (available frames) m Thrashing" Thus, if D > m, then suspend one of the processes" Page Replacement Algorithms Summary: Page Replacement Algorithms How to select the victim frame?" Ø You can select any frame, the page replacement will work; but the performance???" Ø Gives the lowest page-fault rate" Evaluate an algorithm by running it on a particular string of memory references (reference string) and compute the number of page faults on that string" "In all our examples, we will have 3 frames and the following reference string" Algorithm FIFO (First-In, First Out) Second chance LRU (Least Recently Used) OPT (Optimal) Comment Might throw out useful pages Big improvement over FIFO Excellent, but hard to implement exactly Not implementable, but useful as a benchmark Department of Computer Science @ UTSA 28 7
Page Allocation Buddy Allocation Example: Need to allocate 65 contiguous page frames" Ø Look in list of free 128-page-frame blocks " Ø If free block exists, allocate it, else look in next highest order list (here, 256-page-frame blocks)" Ø If first free block is in 256-page-frame list, allocate a 128- page-frame block and put remaining 128-page-frame block in lower order list" Ø If first free block is in 512-page-frame list, allocate a 128- page-frame block and split remaining 384 page frames into 2 blocks of 256 and 128 page frames These blocks are allocated to the corresponding free lists" Question: What is the worst-case internal fragmentation?" 29 Buddy De-Allocation When blocks of page frames are released the kernel tries to merge pairs of buddy blocks of size b into blocks of size 2b" Two blocks are buddies if:" Ø They have equal size b" Ø They are located at contiguous physical addresses" Ø The address of the first page frame in the first block is aligned on a multiple of 2b*2 12 " The process repeats by attempting to merge buddies of size 2b, 4b, 8b etc " Slab Allocator Performs the following functions" Ø Allocate memory" Ø Initialize objects/structures" Ø Use objects/structures" Ø Deconstruct objects/structures" Ø Free memory" /proc/slabinfo gives full information about memory usage on the slab level (see also /usr/bin/slabtop)" 8
Types of questions on Midterm1 Question answering: Ø Example: What is a system call? Problem Analysis: Ø Example: drawing the Gantt chart for FIFO scheduler CS5523: Operating Systems @ UTSA 33 9