1&6(3ULQFLSOHVRI2SHUDWLQJ6\VWHPV)DOOLecture 3: ProcessesGeoffrey M. VoelkerSeptember 26, 2001 CSE 120 – Lecture 3 – Processes 23URFHVVHV● 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?2September 26, 2001 CSE 120 – Lecture 3 – Processes 37KH3URFHVV● 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 executionSeptember 26, 2001 CSE 120 – Lecture 3 – Processes 43URFHVV&RPSRQHQWV● 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)3September 26, 2001 CSE 120 – Lecture 3 – Processes 53URFHVV'LDJUDPStack0x000000000xFFFFFFFFCode(Text Segment)Static Data(Data Segment)Heap(Dynamic Memory Alloc)AddressSpaceSPPCSeptember 26, 2001 CSE 120 – Lecture 3 – Processes 63URFHVV6WDWH● 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?4September 26, 2001 CSE 120 – Lecture 3 – Processes 73URFHVV6WDWH*UDSKNew ReadyRunningWaitingTerminatedCreate ProcessProcess ExitI/O, Page Fault, etc.I/O DoneSchedule ProcessUnschedule ProcessSeptember 26, 2001 CSE 120 – Lecture 3 – Processes 83URFHVV'DWD6WUXFWXUHVHow 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 hardware5September 26, 2001 CSE 120 – Lecture 3 – Processes 93&%'DWD6WUXFWXUH● 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.● Unix: PCB is defined in sys/proc.h as struct proc◆ FreeBSD: 81 fields, 408 bytes (not lightweight)September 26, 2001 CSE 120 – Lecture 3 – Processes 103&%VDQG+DUGZDUH6WDWH● When a process is running, its hardware state (PC, SP, regs, etc.) is in the CPU◆ The hardware registers contain the current values● 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◆ 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!6September 26, 2001 CSE 120 – Lecture 3 – Processes 116WDWH4XHXHVHow 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 anotherSeptember 26, 2001 CSE 120 – Lecture 3 – Processes 126WDWH4XHXHVNetscape PCB X Server PCB Idle PCBEmacs PCBReady QueueDisk I/O QueueConsole QueueSleep Queue...ls PCBThere may be many wait queues, one for each type of wait (disk, console, timer, network, etc.)7September 26, 2001 CSE 120 – Lecture 3 – Processes 133&%VDQG6WDWH4XHXHV● PCBs are data structures dynamically allocated in OS memory● When a process is created, the OS allocates a PCB for it, initialized, and placed on the ready queue● As the process computes, does I/O, etc., its PCB moves from one queue to another● When the process terminates, its PCB is deallocatedSeptember 26, 2001 CSE 120 – Lecture 3 – Processes 143URFHVV&UHDWLRQ● A process is created by another process◆ Parent is creator, child is created (Unix: ps “PPID” field)◆ What creates the first process (Unix: init (PID 0 or 1))?● In some systems, the parent defines (or donates) resources and privileges for its children◆ Unix: Process User ID is inherited – children of your shell execute with your privileges● After creating a child, the parent may either wait for it to finish its task or continue in parallel (or both)8September 26, 2001 CSE 120 – Lecture 3 – Processes 153URFHVV&UHDWLRQ17● The system call on NT for creating a process is called, surprisingly enough, CreateProcess:BOOL CreateProcess(char *prog, char *args) (simplified)● CreateProcess◆ Creates and initializes a new PCB◆
View Full Document