Lecture 3 I O and Processes CSE 120 Principles of Operating Systems Alex C Snoeren Lab 0 HW 1 Due Tuesday 10 03 Input Output I O Input and Output devices operate independently of main CPU Speeds vary often much slower than CPU instructions Device uses interrupts to signal OS when ready done Have you ever heard of an IRQ Interrupt Request Level I O devices are managed by the OS Enforces sharing protection Requires privileged instructions to initiate I O Kernel maintains table to map device interrupts to handlers a k a device drivers Communication handled through memory abstraction Device buffers and registers mapped into address space Data copied into address range send data to I O device CSE 120 Lecture 3 Processes 2 I O Example 1 2 3 4 Ethernet receives packet writes packet into buffer Network Interface Card NIC signals an interrupt CPU stops current instruction saves current context CPU reads handler address from kernel vector table indexed by IRQ jumps to appropriate address Ethernet driver 5 Ethernet device driver reads packet from buffer processes packet 6 Kernel restores saved context reissues interrupted instruction CSE 120 Lecture 3 Processes 3 Interrupt Issues Interrupts suspend current process and transfer control to the OS Can the OS itself be interrupted Sometimes interrupts need to be masked until the OS is ready Applications to handle them Operating System Interrupts are default model for device interaction What is the alternative What are the drawbacks of the alternative approach Hardware When might an OS want to avoid interrupts CSE 120 Lecture 3 Processes 4 Atomicity Interrupts are pesky They can occur at any time including inconvenient ones In particular a handler may interfere with currently executing code paths Can a handler interrupt itself OS needs some way to ensure certain instructions or sequence of instructions are not interrupted One option is to disable interrupts during critical section Another alternative is to have special atomic instructions Guaranteed not to be interrupted E g test and conditionally set a bit based on previous value What if the machine has more than one CPU CSE 120 Lecture 3 Processes 5 Coordinating Execution This lecture starts a class segment that covers processes threads and synchronization These topics are perhaps the most important in this class You can rest assured that they will be covered in the exams Today s topics are processes and process management What are the units of execution How are those units of execution represented in the OS How is work scheduled in the CPU What are the possible execution states of a process How does a process move from one state to another CSE 120 Lecture 3 Processes 6 The Process The process is the OS abstraction for execution It is the unit of execution It is the unit of scheduling It is the dynamic execution context of a program A process is sometimes called a job or a task or a sequential process A sequential process is a program in execution It defines the sequential instruction at a time execution of a program Programs are static entities with the potential for execution CSE 120 Lecture 3 Processes 7 Process Components A process contains all of the state for a program in execution An address space The code for the executing program The data for the executing program An execution stack encapsulating the state of procedure calls The program counter PC indicating the next instruction A set of general purpose registers with current values A set of operating system resources Open files network connections etc A process is named using its process ID PID CSE 120 Lecture 3 Processes 8 Process Address Space 0xFFFFFFFF Stack SP Address Space Heap Dynamic Memory Alloc Static Data Data Segment Code Text Segment PC 0x00000000 CSE 120 Lecture 3 Processes 9 Process State A process has an execution state that indicates what it is currently doing Running Executing instructions on the CPU It is the process that has control of the CPU How many processes can be in the running state simultaneously Ready Waiting to be assigned to the CPU Ready to execute but another process is executing on the CPU Waiting Waiting for an event e g I O completion It cannot make progress until event is signaled disk completes As a process executes it moves from state to state Unix ps STAT column indicates execution state What state do you think a process is in most of the time How many processes can a system support CSE 120 Lecture 3 Processes 10 Process State Graph Create Process New Ready Unschedule Process Terminated I O Done Schedule Process Running Waiting I O Page Fault etc Process Exit CSE 120 Lecture 3 Processes 11 Process Data Structures How does the OS represent a process in the kernel At any time there are many processes in the system each in its particular state The OS data structure representing each process is called the Process Control Block PCB The PCB contains all of the info about a process The PCB also is where the OS keeps all of a process hardware execution state PC SP regs etc when the process is not running This state is everything that is needed to restore the hardware to the same configuration it was in when the process was switched out of the hardware CSE 120 Lecture 3 Processes 12 PCB Data Structure The PCB contains a huge amount of information in one large structure Process ID PID Execution state Hardware state PC SP regs Memory management Scheduling Accounting Pointers for state queues Etc It is a heavyweight abstraction CSE 120 Lecture 3 Processes 13 PCBs and Hardware State When a process is running its hardware state PC SP regs etc is in the CPU When the OS stops running a process it saves the current values of the registers into the process PCB When the OS is ready to start executing a new process it loads the hardware registers from the values stored in that process PCB The hardware registers contain the current values What happens to the code that is executing The process of changing the CPU hardware state from one process to another is called a context switch This can happen 100 or 1000 times a second CSE 120 Lecture 3 Processes 14 State Queues How does the OS keep track of processes The OS maintains a collection of queues that represent the state of all processes in the system Typically the OS has one queue for each state Ready waiting etc Each PCB is queued on a state queue according to its current state As a process changes state its PCB is unlinked from one queue and linked into another
View Full Document
Unlocking...