├── README.md ├── 进程调度 ├── 进程调度.png └── Test.java ├── 银行家算法 ├── 源码.zip ├── Bank(1).java ├── 整体流程图.xmind └── Bank.java └── 动态内存分配 ├── 动态内存.png ├── Main.java └── Test2.java /README.md: -------------------------------------------------------------------------------- 1 | # operateSystem 2 | 进程调度算法、内存管理算法、银行家算法(Java实现),可用于课设 3 | -------------------------------------------------------------------------------- /进程调度/进程调度.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlackerGod/operateSystem/HEAD/进程调度/进程调度.png -------------------------------------------------------------------------------- /银行家算法/源码.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlackerGod/operateSystem/HEAD/银行家算法/源码.zip -------------------------------------------------------------------------------- /动态内存分配/动态内存.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlackerGod/operateSystem/HEAD/动态内存分配/动态内存.png -------------------------------------------------------------------------------- /银行家算法/Bank(1).java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlackerGod/operateSystem/HEAD/银行家算法/Bank(1).java -------------------------------------------------------------------------------- /银行家算法/整体流程图.xmind: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlackerGod/operateSystem/HEAD/银行家算法/整体流程图.xmind -------------------------------------------------------------------------------- /动态内存分配/Main.java: -------------------------------------------------------------------------------- 1 | package Experiment; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static Test2 user; //声明一个用户; 7 | public static void Init(){ //初始化内存大小 8 | System.out.println("请输入内存大小(如果输入0,则按默认400分配)"); 9 | Scanner scanner=new Scanner(System.in); 10 | int choose=scanner.nextInt(); 11 | if(choose==0){ 12 | user=new Test2(); 13 | }else { 14 | user=new Test2(choose); 15 | } 16 | } 17 | public static void mainMenu(){ 18 | System.out.println("***********************"); 19 | System.out.println("* 1.输入作业 *"); 20 | System.out.println("* 2.回收内存块 *"); 21 | System.out.println("* 3.展示分区情况 *"); 22 | System.out.println("* 4.退出系统 *"); 23 | System.out.println("***********************"); 24 | System.out.println("请输入你要进行的操作:"); 25 | } 26 | public static void algorithmMenu(int size){ 27 | System.out.println("请选择分配算法:"); 28 | System.out.println("1.首次适应算法"); 29 | System.out.println("2.循环首次算法"); 30 | System.out.println("3.最佳适应算法"); 31 | System.out.println("4.最坏适应算法"); 32 | Scanner in = new Scanner(System.in); 33 | int choose = in.nextInt(); 34 | switch (choose){ 35 | case 1: 36 | user.FirstFit(size);break; 37 | case 2: 38 | user.NextFit(size);break; 39 | case 3: 40 | user.BestFit(size);break; 41 | case 4: 42 | user.WorstFit(size);break; 43 | default: 44 | System.out.println("请重新选择!"); 45 | } 46 | } 47 | public static void main(String[] args) { 48 | Init(); //初始化内存条大小 49 | Scanner sc=new Scanner(System.in); 50 | 51 | while(true){ 52 | mainMenu(); //主菜单 53 | int choice=sc.nextInt(); 54 | if(choice == 4){ 55 | return; 56 | }else if(choice == 1){ 57 | System.out.println("请输入你要插入的内存块大小"); 58 | int length=sc.nextInt(); 59 | algorithmMenu(length); 60 | }else if(choice==2){ 61 | System.out.println("请输入你要回收的分区号码"); 62 | int ID=sc.nextInt(); 63 | user.collection(ID); 64 | }else if(choice == 3){ 65 | user.showZones(); 66 | }else{ 67 | System.out.println("输入错误,请检查"); 68 | } 69 | } 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /动态内存分配/Test2.java: -------------------------------------------------------------------------------- 1 | package Experiment; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Scanner; 5 | public class Test2{ 6 | private int size; //内存大小 7 | private LinkedList zones; //内存分区 8 | private int pointer; //上次分配的空闲区位置 9 | 10 | class Zone{ //分区结点信息类 11 | private int size; //分区大小 12 | private int head; //分区起始位置 13 | private boolean isFree; //空闲状态 14 | 15 | public Zone(int head, int size) { 16 | this.head = head; 17 | this.size = size; 18 | this.isFree = true; 19 | } 20 | } 21 | public Test2(){ 22 | this.size = 400; 23 | this.pointer = 0; 24 | this.zones = new LinkedList<>(); 25 | zones.add(new Zone(0, size)); 26 | } 27 | public Test2(int size) { 28 | this.size = size; 29 | this.pointer = 0; 30 | this.zones = new LinkedList<>(); 31 | zones.add(new Zone(0, size)); 32 | } 33 | 34 | private void doAlloc(int size, int location, Zone tmp) { 35 | //size 作业大小 location 内存位置 tmp可用空闲区 36 | Zone newHome = new Zone(tmp.head + size, tmp.size - size); 37 | //产生一个新空闲区; 38 | zones.add(location + 1, newHome); 39 | tmp.size = size; 40 | tmp.isFree = false; 41 | System.out.println("成功分配 " + size + "KB 内存!"); 42 | } 43 | public void FirstFit(int size){ 44 | //将point赋值为零 从头开始找 45 | for (pointer = 0; pointer < zones.size(); pointer++){ 46 | Zone tmp = zones.get(pointer); 47 | //找到可用分区(空闲且大小足够) 48 | if (tmp.isFree && (tmp.size > size)){ 49 | doAlloc(size, pointer, tmp); 50 | return; 51 | } 52 | } 53 | //遍历结束后未找到可用分区, 则内存分配失败 54 | System.out.println("无可用内存空间!"); 55 | } 56 | public void NextFit(int size){ 57 | //直接获得上次point位置,开始遍历分区链表 58 | Zone tmp = zones.get(pointer); 59 | if (tmp.isFree && (tmp.size > size)){ 60 | doAlloc(size, pointer, tmp); 61 | return;//查看当前块是否符合要求 62 | } 63 | int len = zones.size(); 64 | int i = (pointer + 1) % len; //循环的过程, 65 | for (; i != pointer; i = (i+1) % len){ 66 | tmp = zones.get(i); 67 | //找到可用分区(空闲且大小足够) 68 | if (tmp.isFree && (tmp.size > size)){ 69 | doAlloc(size, i, tmp); 70 | return; 71 | } 72 | } 73 | //遍历结束后未找到可用分区, 则内存分配失败 74 | System.out.println("无可用内存空间!"); 75 | } 76 | public void BestFit(int size){ 77 | //最佳适应算法 78 | int flag = -1;//记录位置 79 | int min = this.size;//初始化为内存长度,用来保存最小碎片大小; 80 | for (pointer = 0; pointer < zones.size(); pointer++){//从头开始遍历; 81 | Zone tmp = zones.get(pointer); 82 | if (tmp.isFree && (tmp.size > size)){//找到合适条件的内存区 83 | if (min > tmp.size - size){//如果产生的碎片小于上一个最小值,就重新标记最小值位置 84 | min = tmp.size - size; 85 | flag = pointer;//更新位置 86 | } 87 | } 88 | } 89 | if (flag == -1){//初始值,没找到 90 | System.out.println("无可用内存空间!"); 91 | }else { 92 | doAlloc(size, flag, zones.get(flag)); 93 | } 94 | } 95 | public void WorstFit(int size){//最坏适应算法 96 | int flag = -1; 97 | int max = 0;//类似于最佳算法 98 | for (pointer = 0; pointer < zones.size(); pointer++){ 99 | Zone tmp = zones.get(pointer); 100 | if (tmp.isFree && (tmp.size > size)){ 101 | if (max < tmp.size - size){ 102 | max = tmp.size - size; 103 | flag = pointer; 104 | } 105 | } 106 | } 107 | if (flag == -1){ 108 | System.out.println("无可用内存空间!"); 109 | }else { 110 | doAlloc(size, flag, zones.get(flag)); 111 | } 112 | } 113 | public void collection(int id){//id 指定要回收的分区号 114 | if (id >= zones.size()){ 115 | System.out.println("无此分区编号!"); 116 | return; 117 | } 118 | Zone tmp = zones.get(id);//找到分区块 119 | int size = tmp.size;//取出大小 120 | if (tmp.isFree) {//判断状态 121 | System.out.println("指定分区未被分配, 无需回收"); 122 | return; 123 | } 124 | 125 | //检查前面是否空闲,是则合并 126 | if (id > 0 && zones.get(id - 1).isFree){ 127 | Zone fronKuer = zones.get(id - 1); 128 | fronKuer.size += tmp.size; 129 | zones.remove(id); 130 | id--; 131 | } 132 | 133 | //检查后面是否空闲,是则合并 134 | if (id < zones.size() - 1 && zones.get(id + 1).isFree){ 135 | Zone nextKuer = zones.get(id + 1);//取到下一个区块 136 | tmp.size += nextKuer.size; 137 | zones.remove(nextKuer); 138 | } 139 | zones.get(id).isFree = true; 140 | System.out.println("内存回收成功!, 本次回收了 " + size + "KB 空间!"); 141 | } 142 | public void showZones(){//展示内存分区状况 143 | System.out.println("***************************************"); 144 | System.out.println("*分区编号 分区始址 分区大小 空闲状态 *"); 145 | for (int i = 0; i < zones.size(); i++){ 146 | Zone tmp = zones.get(i); 147 | System.out.println("* "+i + " " 148 | + tmp.head + " " 149 | + tmp.size + " " 150 | + tmp.isFree+" *"); 151 | } 152 | System.out.println("****************************************"); 153 | } 154 | 155 | } -------------------------------------------------------------------------------- /进程调度/Test.java: -------------------------------------------------------------------------------- 1 | package Experiment; 2 | 3 | import java.util.*; 4 | 5 | class process implements Comparable{ 6 | public String Name;//标识符 7 | public int InTime;//进入内存时间 8 | public int ServerTime;//服务时间 9 | public int priority;//优先级 10 | public int StartTime;//开始时间 11 | public int FinishTime;//完成时间 12 | public int TurnaroundTime=0;//周转时间 13 | public Double AddTurnaroundTime=0.0;//带权周转时间 14 | 15 | @Override 16 | public int compareTo(process o) { 17 | return this.InTime - o.InTime; 18 | } 19 | } 20 | public class Test { 21 | public static List result=new ArrayList<>();//进程 22 | 23 | public static void Input(){ 24 | //初始化进程属性 25 | System.out.println("请输入你要插入进程数"); 26 | Scanner sc=new Scanner(System.in); 27 | int count=sc.nextInt(); 28 | for(int i=0;i queue=new LinkedList<>();//内存队列 72 | Collections.sort(result); 73 | for(process newProcess:result){ 74 | queue.offer(newProcess); 75 | } 76 | process tmp=queue.peek();//第一个输出的值 77 | tmp.StartTime=tmp.InTime; 78 | tmp.FinishTime=tmp.ServerTime+tmp.StartTime; 79 | tmp.TurnaroundTime=tmp.FinishTime-tmp.InTime; 80 | tmp.AddTurnaroundTime=(double)tmp.TurnaroundTime/(double) tmp.ServerTime; 81 | PrintForm(); 82 | Print(tmp); 83 | while (!queue.isEmpty()){ 84 | process tmpe=queue.poll(); 85 | if(queue.isEmpty()){ 86 | break; 87 | } 88 | process top=queue.peek(); 89 | if(tmpe.FinishTime>top.InTime){ 90 | top.StartTime=tmpe.FinishTime; 91 | } else{ 92 | top.StartTime=top.InTime; 93 | } 94 | top.FinishTime=top.StartTime+top.ServerTime; 95 | top.TurnaroundTime=top.FinishTime-top.InTime; 96 | top.AddTurnaroundTime=(double)top.TurnaroundTime/(double) top.ServerTime; 97 | Print(top); 98 | } 99 | AverNum(); 100 | } 101 | public static void SpfSort(int start, int end){ //根据服务时间排序 102 | for(int i = start; i < end; i++){ 103 | for(int j = i + 1; j < end; j++){ 104 | if(result.get(i).ServerTime> result.get(j).ServerTime){ 105 | process tem = result.get(i); 106 | result.set(i,result.get(j)); 107 | result.set(j, tem); 108 | } 109 | } 110 | } 111 | } 112 | public static void HpfSort(int start, int end, int temover){//根据优先级排序 113 | for(int i = start; i < end; i++){ 114 | for(int j = i + 1; j < end; j++){ 115 | int now = (temover - result.get(i).InTime + result.get(i).ServerTime) / result.get(i).ServerTime; 116 | int next = (temover - result.get(j).InTime + result.get(j).ServerTime) / result.get(j).ServerTime; 117 | if(next > now){//值越大优先 118 | process tem = result.get(i); 119 | result.set(i,result.get(j)); 120 | result.set(j, tem); 121 | } 122 | } 123 | } 124 | } 125 | public static int effective(int temover, int start){ //查看短进程有效值 126 | int end; //去返回有效值的下标 127 | for(end = start; end < result.size(); end++){ 128 | if(result.get(end).InTime > temover){ 129 | break; 130 | } 131 | } 132 | return end; 133 | } 134 | public static void AverNum(){ 135 | if(result == null){ 136 | throw new RuntimeException("请输入进程"); 137 | } 138 | int Turnsum=0; 139 | double AddTurnsum=0.0; 140 | for(int i=0;i queue1=new LinkedList<>(); 194 | int timeover = result.get(0).InTime; //记录上个进程结束时间 195 | int[] serviceTem = new int[result.size()]; //存放所有的进程估计运行时间的,开始全为0. 196 | int i = 1; //看队列进了几个进程了 197 | queue1.offer(0); //排完序,肯定先执行第一个. 198 | 199 | while(!queue1.isEmpty() || i < result.size()){ 200 | int cur = RR; 201 | if(queue1.isEmpty()) { 202 | for (int tep = 0; tep < result.size(); tep++) { 203 | if (serviceTem[tep] == 0) { 204 | queue1.offer(tep); 205 | i = i + 1; 206 | timeover=result.get(tep).InTime; 207 | break; 208 | } 209 | } 210 | } 211 | //出队,进行执行 212 | int tem = queue1.poll(); 213 | if(serviceTem[tem] == 0){ //当数组里估计运行时间为0的话,那就是第一次初始化,可以赋一下初始值. 214 | result.get(tem).StartTime = timeover; 215 | } 216 | while(cur != 0){ //模拟实现加时间片轮转,执行RR次,直到相等或用完。 217 | if(serviceTem[tem] != result.get(tem).ServerTime){ 218 | ++serviceTem[tem]; 219 | timeover++; 220 | } 221 | if(serviceTem[tem] == result.get(tem).ServerTime) { 222 | result.get(tem).FinishTime =timeover; 223 | result.get(tem).TurnaroundTime=result.get(tem).FinishTime-result.get(tem).InTime; 224 | result.get(tem).AddTurnaroundTime=(double)result.get(tem).TurnaroundTime/result.get(tem).ServerTime; 225 | Print(result.get(tem)); 226 | break; 227 | } 228 | cur--; 229 | } 230 | //i记录进程个数,去遍历所有进程,看还有那个没进入,如果进程到了,就插入队列. 231 | if(i < result.size()) { 232 | int j = i; 233 | for (; j < result.size(); j++) { 234 | if (result.get(j).InTime <= timeover) { 235 | queue1.offer(j); 236 | i = i + 1; 237 | } 238 | } 239 | } 240 | //如果当前进程没有执行完,就在进入队列. 241 | if(serviceTem[tem] != result.get(tem).ServerTime) { 242 | queue1.offer(tem); 243 | } 244 | } 245 | AverNum(); 246 | } 247 | public static int InitOther(int i,int temover){ 248 | if (temover >= result.get(i).InTime) { 249 | result.get(i).StartTime = temover; 250 | result.get(i).FinishTime = result.get(i).StartTime + result.get(i).ServerTime; 251 | temover = result.get(i).FinishTime; 252 | result.get(i).TurnaroundTime=result.get(i).FinishTime-result.get(i).InTime; 253 | result.get(i).AddTurnaroundTime=(double)result.get(i).TurnaroundTime/result.get(i).ServerTime; 254 | Print(result.get(i)); 255 | return temover; 256 | } else { 257 | result.get(i).StartTime = result.get(i).InTime; 258 | result.get(i).FinishTime = result.get(i).StartTime + result.get(i).ServerTime; 259 | temover = result.get(i).FinishTime; //记住他的结束时间. 260 | result.get(i).TurnaroundTime=result.get(i).FinishTime-result.get(i).InTime; 261 | result.get(i).AddTurnaroundTime=(double)result.get(i).TurnaroundTime/result.get(i).ServerTime; 262 | Print(result.get(i)); 263 | return temover; 264 | } 265 | 266 | } 267 | public static void main(String[] args) { 268 | Scanner scan=new Scanner(System.in); 269 | int choose=1; 270 | while(choose !=0){ 271 | Menu(); 272 | choose=scan.nextInt(); 273 | if(choose == 0){ 274 | return; 275 | } else if(choose==1){ 276 | Input(); 277 | } else if(choose == 2){ 278 | FCFS(); 279 | } else if(choose == 3){ 280 | SPF(); 281 | } else if(choose == 4){ 282 | HPF(); 283 | } else if(choose == 5){ 284 | RR(); 285 | } else{ 286 | System.out.println("输入错误"); 287 | continue; 288 | } 289 | 290 | } 291 | } 292 | } 293 | -------------------------------------------------------------------------------- /银行家算法/Bank.java: -------------------------------------------------------------------------------- 1 | package Experiment; 2 | import java.util.ArrayList; 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | class Process{ 7 | String name; //进程名字 8 | int allocation[]; //已分配的资源数 9 | int MaxNeed[]; //最大需求数量 10 | int needs[]; //仍然需要 11 | boolean finshined=false; //状态 12 | 13 | @Override 14 | public String toString() { 15 | return "Process{" + 16 | "name='" + name + '\'' + 17 | ", allocation=" + Arrays.toString(allocation) + 18 | //", MaxNeed=" + Arrays.toString(MaxNeed) + 可选项,最大需求矩阵不输出 19 | ", needs=" + Arrays.toString(needs) + 20 | ", finshined=" + finshined + 21 | '}';//重写tostring方法,用来输出进程信息 22 | } 23 | } 24 | public class Bank { 25 | 26 | private static int KINDS=0; //资源种类 27 | private static int[] resource; //总资源数 28 | private static int ProcessCount; //进程数量 29 | private static List team;//进程数组 30 | private static int[] avaliable; //当前可分配资源 31 | 32 | public static void InitAllResource(){ //初始化总资源数和进程数量 33 | System.out.println("请输入资源种类数量:"); 34 | Scanner sc=new Scanner(System.in); 35 | KINDS=sc.nextInt(); 36 | resource=new int[KINDS]; //资源总类数 37 | 38 | //各资源数量 39 | for(int i=0;i(); 54 | 55 | for(int i=0;i=team.size()){ 123 | index=0; //防止越界,循环查找 124 | } 125 | if(!team.get(index).finshined){//判断当前状态 126 | for(int i=0;iwork[i]){ 128 | choose=false; 129 | tmp++; 130 | index++; 131 | break; 132 | }else { 133 | choose=true; 134 | } 135 | } 136 | if(choose){ //找到能分配的,修改work数组,暂时修改状态值 137 | for (int j=0;j