📜  内存管理中的 Next Fit 算法程序

📅  最后修改于: 2021-10-26 06:40:18             🧑  作者: Mango

先决条件:分区分配方法
什么是下一个适合?
Next fit 是“first fit”的修改版本。它从第一个适合找到空闲分区的地方开始,但是下次调用时,它从停止的地方开始搜索,而不是从头开始。此策略使用漫游指针。指针沿着内存链移动以搜索下一个匹配项。这有助于避免总是从空闲块链的头部(开始)使用内存。
与首次拟合相比,它的优势是什么?

  • 首次拟合是一种直接且快速的算法,但倾向于将大部分空闲部分切成小块,因此,即使所有小块的总和大于它,需要大量内存块的进程也不会得到任何东西需要,这就是所谓的外部碎片问题。
  • first fit的另一个问题是它倾向于在内存的开头分配内存部分,这可能会导致开头的内部碎片较多。 Next fit 尝试通过不是从内存的开始而是从它上次结束的位置开始搜索零件的空闲部分来解决这个问题。
  • Next fit 是一种非常快速的搜索算法,并且也比 First Fit 和 Best Fit 内存管理算法相对更快。
Example:
Input :  blockSize[] = {5, 10, 20};
     processSize[] = {10, 20, 30};
Output:
Process No.     Process Size    Block no.
 1              10              2
 2              20              3
 3              30              Not Allocated

算法:

  1. 输入内存块的数量及其大小,并将所有块初始化为空闲。
  2. 输入进程数及其大小。
  3. 首先选择每个进程并检查它是否可以分配给当前块,如果是,则为其分配所需的内存并检查下一个进程,但从我们离开的块开始。
  4. 如果当前块大小较小,则继续检查更多块。

下一个适合

C++
// C/C++ program for next fit
// memory management algorithm
#include 
using namespace std;
 
// Function to allocate memory to blocks as per Next fit
// algorithm
void NextFit(int blockSize[], int m, int processSize[], int n)
{
    // Stores block id of the block allocated to a
    // process
    int allocation[n], j = 0;
 
    // Initially no block is assigned to any process
    memset(allocation, -1, sizeof(allocation));
 
    // pick each process and find suitable blocks
    // according to its size ad assign to it
    for (int i = 0; i < n; i++) {
 
        // Do not start from beginning
        while (j < m) {
 
            if (blockSize[j] >= processSize[i]) {
 
                // allocate block j to p[i] process
                allocation[i] = j;
 
                // Reduce available memory in this block.
                blockSize[j] -= processSize[i];
 
                break;
            }
 
            // mod m will help in traversing the blocks from
            // starting block after we reach the end.
            j = (j + 1) % m;
        }
    }
 
    cout << "\nProcess No.\tProcess Size\tBlock no.\n";
    for (int i = 0; i < n; i++) {
        cout << " " << i + 1 << "\t\t" << processSize[i]
             << "\t\t";
        if (allocation[i] != -1)
            cout << allocation[i] + 1;
        else
            cout << "Not Allocated";
        cout << endl;
    }
}
 
// Driver program
int main()
{
    int blockSize[] = { 5, 10, 20 };
    int processSize[] = { 10, 20, 5 };
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);
 
    NextFit(blockSize, m, processSize, n);
 
    return 0;
}


Java
// Java program for next fit
// memory management algorithm
import java.util.Arrays;
 
public class GFG {
 
// Function to allocate memory to blocks as per Next fit
// algorithm
    static void NextFit(int blockSize[], int m, int processSize[], int n) {
        // Stores block id of the block allocated to a
        // process
        int allocation[] = new int[n], j = 0;
 
        // Initially no block is assigned to any process
        Arrays.fill(allocation, -1);
 
        // pick each process and find suitable blocks
        // according to its size ad assign to it
        for (int i = 0; i < n; i++) {
 
            // Do not start from beginning
            int count =0;
            while (j < m) {
                count++;    //makes sure that for every process we traverse through entire array maximum once only.This avoids the problem of going into infinite loop if memory is not available
                if (blockSize[j] >= processSize[i]) {
 
                    // allocate block j to p[i] process
                    allocation[i] = j;
 
                    // Reduce available memory in this block.
                    blockSize[j] -= processSize[i];
 
                    break;
                }
 
                // mod m will help in traversing the blocks from
                // starting block after we reach the end.
                j = (j + 1) % m;
            }
        }
 
        System.out.print("\nProcess No.\tProcess Size\tBlock no.\n");
        for (int i = 0; i < n; i++) {
            System.out.print( i + 1 + "\t\t" + processSize[i]
                    + "\t\t");
            if (allocation[i] != -1) {
                System.out.print(allocation[i] + 1);
            } else {
                System.out.print("Not Allocated");
            }
            System.out.println("");
        }
    }
 
// Driver program
    static public void main(String[] args) {
        int blockSize[] = {5, 10, 20};
        int processSize[] = {10, 20, 5};
        int m = blockSize.length;
        int n = processSize.length;
        NextFit(blockSize, m, processSize, n);
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for next fit
# memory management algorithm
 
# Function to allocate memory to
# blocks as per Next fit algorithm
def NextFit(blockSize, m, processSize, n):
     
    # Stores block id of the block
    # allocated to a process
 
    # Initially no block is assigned
    # to any process
    allocation = [-1] * n
    j = 0
 
    # pick each process and find suitable blocks
    # according to its size ad assign to it
    for i in range(n):
 
        # Do not start from beginning
        while j < m:
 
            if blockSize[j] >= processSize[i]:
 
                # allocate block j to p[i] process
                allocation[i] = j
 
                # Reduce available memory in this block.
                blockSize[j] -= processSize[i]
 
                break
 
            # mod m will help in traversing the
            # blocks from starting block after
            # we reach the end.
            j = (j + 1) % m
 
    print("Process No. Process Size Block no.")
    for i in range(n):
        print(i + 1, "         ", processSize[i],
                                    end = "     ")
        if allocation[i] != -1:
            print(allocation[i] + 1)
        else:
            print("Not Allocated")
 
# Driver Code
if __name__ == '__main__':
    blockSize = [5, 10, 20]
    processSize = [10, 20, 5]
    m = len(blockSize)
    n = len(processSize)
 
    NextFit(blockSize, m, processSize, n)
     
# This code is contributed by PranchalK


C#
// C# program for next fit
// memory management algorithm
using System;
using System.Linq;
public class GFG {
 
// Function to allocate memory to blocks as per Next fit
// algorithm
    static void NextFit(int []blockSize, int m,
                            int []processSize, int n) {
        // Stores block id of the block allocated to a
        // process
        int []allocation = new int[n];
        int j = 0;
 
        // Initially no block is assigned to any process
        Enumerable.Repeat(-1, n).ToArray();
 
        // pick each process and find suitable blocks
        // according to its size ad assign to it
        for (int i = 0; i < n; i++) {
 
            // Do not start from beginning
            while (j < m) {
 
                if (blockSize[j] >= processSize[i]) {
 
                    // allocate block j to p[i] process
                    allocation[i] = j;
 
                    // Reduce available memory in this block.
                    blockSize[j] -= processSize[i];
 
                    break;
                }
 
                // mod m will help in traversing the blocks from
                // starting block after we reach the end.
                j = (j + 1) % m;
            }
        }
 
        Console.Write("\nProcess No.\tProcess Size\tBlock no.\n");
        for (int i = 0; i < n; i++) {
            Console.Write( i + 1 + "\t\t" + processSize[i]
                    + "\t\t");
            if (allocation[i] != -1) {
                Console.Write(allocation[i] + 1);
            } else {
                Console.Write("Not Allocated");
            }
            Console.WriteLine("");
        }
    }
 
// Driver program
    static public void Main() {
        int []blockSize = {5, 10, 20};
        int []processSize = {10, 20, 5};
        int m = blockSize.Length;
        int n = processSize.Length;
        NextFit(blockSize, m, processSize, n);
    }
}
 
/*This code is contributed by Rajput-Ji*/


PHP
= $processSize[$i])
            {
 
                // allocate block j to p[i] process
                $allocation[$i] = $j;
 
                // Reduce available memory in this block.
                $blockSize[$j] -= $processSize[$i];
 
                break;
            }
 
            // mod m will help in traversing the blocks from
            // starting block after we reach the end.
            $j = ($j + 1) % $m;
        }
    }
 
    echo "\nProcess No.\tProcess Size\tBlock no.\n";
    for ($i = 0; $i < $n; $i++)
    {
        echo " ".($i + 1)."\t\t".$processSize[$i]."\t\t";
        if ($allocation[$i] != -1)
            echo ($allocation[$i] + 1);
        else
            echo "Not Allocated";
        echo "\n";
    }
}
 
    // Driver program
    $blockSize = array( 5, 10, 20 );
    $processSize = array( 10, 20, 5 );
    $m = count($blockSize);
    $n = count($processSize);
 
    NextFit($blockSize, $m, $processSize, $n);
 
 
// This code is contributed by mits
?>


Javascript


输出:
Process No.    Process Size    Block no.
 1               10              2
 2               20              3
 3               5               1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程