Classic question types on operating systems: banker's algorithm for deadlock avoidance, dynamic partition allocation for continuous allocation management, and page replacement algorithm


1. Banker’s Algorithm

1.1 Purpose

Banker's algorithm is used to avoid deadlock and is the most famous deadlock avoidance algorithm.

Competition for resources and improper process advancement order will causelead to deadlock

The so-called deadlock refers to a deadlock caused by multiple processes competing for resources during operation. When a process is in this deadlock, it will be unable to move forward without external force.

  • Necessary conditions for deadlock:
    ① Mutual exclusion : The process requires exclusive control of the allocated resources, that is, a resource is only occupied by one process within a period of time
    ② Request and hold : When the process is blocked due to requesting resources , keep the obtained resources
    ③ No deprivation : The resources obtained by the process cannot be deprived before they are used up, and can only be released by themselves when they are used up
    ④ Loop waiting : When a deadlock occurs, there must be a Process – a circular chain of resources

1.2 Data structure

  • (1) The available resource vector Available
    is an array containing m elements, where each element (Available[i]) represents the i-th categoryNumber of resources available, its initial value is the number of resources of this type configured in the system, and its value changes dynamically with the allocation and recycling of this type of resources [
    Available[j] represents the number of type j resources in the system]
  • (2) The maximum demand matrix Max
    is an nxm matrix that defines the demand for m types of resources by each process in the system.Total demand
    [Max[i][j] represents the total demand for j-type resources by the i-th process]
  • (3) Allocation matrix Allocation
    This is also an nxm matrix, which defines each type of resource in the systemcurrently assignedThe number of resources given to each process
    [Allocation[i][j] represents the number of resources of the jth type that process i has currently obtained.
  • (4) Demand matrix Need
    is also an n×m matrix used to represent each process.still needThe number of various types of resources
    [Need[i][j] indicates that process i also needs the number of resources of the jth type]

There is the following relationship between the above nxm matrices:
         Need[i][j] = Max[i][j] - allocation[i][j]

1.3 Algorithm description

Resource allocation algorithm
Insert image description here
security algorithm (i.e., the security check part of the resource allocation algorithm in the above figure )
Insert image description here

1.4 Example description

Insert image description here
① Is T0 safe?
Insert image description here

②After time T0, if process P2 issues a resource request Request 2 (1,0,1), can the system allocate resources to it?
Insert image description here

③After process P2 applies for resources, if P1 issues a resource request Request 1 (1,0,1), can the system allocate resources to it?
④After process P1 applies for resources, if P3 issues a resource request Request 3 (0,0,1), can the system allocate resources to it?
Insert image description here


2. Continuous allocation management method

Continuous allocation method, that is, allocating a continuous memory space for a user program

2.1 Classification

single contiguous allocation

The simplest storage management method, which can only be used in single-user and single-task operating systems.

Divide the memory into the system area (low-end memory, allocated to the OS) and user area (high-end memory, allocated to users)

Static allocation is adopted, that is, once the job enters the memory, it must wait for it to finish running before the memory can be released.

Single contiguous allocation creates internal fragmentation

Fixed partition allocation

The earliest storage management method that can run multi-programs

Divide the memory space into several fixed-size partitions. Except for the OS which occupies one area, the remaining partitions are loaded with a program. The sizes of the partitions can be unequal, but they must be determined in advance and cannot be changed during runtime, that is, the partition size and Boundaries cannot change at runtime

In fixed partition allocation, programs are usually loaded into memory using static relocation. When a user program is to be loaded into memory, the memory allocation program retrieves the partition description table and finds a partition in the table that meets the requirements and has not yet been allocated. assigned to this program
Insert image description here

It is not possible to share a main memory area with multiple processes, the utilization rate is low, and internal fragmentation will occur.

Dynamic partition allocation

Also known as variable partition allocation, it is a partitioning method that dynamically divides memory.

The memory is not divided into partitions in advance. Instead, when the job enters the memory, the partition is dynamically created according to the size of the job, and the size of the partition is exactly adapted to the needs of the job. Therefore, the size of the partition in the system is variable . The number of partitions is also variable

  • Data structures in partition allocation:
    ① Free partition table
    ② Free partition chain

Insert image description here

See 2.2 for dynamic partition allocation algorithm.


2.2 ☆Dynamic partition allocation algorithm

First adaptation algorithm FF

FF:First Fit

Free partitions (chains) are arranged in increasing order of address
. When allocating memory, search sequentially from the free partition table/chain head until the first free partition that meets the requirements is found . Then according to the job size, a memory space is allocated from the partition to the requester, and the remaining free partitions remain in the free partition table/chain.

Prioritize the use of low address space, but after the low address space is divided multiple times, external fragmentation will occur

Next time adaptation algorithm NF

NF:Next Fit

Also known as the cyclic first adaptation algorithm, that is, based on the first adaptation algorithm, the queue is changed to a circular queue (the free partitions (chains) are still arranged in the order of increasing addresses ), and each time the next free partition found last time is Partition search

Evenly distributed, but lacks large free partitions

Best fit algorithm BF

BF:Best Fit

The free partition table/chain is arranged in increasing order of capacity . When allocating memory, search sequentially from the beginning of the free partition table/chain until the first free partition that meets its size requirements is found.

External fragmentation is very small and can always be assigned to the most appropriate partition for the job and large partitions are reserved, but it creates a lot of fragmented space that is difficult to use.

Worst fit algorithm WF

WF:Worst Fit

The free partition table/chain is arranged in decreasing order of capacity . When allocating memory, the free partition table/chain is searched sequentially from the beginning until the first free partition larger than it is found . The remaining free partitions remain in Free partition table/chain

The remaining space after allocation is relatively large and suitable for next use, but the large space will be used up.

Example explanation

Insert image description here


3. Page replacement algorithm

3.1 Optimal replacement algorithm OPT

When the page number reference string of a process is known in advance, the pages that will no longer be used in the future or will be used at the latest in the future are eliminated every time.

The optimal replacement algorithm is the best and has the lowest page fault rate. However, in actual operations, it is often impossible to know in advance all the page information that will be referenced in the future, so it cannot be implemented. It can only be used as a standard to measure the advantages and disadvantages of other replacement algorithms.

3.2 First-in-first-out algorithm FIFO

The simplest page replacement algorithm, always eliminates the page that first enters the memory every time

3.3 Least Recently Used Algorithm LRU

Select pages that have not been used for the longest period of time and eliminate them

This algorithm can be implemented using a register combination stack and has good performance. Among the commonly used page replacement algorithms, this algorithm is closest to the optimal replacement algorithm.

3.4 Example explanation

Insert image description here

Guess you like

Origin blog.csdn.net/m0_50609545/article/details/122300305