logo资料库

操作系统内存分配算法.docx

第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
资料共7页,全文预览结束
操作系统实验 最佳拟合算法:最佳适配内存分配算法分配存储器中可用的最小空闲分区, 足以容纳系统内的进程。它在完整的内存中搜索可用的空闲分区,并将进程分配 到足够小的内存分区来保存进程。 思想: 总是把既能满足需求,又是最小的空闲分区分配给作业。 过程: 算法首先将所有空闲区按大小排序,以递增形式形成一个空白链, 每次找到的第一个满足需求的空闲区必然是最优的。 Bestfit 代码: #include int main() { int fragments[10], block[10], file[10], m, n, number_of_blocks, number_of_files, temp, lowest = 10000; static int block_arr[10], file_arr[10]; printf("\nEnter the Total Number of Blocks:\t"); scanf("%d", &number_of_blocks); printf("\nEnter the Total Number of Files:\t"); scanf("%d", &number_of_files); printf("\nEnter the Size of the Blocks:\n"); for (m = 0; m < number_of_blocks; m++) { } printf("Block No.[%d]:\t", m + 1); scanf("%d", &block[m]); printf("Enter the Size of the Files:\n"); for (m = 0; m < number_of_files; m++) { } printf("File No.[%d]:\t", m + 1); scanf("%d", &file[m]); for (m = 0; m < number_of_files; m++) { for (n = 0; n < number_of_blocks; n++)
if (block_arr[n] != 1) { } temp = block[n] - file[m]; if (temp >= 0) { } if (lowest > temp) { } file_arr[m] = n; lowest = temp; fragments[m] = lowest; { } block_arr[file_arr[m]] = 1; lowest = 10000; } printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment"); for (m = 0; m < number_of_files && block_arr[m] != 0; m++) { printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file[m], file_arr[m], block[file_arr[m]], fragments[m]); } printf("\n"); return 0; } NextFit 算法: 思想:在分配内存块时,算法首先适合找到一个空闲分区。下一次调用该算 法时,将从停止的位置开始搜索,而不是从头开始搜索。 过程: 算法读取每个文件写入内存的位置,将下一个可用内存块分配给后 续进程。当一个进程被执行存储在内存中时,会检查前一个 bin 或内存块的可用 性。如果它是空闲的,那么一个进程被写入同一个内存块,否则,检查下一个块。 NextFit 代码: #include
int main() { int memory_size[10][2], process_size[10][3]; int i, j, total_processes = 0, total_memory = 0; printf("\nEnter the Total Number of Processes:\t"); scanf("%d", &total_processes); printf("\nEnter the Size of Each Process\n"); for (int i = 0; i < total_processes; i++) { } printf("Enter Size of Process %d:\t", i + 1); scanf("%d", &process_size[i][0]); process_size[i][1] = 0; process_size[i][2] = i; printf("\nEnter Total Memory Blocks:\t"); scanf("%d", &total_memory); printf("\nEnter the Size of Each Block:\n"); for (i = 0; i < total_processes; i++) { } printf("Enter Size of Block %d:\t", i + 1); scanf("%d", &memory_size[i][0]); memory_size[i][1] = 0; for (i = 0; i < total_processes; i++) { while (j < total_memory) { if (memory_size[j][1] == 0 && process_size[i][0] <= memory_size[j][0]) { process_size[i][1] = 1; memory_size[j][1] = 1; printf("\nProcess [%d] Allocated to Memory Block:\t%d", i + 1, j + 1); break; } j++; } } for (i = 0; i < total_memory; i++) { } if (process_size[i][1] == 0) { } printf("\nProcess [%d] Unallocated\n", i + 1);
printf("\n"); return 0; } FirstFit 算法:即第一个适合算法,第一个适合的内存分配算法分配内存中可 用的第一个可用分区,足以容纳系统内的进程。它不会检查所需的最小空间,而 是选择能够处理该进程的第一个遇到的分区。 思想:使用内存中低地址部分的空闲分区,在高地址部分的空闲分区非常少 被利用,从而保留了高地址部分的大空闲区。 过程:首先给出 5 个分区,以及 4 个文件,在分配内存时,从空闲分区开始 查找,真到找到满足其大小的分区为止,再按文件的大小从分区中划出一块内存 给请求者,如此循环。 FirstFit 代码: #include int main() { static int block_arr[10], file_arr[10]; int fragments[10], blocks[10], files[10]; int m, n, number_of_blocks, number_of_files, temp; printf("\nEnter the Total Number of Blocks:\t"); scanf("%d", &number_of_blocks); printf("Enter the Total Number of Files:\t"); scanf("%d", &number_of_files); printf("\nEnter the Size of the Blocks:\n"); for (m = 0; m < number_of_blocks; m++) { } printf("Block No.[%d]:\t", m + 1); scanf("%d", &blocks[m]); printf("Enter the Size of the Files:\n"); for (m = 0; m < number_of_files; m++) { } printf("File No.[%d]:\t", m + 1); scanf("%d", &files[m]); for (m = 0; m < number_of_files; m++) { for (n = 0; n < number_of_blocks; n++)
{ } if (block_arr[n] != 1) { } temp = blocks[n] - files[m]; if (temp >= 0) { } file_arr[m] = n; break; fragments[m] = temp; block_arr[file_arr[m]] = 1; } printf("\nFile Number\tBlock Number\tFile Size\tBlock Size\tFragment"); for (m = 0; m < number_of_files; m++) { printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file_arr[m], files[m], blocks[file_arr[m]], fragments[m]); } printf("\n"); return 0; } WorstFit 算法:最差的适应性内存分配算法分配内存中可用的最大空闲分区, 足以保存系统内的进程。 思想:算法按大小递减的顺序形成空闲区链,分配时直接从空闲区链的第一个空闲分 区中分配(不能满足需要则不分配)。 过程:分配内存时,算法首先搜索完整的内存以获得可用的空闲分区,然后 将进程分配给最大的内存分区 WorstFit 代码: #include int main() { int fragments[10], blocks[10], files[10]; int m, n, number_of_blocks, number_of_files, temp, top = 0; static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t"); scanf("%d", &number_of_blocks); printf("Enter the Total Number of Files:\t"); scanf("%d", &number_of_files); printf("\nEnter the Size of the Blocks:\n"); for (m = 0; m < number_of_blocks; m++) { } printf("Block No.[%d]:\t", m + 1); scanf("%d", &blocks[m]); printf("Enter the Size of the Files:\n"); for (m = 0; m < number_of_files; m++) { } printf("File No.[%d]:\t", m + 1); scanf("%d", &files[m]); for (m = 0; m < number_of_files; m++) { for (n = 0; n < number_of_blocks; n++) { if (block_arr[n] != 1) { } temp = blocks[n] - files[m]; if (temp >= 0) { } if (top < temp) { } file_arr[m] = n; top = temp; fragments[m] = top; block_arr[file_arr[m]] = 1; top = 0; } } printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment"); for (m = 0; m < number_of_files; m++) { printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]); } printf("\n");
return 0; }
分享到:
收藏