Download presentation
Presentation is loading. Please wait.
Published byDebra Summers Modified over 9 years ago
1
SWE202 Review
2
Processes
3
Process State As a process executes, it changes state – new: The process is being created – running: Instructions are being executed – waiting: The process is waiting for some event to occur – ready: The process is waiting to be assigned to a processor – terminated: The process has finished execution or abort abnormally
4
Diagram of Process State
5
Process Control Block (PCB)
6
CPU Switch From Process to Process
7
Ready Queue And Various I/O Device Queues
8
Schedulers Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue. Most of the process/job is on secondary memory storage such as: disk drive, etc. Such process/job will be swapped in/out of the system memory whenever it is necessary to do so. Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU to execute the process. All of these processes will be in system memory and ready to be executed by CPU.
9
Addition of Medium Term Scheduling
10
Context Switch When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process. Context switching operation is done by hardware, it is relatively working faster than software operation. Context-switch time is the additional waste overhead; the system does no useful work while switching. In other words, no CPU execution during the period of context switching from current process to new process. Time dependent on context switching hardware support
11
CPU Scheduling
18
Deadlocks
19
The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example – System has 2 disk drives. – Process P 1 and Process P 2 each hold one disk drive and each needs another one. Example – semaphores A and B, initialized to 1 Process P 0 Process P 1 wait (A);wait(B) wait (B);wait(A)
20
Bridge Crossing Example Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.
21
System Model Resource types Resources: R 1, R 2,..., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances. Each process utilizes a resource as follows: – Request: If a request cannot be granted immediately (if the resource is being used by another process), then the requesting process must wait until it can acquire the resource. – Use: The process can operate on the resource (for example, if the resource is a printer, the process can print on the printer). – Release: The process releases the resource. Remark: The request and release of resources are system calls via release() and request() commands.
22
Deadlock Characterization Mutual exclusion: only one process at a time can use a resource at any given time frame, other process has to wait. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P 0, P 1, …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P 0 is waiting for a resource that is held by P 0. Deadlock can arise if four conditions hold simultaneously.
23
Resource-Allocation Graph Process Resource Type with 4 instances P i requests instance of R j P i is holding an instance of R j PiPi RjRj PiPi RjRj
24
Example of a Resource Allocation Graph Process P1 is holding an instance of resource type R2 and waiting for an instance of resource type R1. Process P2 is holding an instance of resource R1 including an instance of resource type R2 while waiting for an instance of resource type R3. Process P3 is holding resource R3.
25
Resource Allocation Graph With A Deadlock n Process P1 is holding an instance of resource type R2 and waiting for an instance of resource type R1. n Process P2 is holding an instance of resource R1 including an instance of resource type R2 while waiting for an instance of resource type R3. n Process P3 is holding an instance of resource type R3 and now requesting additional instance of resource type R2. n Create circular deadlock condition.
26
Graph With A Cycle But No Deadlock n Circular deadlock can be released. n If process P2 finishes its process with the instance of resource type R1. Process P1 can then use the released instance of resource type R1 and proceed to finish its own process. n If process P4 finishes its process with the instance of resource type R2. Process P3 can then use the released instance of resource type R2 and proceed to finish its own process.
27
Basic Facts on deadlock condition If graph contains no cycles no deadlock. If graph contains a cycle – if only one instance per resource type, then deadlock ( a definite circular deadlock. – if several instances per resource type, there is a possibility of deadlock (if it is not a circular deadlock, it can be resolved later on).
28
Methods for Handling Deadlocks Use a protocol to prevent or avoid deadlock to ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state, detect it and then recover from it. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX and Windows. The operating system will provide information regarding to the status of all its resources. It is up to the application developer to write programs that handle any possible deadlocks.
29
Deadlock Prevention Mutual Exclusion – must hold for nonsharable resources. For example, Read-only files can be shared among processes. Mutual Exclusion doesn’t address the sharable issues. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. – Require process to request and be allocated all its resources before it begins execution. – Before it can request additional resources, however, it must release all resources that it is currently allocated. – Allow process to request resources only when the process has none. – Low resource utilization; starvation possible if most of the processes need the same resource. Restrain the ways request can be made.
30
Deadlock Prevention (Cont.) No Preemption – – If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. – Preempted resources are added to the list of resources for which the process is waiting. – Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. – This protocol cannot generally be applied to such resources as printers and tape drives. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. It is up to the application developer to impose the above circular wait algorithm.
31
Deadlock Avoidance Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Requires that the system has some additional a priori information available.
32
Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a sequence of ALL the processes is the systems such that for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i. That is: – If P i resource needs are not immediately available, then P i can wait until all P j have finished. – When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. – When P i terminates, P i +1 can obtain its needed resources, and so on.
33
Typical Avoidance algorithms Single instance of a resource type. Use a resource- allocation graph Multiple instances of a resource type. Use the banker’s algorithm
34
Banker’s Algorithm Multiple instances. Each process must a priori claim maximum use. When a process requests a resource it may have to wait. The Banker’s Algorithm will look at all resources and all incoming requests and allocates them accordingly if the system is still in safe state (using typical safety algorithm). When a process gets all its resources it must return them in a finite amount of time.
35
Virtual Memory
36
Virtual Memory That is Larger Than Physical Memory
37
Demand Paging Bring a page into memory only when it is needed (for execution) – Less I/O needed – Less memory needed – Faster response – More users Page is needed reference to it – invalid reference abort – not-in-memory bring to memory Lazy swapper – never swaps a page into memory unless page will be needed – Swapper that deals with pages is a pager
38
Transfer of a Paged Memory to Contiguous Disk Space
39
Valid-Invalid Bit With each page table entry a valid–invalid bit is associated (v in-memory, i not-in-memory) Initially valid–invalid bit is set to i on all entries Example of a page table snapshot: During address translation, if valid–invalid bit in page table entry is I page fault v v v v i i i …. Frame #valid-invalid bit page table
40
Page Table When Some Pages Are Not in Main Memory
41
Page Fault If there is a reference to a page, first reference to that page will trap to operating system: page fault 1.Operating system looks at another table to decide: – Invalid reference abort – Just not in memory 2.Get empty frame 3.Swap page (virtual memory) into frame (physical memory) 4.Reset tables 5.Set validation bit = v 6.Restart the instruction that caused the page fault
42
Steps in Handling a Page Fault
43
Page Replacement
44
First-In-First-Out (FIFO) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process) 4 frames Belady’s Anomaly: more frames more page faults 1 2 3 1 2 3 4 1 2 5 3 4 9 page faults 1 2 3 1 2 3 5 1 2 4 5 10 page faults 4 43
45
FIFO Page Replacement
46
Optimal Page Replacement
47
LRU Page Replacement
48
Thrashing If a process does not have “enough” pages, the page-fault rate is very high. This leads to: – low CPU utilization – operating system thinks that it needs to increase the degree of multiprogramming – another process added to the system Thrashing a process is busy swapping pages in and out
49
Thrashing (Cont.)
50
Q&A
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.