
a404d843fe015026b8093761b2fe432f.ppt
- Количество слайдов: 20
Process management n Information maintained by OS for process management ¡ process context ¡ process control block n OS virtualization of CPU for each process. ¡ Context switching ¡ Dispatching loop
Process n a program in execution ¡ We should know about processes by now. How does the OS correctly run multiple processes concurrently? n What kind of information to be kept? ¡ What does the OS have to do in order to run processes correctly. ¡
Process context n MAX stack Contains all states necessary to run a program ¡ The information the process needs to do the job: code, data, stack, heap. n heap data This is known as User level context. text (code) 0 Process in memory
User level context. MAX … int aa; char buf[1000]; void foo() { int a; … } main() { int b; char *p; p = new char[1000]; foo(); } (b, *p) - main (a) - foo heap (p) (char[1000]) data (aa, buf) text (code) 0 Process memory stack
Process context P 1 n Contains all states necessary to run a program ¡ Is the user level context sufficient? n n Only if the system runs through one program at a time The system typically needs to switch back and forth between programs. P 2 R 0 = 1 R 0 = 2 R 2 = R 0 + 1 R 2 = R 0 • R 2 in P 1 is wrong. How to make It correct? • Save R 0 in P 1 before switching • Restore R 0 in P 1 when switching from P 2 to P 1. • Registers should be a part of process context: the register context!
n Process context: ¡ User level context n ¡ ¡ Code, data, stack, heap Register context (R 0, R 1, …, PC, stack pointer, PSW, etc). What else? n OS resources. E. g open files, signal related data structures, etc.
Why is process context important? n To run a process correctly, the process instructions must be executed within the process context!
n Where is the process context stored? ¡ ¡ User level context is in memory. Other context information is stored in a data structure called process control block. The OS has a process control block table. For each process, there is one entry in the table. Process control block also contains other information that the OS needs to manage the processes. n n n Process status (running, waiting, etc) Process priority ……
An example PCB Figure 3. 3
OS CPU abstraction n Hardware reality: ¡ n One CPU runs the fetchexecute algorithm OS abstraction: ¡ ¡ Each process has one CPU, running the fetchexecute algorithm for the process. Each process executes within its context. Load PC; IR = MEM[PC]; While (IR != HALT) { PC ++; Execute IR; IR = MEM[PC]; }
OS CPU abstraction n What does the OS have to do? ¡ Embed the process instruction sequence into hardware instruction sequence. Process X instructions: x 0, x 1, x 2, …. Process Y instructions: y 0, y 1, y 2, … Process Z instructions: z 0, z 1, z 2, … Hardware instructions? x 0, x 1, x 2, y 0, y 1, y 2, z 0, z 1, z 2, x 3, x 4, x 5, … Does this embedding work? No!! Instructions in a process should only be executed within the process’s context to be correct.
OS CPU abstraction Process X instructions: x 0, x 1, x 2, …. Process Y instructions: y 0, y 1, y 2, … Process Z instructions: z 0, z 1, z 2, … x 0, x 1, x 2, [store X’s context], [restore Y’s context] y 0, y 1, y 2… OS must do this to keep programs execute within its context: Context switching
Dispatching Loop n The hardware view of the system execution: dispatching loop ¡ LOOP n n Context n Switch: Dispatcher n code Run process Save process states Choose a new process to run Load states for the chosen process Scheduling
Simple? Not Quite… n n n How does the dispatcher (OS) regain control after a process starts running? What states should a process save? How does the dispatcher choose the next thread?
How Does the Dispatcher Regain Control? n Two ways: 1. Internal events ¡ ¡ ¡ 2. A process is waiting for I/O A process is waiting for some other process Yield—a process gives up CPU voluntarily External events ¡ ¡ Interrupts—a complete disk request Timer—it’s like an alarm clock
What States Should a process save? n Anything that the next process may trash ¡ ¡ ¡ Program counter Registers Etc.
How Does the Dispatcher Choose the Next process? n n The dispatcher keeps a list of processes that are ready to run If no processes are ready ¡ n Dispatcher just loops Otherwise, the dispatcher uses a scheduling algorithm to find the next process.
Process States n A process is typically in one of the three states 1. 2. 3. Running: has the CPU Blocked: waiting for I/O or another thread Ready to run: on the ready list, waiting for the CPU
Figure 3. 2
a404d843fe015026b8093761b2fe432f.ppt