Operating System - Le Khac Nhien An - 13


Figure 3.19 Resource allocation graph


Methods of handling blockages


There are mainly three approaches to dealing with blockages:


Use a protocol to ensure that the system never gets stuck.


Allow blockages to occur and seek to correct them.


Completely ignore congestion handling, pretending the system never gets congested.


Prevent blockages


For congestion to not occur, at least one of the four conditions must not occur:


Non-shareable resources: In general, it is almost impossible to avoid this condition because the nature of the resource is almost fixed. However, for some rendering resources, one can use spooling mechanisms to transform them into sharable resources.


Resource reservation and request: it must be ensured that whenever a process requests a resource, it does not reserve other resources. One of the following two access mechanisms can be implemented:


The process must request all necessary resources before starting processing.

=> The difficulty with this method is that it is difficult for the process to accurately estimate the resources needed because the demand may depend on the processing. In addition, if the process occupies resources that are not needed immediately, the use of resources will be inefficient.


When a process requests a new resource and is denied, it must release the resources it is holding, and then reallocate them again at the same time as the new resource.


=> This method raises difficulties in protecting the data integrity of the system.


No resource reclaim: allows the system to reclaim resources from locked processes and re-allocate them to the process when it is released from the locked state. However, with some types of resources, reclaiming will be very difficult because it violates data integrity.


Existence of a cycle: avoid creating cycles in the graph by allocating resources according to a hierarchy like this:


Let R = {R 1 , R 2 ,...,Rm} be the set of resource types. Resource types are ranked from 1-N.

For example : F(disk) = 2, F(printer) = 12


Processes when requesting resources must comply with the rule: when a process is holding resource Ri, it can only request resources Rj if F(Rj) > F(Ri).


Avoid congestion


Congestion prevention is a major concern when it comes to resource utilization. Congestion avoidance is the elimination of all opportunities that could lead to congestion in the future. Complex mechanisms are required to accomplish this.


Some basic concepts


Safe state : State A is safe if the system can satisfy the resource demands (up to the maximum) of each process in a certain order while still preventing deadlock.


A safe allocation sequence: an order of processes <P 1 , P 2 ,...,Pn> is safe for the current allocation state if for each process Pi the resource demand of Pi can be satisfied with the free resources of the system, plus the resources currently held by other processes Pj, with j<i.

A safe state cannot be a deadlocked state. Conversely, an unsafe state can lead to a deadlocked state.


Allocation strategy : only satisfy the process's resource request when the resulting state is safe!


Algorithm for determining safe state The following data structures are required: int Available[NumResources];

/* Available[r]= number of free instances of resource r*/ int Max[NumProcs, NumResources];

/*Max[p,r]= maximum demand of process p for resource r*/ int Allocation[NumProcs, NumResources];

/* Allocation[p,r] = number of resources r actually allocated to p*/ int Need[NumProcs, NumResources];

/* Need[p,r] = Max[p,r] - Allocation[p,r]*/ 1. Suppose there are arrays

int Work[NumProcs, NumResources] = Available; int Finish[NumProcs] = false;

2. Find i such that Finish[i] == false Need[i] <= Work[i]

If there is no such i, go to step 4.


3. Work = Work + Allocation[i]; Finish[i] = true;

Go to step 2


4.If Finish[i] == true for all i, then the system is in a safe state. For example : Suppose the current state of the system is described as follows:

Max

Allocation

Available


R1

R2

R3

R1

R2

R3

R1

R2

R3

P1

3

2

2

1

0

0



P2

6

1

3

2

1

1



P3

3

1

4

2

1

1



P4

4

2

2

0

0

2



Maybe you are interested!


If process P2 requests 4 for R1, 1 for R3. Can this request be met without causing a deadlock? Seeing that Available[1] =4, Available[3] =2 is enough to satisfy P2's request, we have


Need

Allocation

Available


R1

R2

R3

R1

R2

R3

R1

R2

R3

P1

2

2

2

1

0

0



P2

0

0

1

6

1

2



P3

1

0

3

2

1

1



P4

4

2

0

0

0

2




Need

Allocation

Available


R1

R2

R3

R1

R2

R3

R1

R2

R3

P1

2

2

2

1

0

0



P2

0

0

0

0

0

0



P3

1

0

3

2

1

1



P4

4

2

0

0

0

2




Need

Allocation

Available


R1

R2

R3

R1

R2

R3

R1

R2

R3

0

0

0

0

0

0



P2

0

0

0

0

0

0



P3

1

0

3

2

1

1



P4

4

2

0

0

0

2



P1


Need

Allocation

Available


R1

R2

R3

R1

R2

R3

R1

R2

R3

P1

0

0

0

0

0

0



P2

0

0

0

0

0

0



P3

0

0

0

0

0

0



P4

4

2

0

0

0

2




Need

Allocation

Available


R1

R2

R3

R1

R2

R3

R1

R2

R3

P1

0

0

0

0

0

0



P2

0

0

0

0

0

0



P3

0

0

0

0

0

0



P4

0

0

0

0

0

0




The resulting state is safe and allocable. The algorithm requires resources.

Suppose process Pi requests k instances of resource r .


1.If k <= Need[i], go to step 2 Otherwise, an error occurs 2.If k <= Available[i], go to step 3 Otherwise, Pi must wait

3. Suppose the system has allocated Pi the resources it requested and updated the system status as follows:


Available[i] = Available[i] - k; Allocation[i]= Allocation[i]+ k; Need[i] = Need[i] - k;

If the resulting state is safe, the above resources will now be actually allocated to Pi.


On the contrary, Pi has to wait


Detect blockages


The following data structures are required: int Available[NumResources];

// Available[r]= number of free instances of resource r int Allocation[NumProcs, NumResources];

// Allocation[p,r] = number of resources r actually allocated to p int Request[NumProcs, NumResources];

// Request[p,r] = number of resources r that process p requests Congestion Detection Algorithm

1. int Work[NumResources] = Available; int Finish[NumProcs];

for (i = 0; i < NumProcs; i++) Finish[i] = (Allocation[i] == 0);

2. Find i such that Finish[i] == false Request[i] <= Work

If there is no such i, go to step 4.


3. Work = Work + Allocation[i]; Finish[i] = true;

Go to step 2

4. If Finish[i] == true for all i, then the system is deadlock free.

If Finish[i] == false for some value i,


then processes for which Finish[i] == false will be in deadlock.

Correcting congestion


Once a deadlock is detected, there are two main options for correcting the deadlock: Suspend the execution of the involved processes

This approach is based on reclaiming resources of terminated processes. One of the following two methods can be used:


-Suspend all processes in a deadlocked state


-Suspend each related process until there is no more deadlock cycle: to choose the appropriate process to be suspended, it is necessary to rely on factors such as priority, processing time, number of resources occupied, number of resources requested...


Resource Recovery


Deadlock can be corrected by preempting some resources from processes and allocating these resources to other processes until the deadlock cycle is eliminated. Three problems need to be solved:


-Choosing a victim: which process will have its resources revoked? and which resources will be revoked?


-Revert to pre-deadlock state: when reclaiming resources of a process, it is necessary to restore the state of the process back to the most recent previous state without deadlock.


-The "resource starvation" situation: how to ensure that no process is always deprived of resources?

Memory Management

This lesson will help you visualize the issues that need to be considered when designing the memory management module of the Operating System. Some memory organization models are also introduced and analyzed their advantages and disadvantages so that you can understand how memory allocation and recovery takes place.


Main memory is the only storage device through which the CPU can exchange information with the external environment, so the need to organize and manage memory is one of the top priorities of the operating system. Main memory is organized as a one-dimensional array of memory words, each memory word has an address. Information exchange with the external environment is done through reading or writing data to a specific address in memory.


Most modern operating systems allow multitasking to improve CPU utilization. However, this technique creates the need to share memory between different processes. The problem is that: « memory is finite and memory requirements are infinite ».


The operating system is responsible for allocating memory to processes that request it. To perform this task well, the operating system needs to consider many aspects:


Address correspondencelogical and physical addresses( physical ) : how to convert a symbolic address in the program to a real address in main memory?


Physical memory management : how to expand available memory to host multiple concurrent processes?


Information sharing : how to allow two processes to share information in memory?


Protection : How to prevent processes from invading memory allocated to other processes?


Memory management solutions are highly dependent on hardware characteristics and have gone through many stages of improvement to become the quite satisfactory solutions they are today.

Comment


Agree Privacy Policy *