Goals of Today’s Lecture • How multiple programs can run at once o Processes o switching Operating Systems o control block and Protection o • Boundary between parts of the system User programs Prof. David August o o COS 217 o Underlying hardware • Mechanics of handling a page fault o Page tables o Process ID registers 1 o Page faults 2

Operating System What is a Process? • Supports virtual machines • A process is a running program with its own … o Promises each process the illusion of having whole o Processor state machine to itself – EIP, EFLAGS, registers Address space (memory) • Provides services: o – Text, bss, data, heap, stack o Protection User User User User • Supporting the abstraction o Process Process Process Process Processor o o – Saving state per process o File systems Operating System Operating System – Context switching o Synchronization Main memory o etc. o Hardware – Sharing physical memory Hardware – Supporting virtual memory 3 o Efficiency, fairness, protection 4 Divide Hardware into Little Pieces? Indirection, and Sharing in Time?

User User User User Process Process Process Process

Operating System Operating System

Hardware Hardware

• Idea: registers, memory, ALU, etc. per process • Idea: swap processes in and out of the CPU; o Pro: totally independent operation of each process map references into physical addresses Con: lots of extra hardware; o o Pro: make effective use of the resources by sharing some parts idle at any given time; o Con: overhead of swapping processes; hard limit on the number of processes overhead of mapping memory references 5 6

When to Change Which Process is Running? Life Cycle of a Process • When a process is stalled waiting for I/O • Running: instructions are being executed o Better utilize the CPU, e.g., while waiting for disk access •Waiting:waiting for some event (e.g., I/O finish) • Ready: ready to be assigned to a processor 1: CPU I/O CPU I/O CPU I/O 2: CPU I/O CPU I/O CPU I/O Create Ready Running Termination

• When a process has been running for a while o Sharing on a fine time scale to give each process the illusion of running on its own machine Waiting o Trade-off efficiency for a finer granularity of fairness 7 8 Switching Between Processes Context Switch: What to Save & Load?

Process 1 Process 2 • Process state o New, ready, waiting, halted Waiting Running Save context • CPU registers . EIP, EFLAGS, EAX, EBX, … . o Load context • I/O status information o Open files, I/O requests, … Waiting Running • Memory management information Page tables Save context o . . • Accounting information o Time limits, group ID, ... Running Load context Waiting • CPU scheduling information o Priority, queues 9 10

Process Control Block Sharing Memory

• For each process, the OS keeps track of ... • In the old days… Process 3’s o Process state ready o MS-DOS (1990) o CPU registers EIP memory EFLAGS o Original Apple Macintosh (1984) Process CPU scheduling information EAX o EBX 2’s o Memory management information ... • Problem: protection memory etc. o Accounting information What prevents process 1 from reading/writing I/O status information o o process 3’s memory? PCB1 Process o What prevents process 2 from reading/writing 1’s PCB2 OS’s memory? memory PCB3 • In modern days, Virtual PCB1 PCB2 Process Process Process IBM VM-370 (1970) o PCB3 1’s 2’s 3’s o (1975) OS’s OS’s memory memory memory memory 11 o MS Windows (2000) memory 12 Virtual Memory Virtual Memory for a Process • Give each process illusion of large address space o E.g., 32-bit addresses that reference 4 Gig of memory • Divide the physical memory into fixed-sized pages Address o E.g., 4 Kilobyte pages Translation • Swap pages between disk and main memory Bring in a page when a process accesses the space o address o May require swapping out a page already in memory offset in page • Keep track of where pages are stored in memory offset in page o Maintain a for each process to do mapping virtual physical • Treat address as page number and offset in page page number page number

o High-order bits refer to the page 0 o Low-order bits refer to the offset in the page 13 Virtual Address Space Physical Address Space 14

Virtual Memory Page Tables 1 1 3 6 2 0 Process 5 0 Number 5 2 2 2 1 0 1 4 0 0 4 1 1 6 2 2 0 3 0 0 1 1 Process 1 Virtual 1 Process 2 Virtual 1 2 Address Space 1 Address Space 1 0 1 0 1 1 Process 2 Virtual 0 Process 1 Virtual 0 0 0 0 Address Space Physical Address Space Physical OS Address Space 15 OS Address Space 16 V.A.S. V.A.S. Page Tables Reside in Memory... Process ID Register 1 6 3 6 0 2 2 0 5 5 5 2 0 1 2 1 0 4 1 4 4 1 0 2 0 6 2 3 0 3 0 1 Process 2 1 1 2 2 Process 2 Virtual Process ID 2 Address Space 1 1 0 1 1 1 address Process 1 Virtual offset in page 0 0 0 virtual 0 Address Space Physical page number Physical OS Address Space 17 Address Space 18 V.A.S. Protection Between Processes Paging

3 3 2 2 2 2 5 • User-mode (unprivileged) xx 1 2 1 process cannot modify 1 2 1 1 4 Process ID register 1 4 0 0 0 6 0 6 2 0 • If page tables are set up 0 3 Process 2 correctly, process #1 can Process 2 1 2 Process ID 2 access only its own pages Process ID 2 in physical memory 1 1 address address offset in page • The operating system sets offset in page 0 virtual up the page tables virtual 0 page number page number Physical 19 Address Space 20 Page Fault! Write Some Other Page to Disk

3 yy 2 2 2 2 xx xx 2 1 2 1 1 2 1 1 4 1 4 0 0 0 6 2 0 6 0 3 0 3 Process 2 1 Process 2 1 2 2 Process ID 2 Process ID 2 1 1 1 1

movl 0002104, %eax 0 movl 0002104, %eax 0 0 0 Physical Physical Address Space 21 Address Space 22

Fetch Current Page, Adjust Page Tables Measuring the Memory Usage

Virtual memory usage yy Physical memory usage (“resident set size”) 2 2 CPU time used by this process so far 3 Unix % ps l 1 2 1 F UID PID PPID PRI VSZ RSS STAT TIME COMMAND 4 0 115 7264 7262 17 4716 1400 SN 0:00 -csh 1 0 115 7290 7264 17 15380 10940 SN 5:52 emacs 0 0 0 115 3283 7264 23 2864 812 RN 0:00 ps l 0 6 0 3 Process 2 1 Windows 2 Process ID 2 1 1

movl 0002104, %eax 0 0 Physical Address Space 23 24 Context Switch, in More Detail Context Switch, in More Detail

Process 1 Process 2 Process 1 page fault Waiting Running Save context Running addl %eax, %ecx . movl –8(%ebp), %eax . addl %eax, %ecx Load context . . Waiting Running Waiting .

Process Save context PCB1 . 1’s PCB2 . memory PCB3 Running Load context Waiting Running Registers OS’s memory

25 26

Context Switch, in More Detail Context Switch, in More Detail

Process 1 Fault-handler hardware Process 1 OS software 1. Enters privileged mode 5. Pops saved EIP,ESP into 2. Sets EIP to specific location PCB1 Running in operating system Running 6. Copies rest of registers into addl %eax, %ecx 3. Sets ESP to operating- addl %eax, %ecx PCB1 movl –8(%ebp), %eax system stack in OS memory movl –8(%ebp), %eax 7. Sends instructions to disk addl %eax, %ecx 4. Pushes old (process 1) EIP addl %eax, %ecx drive to fetch page . and ESP on OS stack . . . Waiting . Waiting .

Process PCB1 Process PCB1 1’s PCB2 1’s PCB2 memory PCB3 memory PCB3 Running Running Registers OS’s Registers OS’s memory memory

27 28 Resuming Some Other Process , just another kind of fault

OS software Hardware Process 1 8. Sets process-ID register to 2 12. Pops EIP,ESP into registers system call 9. Pushes saved EIP,ESP from 13. Switches back to (privileged instruction) PCB2 onto OS stack unprivileged mode Running 10. Copies rest of registers from 14. Resumes where process 2 mov $4,%eax PCB2 left off last time int $0x80 11. Executes “return from ” addl %eax, %ecx instruction . . Waiting .

Process PCB1 Process PCB1 1’s PCB2 1’s PCB2 memory PCB3 memory PCB3 Running Registers OS’s Registers OS’s memory memory

29 30

Summary • Abstraction of a “process” o CPU: a share of CPU resources on a small time scale o Memory: a complete address space of your own • OS support for the process abstraction o CPU: context switch between processes o Memory: virtual memory (VM) and page replacement o Files: open/read/write, rather than “move disk head” o Protection: ensure process access only its own resources • Hardware support for the process abstraction o Context switches, and push/pop registers on the stack o Switch between privileged and unprivileged modes o Map VM address and process ID to physical memory 31