先决条件:分区分配方法
什么是Next Fit?
下一个适合度是“首次适合度”的修改版本。它首先是找到一个可用分区的开始,但是在下次调用时,它从中断的地方开始搜索,而不是从头开始。该策略使用了漫游指针。指针沿着存储链移动,以寻找下一个合适的位置。这有助于避免总是从空闲块链的开头(开头)开始使用内存。
相较于首次健身,它有什么优势?
- 首次拟合是一种直接且快速的算法,但是倾向于将大量的自由零件切成小块,因此,即使所有小块的总和大于零,需要大量内存块的进程也不会得到任何结果这就是所谓的外部碎片问题。
- 首次拟合的另一个问题是,它倾向于在内存的开头分配内存部分,这可能会导致在开头出现更多的内部碎片。 Next 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
算法:
- 输入存储块的数量及其大小,然后将所有块初始化为空闲状态。
- 输入进程数及其大小。
- 首先选择每个进程,并检查是否可以将其分配给当前块;如果是,则为其分配所需的内存,并检查下一个进程,但从我们没有开始的那个块开始。
- 如果当前块大小较小,则继续检查其他块。
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
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;
}
}
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
?>
输出:
Process No. Process Size Block no.
1 10 2
2 20 3
3 5 1