CS241 System Programming Process Klara Nahrstedt Lecture 4 1 27 06 Overview Process State z Process Information z Context Switching z Summary z 2 1 22 2006 CS 241 System Programming Klara Nahrstedt Administrative Read T Chapter 2 1 z Read R R Chapter 3 z 3 1 22 2006 CS 241 System Programming Klara Nahrstedt Processes z z z A process is an abstraction for sequence of operations that implement a computation A process may be manipulated suspended scheduled and terminated Process is a unit of work in a modern computer Process Types z 4 OS processes executing system code User processes executing user code Processes are executed concurrently with CPU multiplexing among them 1 22 2006 CS 241 System Programming Klara Nahrstedt Process An Object with State z z 5 OS manipulated a process as if it is an object with a state The operations that OS performs on the process change the state of the process 1 22 2006 CS 241 System Programming Klara Nahrstedt Process States z z z 6 The process has states during its lifetime running waiting ready suspended ended State of a process is defined by the current process activity Operations on the process may depend on its state 1 22 2006 CS 241 System Programming Klara Nahrstedt Major Process States z Active z Waiting or halted process is waiting for some event to occur 7 Running On CPU instructions in the program code are being executed Start new before execution process is being created 1 22 2006 CS 241 System Programming Klara Nahrstedt Major Process States 8 Ready waiting for CPU process is waiting to be assigned to the CPU Blocked waiting waiting for an event Suspended waiting to be swapped in Dead terminated finished or aborted 1 22 2006 CS 241 System Programming Klara Nahrstedt Process States z Possible process states z z 9 Running occupy CPU Blocked Ready does not occupy CPU Other states suspended terminated Transitions between states Question in a single processor machine how many processes can be in running state CS 241 System Programming 1 22 2006 Klara Nahrstedt Process States Finite State Diagram 10 1 22 2006 CS 241 System Programming Klara Nahrstedt State Transitions 11 1 22 2006 CS 241 System Programming Klara Nahrstedt So What Is A Process z z z z 12 It s one executing instance of a program It s separate from other instances It can start launch other processes It can be launched by them 1 22 2006 CS 241 System Programming Klara Nahrstedt So What s In A Process And Why z Process State z Program Counter z time limits process number owner I O Status Information 13 base limit information Accounting Information z process priority and pointer Memory Management Information z index registers stack pointers general purpose registers CPU Scheduling Information z the address of the next instruction to be executed for this process CPU Registers z new ready running waiting halted list of I O devices allocated to the process 1 22 2006 CS 241 System Programming Klara Nahrstedt What Does This Program Do int myval int main int argc char argv myval atoi argv 1 while 1 printf myval is d loc 0x lx n myval long myval Now simultaneously start two instances of this program z Myval 5 z Myval 6 z What will the outputs be 14 1 22 2006 CS 241 System Programming Klara Nahrstedt Here s The Output 15 1 22 2006 CS 241 System Programming Klara Nahrstedt Instances Of Programs z z The address was always the same The values were different z z z 16 Implies that the programs aren t seeing each other But they think they re using the same address Conclusion addresses are not absolute Implication memory mapping What s the benefit 1 22 2006 CS 241 System Programming Klara Nahrstedt Remember This Application Libraries User space level Kernel space level Portable OS Layer Machine dependent layer 17 1 22 2006 CS 241 System Programming Klara Nahrstedt Address Space z One common approach Kernel is high memory User is low memory z What restrictions apply z read f buf nbytes 0xffff Kernel space User space 0x0000 18 1 22 2006 CS 241 System Programming Klara Nahrstedt More Address Space z Program segments z Stack Lots of flexibility 19 0xffff Kernel space Text Data Stack Heap Allows stack growth Allows heap growth No predetermined division 1 22 2006 Heap Code Data 0x0000 CS 241 System Programming Klara Nahrstedt Process Control Block PCB Fields of a process table entry 20 1 22 2006 CS 241 System Programming Klara Nahrstedt Process Scheduling z z z 21 Objective of multiprogramming maximal CPU utilization i e have always a process running Objective of time sharing switch CPU among processes frequently enough so that users can interact with a program which is running Need Context Switching 1 22 2006 CS 241 System Programming Klara Nahrstedt Context Switch z z z Switch CPU from one process to another Performed by scheduler It includes z Context switch is expensive 1 1000 microseconds z 22 save PCB state of the old process load PCB state of the new process Flush memory cache Change memory mapping TLB No useful work is done pure overhead Can become a bottleneck Real life analogy Need hardware support 1 22 2006 CS 241 System Programming Klara Nahrstedt PCB 0 exec Process Context Switch PCB 1 Interrupt of system call idle Save state into PCB 0 Reload state from PCB 1 Interrupt System call idle exec Save state into PCB 1 Reload state from PCB 0 idle exec 23 1 22 2006 CS 241 System Programming Klara Nahrstedt Interrupts z z z z 24 Interrupt is an electrical signal generated by a peripheral device A Device generates interrupt to set a hardware flag within the processor At each instruction cycle processor checks hardware flags to see if any peripheral devices need attention If interrupt is detected processor saves the current value of the program counter and loads a new value address of a special function interrupt service routine or interrupt handler 1 22 2006 CS 241 System Programming Klara Nahrstedt Interrupts to Implement Time Sharing z z z z z 25 A device called timer can generate interrupt after a specified interval of time Process Management OS starts the timer before setting the program counter set quantum When timer expires it generates an interrupt that causes the CPU to execute timer interrupt service routine Interrupt service routine writes the address if the OS code into the program counter and the OS is back in control CPU switch occurs old process loses the CPU and new process starts 1 22 2006 CS 241 System Programming Klara Nahrstedt Interrupt Processing z z z How is the Illusion of
View Full Document