Project 2, Interrupts, and Scheduling CS 4411 Spring 2020
Announcements • Office hours • Regrades • Piazza
Outline for Today • Arrays and Stacks • Project 2 Overview • Interrupt Handling • Privilege Modes • Timer Interrupts • Scheduling with Quanta
On P1: A Note About Stacks 0xFFFFFFFF • Standard process layout: Stack “grows Kernel downward” • What does this mean? pop Stack SP • push instruction: push • Decrements SP • Stores register to memory at SP • pop instruction: Heap • Reads memory at SP into register Virtual addresses • Increments SP Data Code 0x00000000
Compare to Arrays • Arrays in C are contiguous memory • Array index is really pointer addition arr + 3 * sizeof(int) • Array variable is a pointer to first element int[4] arr; arr[o] arr[1] arr[2] arr[3] arr 0x6FAA00 0x6FAA00 0x6FAA04 0x6FAA08 0x6FAA0C 0x6FAA10
Arrays vs. Stacks arr int[4] arr; arr[o] arr[1] arr[2] arr[3] 0x6FAA00 0x6FAA04 0x6FAA08 0x6FAA0C 0x6FAA10 Register 4 Register 3 Register 2 Register 1 stack push sp
malloc() Behavior • malloc() is a natural fit for arrays: it returns a pointer to the lowest memory address in the allocated region int* arr = malloc(4 * sizeof(int)); arr 0x6FAA00 0x6FAA00 0x6FAA10 • Is this what you want for a thread/process’s stack? (Can you use arr as a stack pointer?)
Outline • Arrays and Stacks • Project 2 Overview • Interrupt Handling • Privilege Modes • Timer Interrupts • Scheduling with Quanta
Project 2 Basics • EGOS has a scheduler, but it’s not very good • Round-robin algorithm • FIFO run queue, timer interrupts force yield • Replace scheduling logic with Multi-Level Feedback Queue • Measure quality of new scheduler • Each process’s completion time and number of yields • Overall average CPU load
Project 2 Logistics • One file to edit: src/grass/process.c • When you make changes, keep the original code, and use a macro to select whether new or old code is compiled: #ifdef HW_MLFQ Your new code proc_next = mlfq_get_next(&run_queue, level); #else Original (round- proc_next = queue_get(&proc_runnable); robin) code #endif • If COMMONFLAGS in Makefile.common includes -DHW_MLFQ your code will be used, otherwise original code will be used
Concepts in Project 2 • Interrupt handling • Context switches (again) • Process blocking and I/O • Scheduling decisions and bookkeeping
Outline • Arrays and Stacks • Project 2 Overview • Interrupt Handling • Privilege Modes • Timer Interrupts • Scheduling with Quanta
Essentials of Interrupt Handling • Hardware-assisted • Interrupt Vector selects IV Interrupt Interrupt CPU Controller where CPU jumps Program • In a fixed, known Signals location, has an entry for Interrupt each type of interrupt Handler Devices • Forced context switch Memory
Privileges • Interrupt handling is a privileged operation • HW sets kernel-mode bit • Interrupt handlers are part of kernel • After interrupt handler runs, return control to user process User process User process User mode Kernel mode Keyboard interrupt handler Disk interrupt handler
Memory Layout • When interrupt happens, some other process Kernel is running • To switch to interrupt handler, kernel Stack SP memory must also be mapped in process’s PC address space • Otherwise, how would you get to interrupt handler’s code? Heap Data Code
Memory Layout • Interrupt handler is a program, needs a stack Kernel Kernel stack • Where should its stack be? • Kernel, in privileged mode, has access to Stack SP process’s entire memory space PC • Each process has a kernel stack • SP moved here every time kernel takes control • E.g. when interrupt handler is running Heap Data Code
Interrupt Handling in EGOS • Interrupts generated by “ intr ” module in Earth ( src/earth/intr.c) • Simulates interrupt controller • Kernel registers an interrupt handler that calls proc_got_interrupt() in process.c for all interrupts • Interrupts disabled (masked) by default in kernel mode • Interrupts only enabled: • When executing user-mode process • When waiting for I/O (even in kernel mode) • Masked interrupts will fire once interrupts re-enabled
A Special Kind of Interrupt Other Types of Interrupts Timer Interrupts • I/O Interrupts • Ding! Time has elapsed! • No pending task to do • Device has some input for you! • What’s the point? • Page Fault Interrupts • Periodically returns control to • Process needs memory! the kernel, even for long- • System Calls running processes • Kernel can switch to a different • Process wants you to do process – pre-emption something!
Outline • Arrays and Stacks • Project 2 Overview • Interrupt Handling • Privilege Modes • Timer Interrupts • Scheduling with Quanta
Reasons for Scheduling • Why might control return to the kernel? • A timer interrupt occurred • Another kind of interrupt occurred (I/O, system call, etc) • Process is blocked waiting for an event • Process has terminated • Which of these requires the kernel to schedule a new process?
A Day in the Life Process 2 P 2 Disk Process 1 Process 1 User mode interrupt Timer read() syscall Kernel mode interrupt Timer interrupt System call routine Disk interrupt handler handler
Quanta and Scheduling • Quantum = arbitrary unit (of time) • In a scheduler, quantum = maximum time a process can execute • Round Robin with 10ms quantum: Process 1 P 1 P 1 Process 2 P 2 10ms P 3 P 3 Blocks and waits Time Blocks and waits I/O finally ready
Quanta and Clock Interrupts • Timer (clock) interrupts are how the OS measures time • Scheduler’s quantum is a multiple of clock ticks • Each timer interrupt is a clock tick P2 User mode P1 P1 P1 P1 P1 Kernel mode 1 2 3 4 5 Kernel’s tick counter:
Round Robin’s Details • Round Robin with 10ms quantum • Timer interrupt (clock tick) every 2ms Timer interrupts Process 1 P 1 P 1 Process 2 P 2 10ms P 3 P 3 Syscall Syscall
On a Timer Interrupt • Increment clock tick • Determine if quantum is over • If not, interrupted process should resume running • Make scheduling decision • In Multi-Level Feedback Queue, what happens when a process reaches the end of a quantum without blocking?
Recommend
More recommend