Operating Systems and ProtectionGoals of Today’s LectureOperating SystemWhat is a Process?Divide Hardware into Little Pieces?Indirection, and Sharing in Time?When to Change Which Process is Running?Life Cycle of a ProcessSwitching Between ProcessesContext Switch: What to Save & Load?Process Control BlockSharing MemoryVirtual MemoryVirtual Memory for a ProcessVirtual MemoryPage TablesPage Tables Reside in Memory...Process ID RegisterProtection Between ProcessesPagingPage Fault!Write Some Other Page to DiskFetch Current Page, Adjust Page TablesMeasuring the Memory UsageContext Switch, in More DetailContext Switch, in More DetailContext Switch, in More DetailContext Switch, in More DetailResuming Some Other ProcessSystem call, just another kind of faultSummary1Operating Systemsand ProtectionProf. David AugustCOS 2172Goals of Today’s Lecture• How multiple programs can run at onceo Processeso Context switchingo Process control blocko Virtual memory• Boundary between parts of the systemo User programso Operating systemo Underlying hardware• Mechanics of handling a page faulto Page tableso Process ID registerso Page faults3Operating System• Supports virtual machineso Promises each process the illusion of having whole machine to itself• Provides services:o Protectiono Scheduling o Memory managemento File systemso Synchronizationo etc.HardwareOperating SystemUserProcessUserProcess4What is a Process?• A process is a running program with its own …o Processor state– EIP, EFLAGS, registerso Address space (memory)– Text, bss, data, heap, stack• Supporting the abstractiono Processor– Saving state per process– Context switchingo Main memory– Sharing physical memory– Supporting virtual memoryo Efficiency, fairness, protectionHardwareOperating SystemUserProcessUserProcess5Divide Hardware into Little Pieces?• Idea: registers, memory, ALU, etc. per processo Pro: totally independent operation of each processo Con: lots of extra hardware;some parts idle at any given time;hard limit on the number of processesOperating SystemUserProcessUserProcessHardware6Indirection, and Sharing in Time?• Idea: swap processes in and out of the CPU;map references into physical addresseso Pro: make effective use of the resources by sharingo Con: overhead of swapping processes;overhead of mapping memory referencesHardwareOperating SystemUserProcessUserProcess7When to Change Which Process is Running?• When a process is stalled waiting for I/Oo Better utilize the CPU, e.g., while waiting for disk access• When a process has been running for a whileo Sharing on a fine time scale to give each process the illusion of running on its own machineo Trade-off efficiency for a finer granularity of fairnessCPUCPUCPUI/OI/O I/O1:CPUCPUCPUI/OI/O I/O2:8Life Cycle of a Process• Running: instructions are being executed•Waiting:waiting for some event (e.g., I/O finish) • Ready: ready to be assigned to a processorCreate Ready Running TerminationWaiting9Switching Between ProcessesRunningRunningSave contextLoad contextSave contextLoad context......Process 1Process 2RunningWaitingWaitingWaiting10Context Switch: What to Save & Load?• Process state o New, ready, waiting, halted• CPU registers o EIP, EFLAGS, EAX, EBX, …• I/O status information o Open files, I/O requests, …• Memory management information o Page tables• Accounting informationo Time limits, group ID, ...• CPU scheduling information o Priority, queues11Process Control Block• For each process, the OS keeps track of ...o Process state o CPU registers o CPU scheduling informationo Memory management informationo Accounting information o I/O status informationreadyEIPEFLAGSEAXEBX...etc.PCB3PCB2PCB1OS’s memoryProcess1’smemoryProcess2’smemoryProcess3’smemory12Sharing Memory• In the old days…o MS-DOS (1990)o Original Apple Macintosh (1984)• Problem: protectiono What prevents process 1 from reading/writing process 3’s memory?o What prevents process 2 from reading/writing OS’s memory?• In modern days, Virtual Memory protectiono IBM VM-370 (1970)o UNIX (1975)o MS Windows (2000)PCB3PCB2PCB1OS’smemoryProcess1’smemoryProcess2’smemoryProcess3’smemory13Virtual Memory• Give each process illusion of large address spaceo E.g., 32-bit addresses that reference 4 Gig of memory• Divide the physical memory into fixed-sized pageso E.g., 4 Kilobyte pages• Swap pages between disk and main memoryo Bring in a page when a process accesses the spaceo May require swapping out a page already in memory• Keep track of where pages are stored in memoryo Maintain a page table for each process to do mapping• Treat address as page number and offset in pageo High-order bits refer to the pageo Low-order bits refer to the offset in the page14Virtual Memory for a ProcessVirtual Address Space Physical Address SpaceAddressTranslationaddress0virtualpage numberoffset in pagephysicalpage numberoffset in page15Virtual MemoryProcess 2 VirtualAddress SpacePhysical Address Space01Process 1 VirtualAddress Space0121012001OSV.A.S.0116Page TablesProcess 1 VirtualAddress SpacePhysical Address Space01Process 2 VirtualAddress Space01210120010101234563251460012ProcessNumberOSV.A.S.17Page Tables Reside in Memory...Process 1 VirtualAddress SpacePhysical Address Space01Process 2 VirtualAddress Space01210120010162345OSV.A.S.18Process ID RegisterPhysical Address SpaceProcess 2012101200101234562Process IDaddressvirtualpage numberoffset in page325146001219Protection Between ProcessesProcess 201232514600122virtualpage numberoffset in pageProcess IDaddress• User-mode (unprivileged) process cannot modify Process ID register• If page tables are set up correctly, process #1 can access only its own pages in physical memory• The operating system sets up the page tables20PagingProcess 201232xx14600122virtualpage numberoffset in pageProcess IDaddressPhysical Address Space1120012321Page Fault!Process 201232xx14600122Process IDPhysical Address Space11200123movl 0002104, %eax22Write Some Other Page to DiskProcess 2012yy2xx14600122Process IDPhysical Address Space1100123movl 0002104, %eax223Fetch Current Page, Adjust Page TablesProcess 2012yy2314600122Process IDPhysical Address Space11000123movl 0002104, %eax24Measuring the Memory Usage% ps lF UID PID PPID PRI VSZ RSS STAT TIME COMMAND0 115 7264 7262 17 4716 1400 SN 0:00 -csh0 115 7290 7264 17 15380 10940 SN 5:52 emacs0 115 3283 7264 23 2864 812 RN 0:00 ps lVirtual memory
View Full Document