900 likes | 1.06k Views
Chapter 2. Processes and Scheduling (I). Contents. Foundation : Process Description and Control Implement : Mutual Exclusion and Synchronization Avoidance : Deadlock (死锁) and Hungry (饥饿) Solution : Several Classical( 经典 ) Problems About : Process Communications
E N D
Chapter 2 Processes and Scheduling (I)
Contents • Foundation:Process Description and Control • Implement: Mutual Exclusion and Synchronization • Avoidance: Deadlock(死锁) and Hungry(饥饿) • Solution: Several Classical(经典) Problems • About: Process Communications • Policy: Process scheduling
Process Description and Control (p101) • Concurrence (vs.parallel) execution of processes (supplement(补充)) • Process states and transition • Process description(PCB) • Process control(Primitive/Atomic)
Serial Execution of Program • Serial execution characteristics • Sequence(顺序)、isolate(独立)、recurrence(再现) • Concurrence execution characteristics • discontinuity、no-isoloate、no-recurrence • Bernstein condition R(P1) ∩W(P2)∪W(P1) ∩R(P2)∪W(P1) ∩W(P2) = {Φ}
Some Problems for Process Management(p101) • How to dispatch(分派) CPU to multi-processes • How to allocate resource to multi-processes and avoid deadlock and hungry • How to implement the process communication
Process Concept(p60) • A program in execution • The “animated spirit” of a program • The entity that can be assigned to and executed on a processor
Process Characteristics(Supplement) • Dynamic: create->dispatch->request I/O-> redispatch(重分派)->exit • Concurrence: • Independence:Each process does not feel the existence of other processes. • Discontinuity:
Architecture of Process(p120) • User data and program • System stack • PCB,Process Control Block
Process States • Concurrence execution of processes • States of processes(2-states、5-states)
Example of Concurrence Execution(p102) An example: • Three processes are fully loaded in main memory. • A small dispatcher program that moves the processor from one process to another. • The first 12 instruction executed in processes A and C. • Process B executes four instructions and the fourth instruction invokes(激发) an I/O operation for which the process must wait.
Two State Process Model(P104) • Two states • Running • Not-running • A new process enters into the not-running state. • A running process will be interrupted and a new process will be selected to run.
Figure 3.4b • There is a single queue in which each entry(项) is a pointer to a particular process.
Creation and Termination Processes (I) • Creation Process: OS builds the data structures that are used to manage the process and allocates address space to the process. • Reasons for Process Creation • New batch job • Interactive(交互) logon • Created by OS to provide a service example: printing • Spawned(产生) by existing process process tree
Example of CreateProcess Create() { struct pcb *p; p=alloc_pcb(); //alloc a empty process control block …… // initial p p->state= not-running; p->parent=current; current->child=p; enqueue(WAITQUEUE, p); // put p into wait queue …… }
Creation and Termination Processes (II) • Reasons for Process Termination • Halt command • User log off • User program exit • Program error(e.g. no resource for use) • Normal completion • Time limit exceeded • Memory unavailable
Creation and Termination Processes (III) • Reasons for Process Termination • Bounds violation • Protection error • Example write to read-only file • Arithmetic error • Time overrun • Process waited longer than a specified maximum for an event
Creation and Termination Processes (IV) • Reasons for Process Termination • I/O failure • Invalid instruction • happens when try to execute data • Privileged instruction • Data misuse • Operating system intervention • such as when deadlock occurs • Parent terminates so child processes terminate • Parent request
A Five State Model (p107) • Some processes in the not-running state are ready to execute, while others are blocked, waiting for an I/O operation to complete.
A Five State Model • Running: For single processor,only one process can be in this state at a time. • Ready: A process that is prepared to execute when given the opportunity. • Blocked: A process cannot execute until some event occurs, such as the completion of an I/O operation. • New: A process that has just been created, but has not yet been admitted to the pool(池) of executable processes. • Exit: A process has been release from the pool of executable processes.
State Transition for a Process (I) • Null->new: A new process is created to execute a program • New->ready: OS will move a process from new state to ready state when it is prepared to take on an additional process. • Ready->running: OS chooses one of the processes in the ready state to run. • Running->Exit: Process has completed or aborts. • Running->ready: The running process has reached the maximum allowable time for uninterrupted execution, or higher priority process reaches to preempt the CPU.
State Transition for a Process(II) • Running->blocked: A process is put in the blocked state if it requests something for which it must wait. E.g. a system service or I/O operation. • Blocked->ready: When the event for which the blocked process has been waiting occurs. • Ready->exit: In some systems, a parent may terminate a child process at any time. Also, if a parent terminates, all child processes of it may be terminated. • Blocked->exit: Same as the Ready->exit.
Swapping(p111交换) • The processor is so much than I/O that it will be common for all of the processes in memory to be waiting for I/O. • Swapping: Moving part or all of a process from main memory to disk.
Suspending(挂起) • When all of the processes in memory are blocked, OS will suspend a process ,and swap it out to disk to release the memory for other ready process. • A new state may be introduced in the suspending state
Reasons for Process Suspension (Table 3.4 p116) • Swapping: OS needs to release sufficient main memory to bring in a process that is ready to execute. • Other OS reason: OS may suspend a background or utility process or a process that is suspected of causing a problem. • Interactive user request: A user may wish to suspend execution of a program for purposes of debugging or waiting the use of a resource.
Reasons for Process Suspension • Timing: A process may be executed periodically(周期性) and may be suspended while waiting for the next time interval. • Parent process request: A parent process may wish to suspend execution of a descendent(子孙) to examine or modify the suspended process, or to coordinate the activity of various descendents.
Characteristics of Suspending Process (p116) • Is not immediately available for execution. • The process may or may not be waiting on an event. If it is, this blocked condition is independent of the suspend condition, and occurrence of the blocking event does not enable the process to be executed. • The process was placed in a suspended state by an agent: either itself, a parent process or OS. • The process may not be removed from this state until the agent explicitly orders the removal.
Suspending and Block Problem: • Only a blocked process can be suspended? • How to active a suspend process?
Suspending and Block • Two concepts • If a process is waiting a event, it may be blocked. • If a process is swapped out to memory, it is suspended.
Combination States • Ready: The process is in main memory and available for execute. • Blocked: The process is in main memory and awaiting an event. • Blocked, suspend: The process is in secondary memory and awaiting an event. • Ready, suspend: The process is in secondary memory but is available for execution as soon as it is loaded into main memory.
States Transition with Suspend State (p116) • Blocked->blocked,suspend: OS swap out a blocked process to release memory. • Blocked,suspend->ready,suspend: A process in the blocked, suspend state is moved to the ready, suspend state when the event for which it has been waiting occurs. • Ready,suspend->ready: OS bring one in to continue execution. • Ready->ready,suspend: Sometime OS swap out a ready process to release memory.
States Transition with Suspend State • New->ready,suspend(new->ready): When a new process is created, it can either be added to the ready queue or ready, suspend queue. If there is no enough memory for a new process, the new->ready,suspend transition happened. • Blocked,suspend->blocked: If there is free memory, OS may put the highest priority blocked, suspend process into memory.
States Transition with Suspend State • Running->ready,suspend: OS is preempting the process because a higher priority process on the blocked,suspend queue has just become unblocked, the OS could move the running process directly to the (ready, suspend)queue and free some main memory • Various->exit: When a process be terminated by it’s parent, the process can be in any state when be terminated.
Thread • Why using thread • Thread entity • Thread states: ready、running、block • State transition
Why Using Thread • To improve the concurrence of processes • Unit of resource ownership • Always process before or after introduce thread • Unit of dispatching • Before introducing thread, is process, after that, is thread (p145) • A main thread is always associate with the process in all its lifeline. • The context of a thread is smaller than that of a process, and switching thread(线程切换) has small spending than switching process. • The overhead of communication between two threads in one process is little.
Thread Entity(p146) • States: running, ready, etc. • Thread context • Every thread has it’s execution point(执行点). • An execution stack. • Some per-thread static storage for local variables. • Access to the memory and resources of its process, share with all other threads in that process.
Multi-thread • Each process includes many threads,and it has a private space and a PCB • Threads belongs to a process share the resource of the process • E.g. When a thread open a file,the other threads in the same process can also access the file.
Examples of Multi-thread • Ftp server. • Ms-word and it’s spelling checking. • Asynchronous processing, e.g.: write disk (backup). • When a thread is computing data, another thread is reading the data.
The States of Thread(p150) • Ready • Running • Block • ?Suspend