(实验目的、实验原理、实验步骤、内容、程序代码、实验数据、结论等)
1.实验目的
详细了解系统之中是如何存储进程的。 通过实验加强对基于顺序搜索的动态分区分配算法的理解和掌握。 加深理解有关存储结构的概念。 主存的分配和回收的实现与主存储器的管理方式有关的,通过本实验帮助学生理解在可变分区管理方式下应怎样实现主存空间的分配和回收。
2.实验要求
代码实现四种算法
撰写课程设计报告 报告要有设计、实现、测试等过程。
3.实验过程描述
原理(思路)
态分区分配是根据进程的实际需要,动态地址为之分配内存空间,而在分配时,须按照一定的分配算法,从空闲分区表或空闲分区链中选出一分区分配给该作业。 在本实验中运用了四种分配算法,分别是首次适应算法,循环首次适应算法,最坏适应算法,最佳适应算法。
4.实验代码
package 实验三;
import java.util.Arrays;
import java.util.Scanner;
public class DpAllocation {
static process[] processS = {new process(0, 2, 20),
new process(3, 7, 10),
new process(6, 8, 15),
new process(9, 4, 15),
new process(12, 4, 15),
new process(15, 1, 10)};
static int Can_space = 0;//可用空间
static int[][] Current = new int[0][0];//当前空间内元素
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入可用空间数");
Can_space = in.nextInt();
int n = -1;
System.out.println("请输入选择 1: 首次适应 2:循环首次适应 3:最坏适应算法 4:最佳适应算法");
n = in.nextInt();
switch (n) {
case 0:
break;
case 1:
First_Fit();
break;
case 2:
Next_Fit();
break;
case 3:
Worst_Fit();
break;
case 4:
Best_Fit();
break;
default:
System.out.println("无效输入");
}
}
private static void First_Fit() {
int Current_time = 0;
for (int i = 0; i < processS.length; i++) {//开始输入所有进程
Current_time = processS[i].inputTime;//获取当前时间
for (int j = Current.length - 1; j >= 0; j--) {//时间之前的全部消失
if (Current[j][3] + processS[Current[j][2]].runTime <= Current_time) {
Current = Delete(j);
}
}
if (Current.length == 0) {
if (Can_space >= processS[i].space)
Current = add(0, processS[i].space, i, processS[i].inputTime, 0);
printprocess(Current_time);
} else {
int Begin = 0;//开始空间
int space = 0;//可用空间
for (int j = 0; j <= Current.length; j++) {//开始空间 结束空间 占用进程 输入时间
if (j == Current.length) {
space = Can_space - Current[j - 1][1];
} else {
space = Current[j][0] - Begin;
}
if (space >= processS[i].space) {
Current = add(Begin, Begin + processS[i].space, i, Current_time, j);
printprocess(Current_time);
break;
}
if (j != Current.length) {
Begin = Current[j][1];
}
}
}
}
}
private static void Next_Fit() {
int Current_time = 0;
for (int i = 0; i < processS.length; i++) {//开始输入所有进程
Current_time = processS[i].inputTime;//获取当前时间
for (int j = Current.length - 1; j >= 0; j--) {//时间之前的全部消失
if (Current[j][3] + processS[Current[j][2]].runTime <= Current_time) {
Current = Delete(j);
}
}
if (Current.length == 0) {
if (Can_space >= processS[i].space)
Current = add(0, processS[i].space, i, processS[i].inputTime, 0);
printprocess(Current_time);
} else {
int Begin = Current[Current.length - 1][1];//开始空间
int space = 0;//可用空间
boolean be = true;
for (int j = Current.length; j <= Current.length; j++) {//开始空间 结束空间 占用进程 输入时间
if (j == Current.length) {
space = Can_space - Current[j - 1][1];
} else {
space = Current[j][0] - Begin;
}
if (space >= processS[i].space) {
Current = add(Begin, Begin + processS[i].space, i, Current_time, j);
printprocess(Current_time);
break;
}
if (j != Current.length) {
Begin = Current[j][1];
}
if (j == Current.length && be) {
be = false;
Begin = 0;
j = -1;
}
}
}
}
}
private static void Worst_Fit() {
int Current_time = 0;
for (int i = 0; i < processS.length; i++) {//开始输入所有进程
Current_time = processS[i].inputTime;//获取当前时间
for (int j = Current.length - 1; j >= 0; j--) {//时间之前的全部消失
if (Current[j][3] + processS[Current[j][2]].runTime <= Current_time) {
Current = Delete(j);
}
}
if (Current.length == 0) {
if (Can_space >= processS[i].space)
Current = add(0, processS[i].space, i, processS[i].inputTime, 0);
printprocess(Current_time);
} else {
int danqiangBegin = 0;//开始空间
int danqiangspace = 0;//当前空间
int index = -1;
int Begin = 0;//开始空间
int space = 0;//可用空间
for (int j = 0; j <= Current.length; j++) {//开始空间 结束空间 占用进程 输入时间
if (j == Current.length) {
space = Can_space - Current[j - 1][1];
} else {
space = Current[j][0] - Begin;
}
if (space >= processS[i].space && space > danqiangspace) {
index = j;
danqiangspace = space;
danqiangBegin = Begin;
}
if (j != Current.length) {
Begin = Current[j][1];
}
}
if (index != -1) {
Current = add(danqiangBegin, danqiangBegin + processS[i].space, i, Current_time, index);
printprocess(Current_time);
}
}
}
}
private static void Best_Fit() {
int Current_time = 0;
for (int i = 0; i < processS.length; i++) {//开始输入所有进程
Current_time = processS[i].inputTime;//获取当前时间
for (int j = Current.length - 1; j >= 0; j--) {//时间之前的全部消失
if (Current[j][3] + processS[Current[j][2]].runTime <= Current_time) {
Current = Delete(j);
}
}
if (Current.length == 0) {
if (Can_space >= processS[i].space)
Current = add(0, processS[i].space, i, processS[i].inputTime, 0);
printprocess(Current_time);
} else {
int danqiangBegin = 0;//开始空间
int danqiangspace = -1;//当前空间
int index = -1;
int Begin = 0;//开始空间
int space = 0;//可用空间
for (int j = 0; j <= Current.length; j++) {//开始空间 结束空间 占用进程 输入时间
if (j == Current.length) {
space = Can_space - Current[j - 1][1];
} else {
space = Current[j][0] - Begin;
}
if (space >= processS[i].space && (danqiangspace == -1 || space < danqiangspace)) {
index = j;
danqiangspace = space;
danqiangBegin = Begin;
}
if (j != Current.length) {
Begin = Current[j][1];
}
}
if (index != -1) {
Current = add(danqiangBegin, danqiangBegin + processS[i].space, i, Current_time, index);
printprocess(Current_time);
}
}
}
}
private static int[][] add(int begin, int end, int j, int run, int index0) {
int[][] copy = new int[Current.length + 1][];
int index = 0;
if (0 == index0) {
copy[index++] = new int[]{begin, end, j, run};
for (int i = 0; i < Current.length; i++) {
copy[index++] = Current[i];
}
return copy;
}
index = 0;
for (int i = 0; i < Current.length; ) {
copy[index++] = Current[i];
i++;
if (i == index0) {
copy[index++] = new int[]{begin, end, j, run};
}
}
return copy;
}
private static int[][] Delete(int j) {
int[][] copy = new int[Current.length - 1][];
int index = 0;
for (int i = 0; i < Current.length; i++) {
if (i != j) {
copy[index++] = Current[i];
}
}
return copy;
}
private static void printprocess(int time) {
System.out.println("当前时间为" + time);
System.out.println("开始空间 结束空间 占用进程 输入时间");
for (int[] i : Current) {
System.out.println(" " + i[0] + " " + i[1] + " " + processS[i[2]].processname + " " + i[3]);
}
}
}
辅助类
package 实验三;
public class process {
int inputTime ;//输入时间
int runTime;//运行时间
int space;//空间
static int name=0;
String processname;
boolean bool=false;
public process(int inputTime, int runTime, int space) {
this.inputTime = inputTime;
this.runTime = runTime;
this.space = space;
processname="进程"+name++;
}
}
上一个操作系统之 银行家算法(java)
下一个操作系统之LRU算法(java)
如果本文章对你有用 记得点赞哦 ( ̄∇ ̄)