devbox
我家小花儿
这个屌丝很懒,什么也没留下!
热门文章
  • 1Elon Musk学习方法研究_elon musk and stanford
  • 2Spring Cloud 学习笔记:Eureka 服务注册与发现_

        public String pcbName;//进程名

        public PCB next;//指针

        public int time;//运行时间

        public int priority;//优先数

        public char statue = 'R';//进程的状态,初始化为就绪态

        //构造方法

        public PCB(String pcbName, int time, int priority) {

            this.pcbName = pcbName;

            this.time = time;

            this.priority = priority;

        }}

    3)流程

    4源程序代码

    1. package hh;
    2. // 定义进程控制块PCB
    3. public class PCB {
    4. public String pcbName;//进程名
    5. public PCB next;//指针
    6. public int time;//运行时间
    7. public int priority;//优先数
    8. public char statue = 'R';//进程的状态,初始化为就绪态
    9. //构造方法
    10. public PCB(String pcbName, int time, int priority) {
    11. this.pcbName = pcbName;
    12. this.time = time;
    13. this.priority = priority;
    14. }
    15. //重写toString方法
    16. public String toString() {
    17. return "进程名='" + pcbName + '\'' +
    18. ", 要求运行时间=" + time +
    19. ", 优先级数=" + priority +
    20. ", 状态=" + statue;
    21. }
    22. }
    23. /*按优先数调度算法模拟实现处理器调度
    24. */
    25. class ProcessScheduling {
    26. private PCB head;
    27. /**
    28. * PCB对象插入操作,根据每一个进程的优先级数进行插入
    29. * 如果优先级数相等,则按先来先服务原则进行插入
    30. */
    31. public void addPCB(PCB node) {
    32. //若要求运行时间=0,则把它的状态修改成“结束”(E),且退出队列
    33. if(node.time == 0) {
    34. node.statue = 'E';
    35. System.out.println(node.pcbName + "退出进程队列");
    36. System.out.println(node + "\n");
    37. return;
    38. }
    39. //若头结点为空
    40. if(this.head == null) {
    41. this.head = node;
    42. return;
    43. }
    44. //边插入边按优先级进行排序
    45. //如果node的优先级比头结点大
    46. if(node.priority > this.head.priority) {
    47. node.next = this.head;
    48. this.head = node;
    49. return;
    50. }
    51. //如果node的优先级与头结点相等,按先来先服务原则
    52. if(node.priority == this.head.priority) {
    53. node.next = this.head.next;
    54. this.head.next = node;
    55. return;
    56. }
    57. //如果node的优先级不比头结点大,也不与头结点相等
    58. PCB cur = this.head;
    59. while(cur.next != null) {
    60. if(cur.next.priority < node.priority) {
    61. PCB curNext = cur.next;
    62. node.next = curNext;
    63. cur.next = node;
    64. return;
    65. }
    66. //如果node的优先级与头结点相等,按先来先服务原则
    67. if(cur.next.priority == node.priority) {
    68. PCB curNext = cur.next;
    69. node.next = curNext.next;
    70. curNext.next = node;
    71. return;
    72. }
    73. cur = cur.next;
    74. }
    75. //此时如果还未结束,则node应该直接插入到队尾
    76. cur.next = node;
    77. node.next = null;
    78. }
    79. /**
    80. * 优先数调度算法模拟实现处理器调度的运行
    81. * 每一次运行优先级数最高的一个进程,运行结束后再将其插入到队列中
    82. */
    83. public void runPCB() {
    84. while(this.head != null) {
    85. //运行优先级最高的第一个进程
    86. PCB cur = this.head;
    87. this.head = this.head.next;
    88. System.out.println();
    89. System.out.println("开始执行" + cur.pcbName + "进程");
    90. System.out.println(cur);
    91. //cur的运行优先级数-1
    92. cur.priority -= 1;
    93. //cur的运行时间-1
    94. cur.time -= 1;
    95. System.out.println(cur.pcbName + "进程执行完毕");
    96. System.out.println();
    97. //再将cur插入进程队列
    98. addPCB(cur);
    99. //打印每次被选中进程的进程运行一次后进程队列的变化。
    100. if(this.head == null) {
    101. System.out.println("所有的线程执行完毕");
    102. return;
    103. }
    104. System.out.println("=====================");
    105. System.out.println("此时进程队列的所有进程信息");
    106. display();
    107. System.out.println("=====================");
    108. }
    109. }
    110. //打印进程队列
    111. public void display() {
    112. for(PCB pcb = this.head; pcb != null; pcb = pcb.next) {
    113. System.out.println(pcb);
    114. }
    115. }
    116. }
    117. package hh;
    118. public class TestPCB {
    119. public static void main(String[] args) {
    120. PCB[] pcbs = new PCB[5];
    121. pcbs[0] = new PCB("P1",2,1);
    122. pcbs[1] = new PCB("P2",3,5);
    123. pcbs[2] = new PCB("P3",1,3);
    124. pcbs[3] = new PCB("P4",2,4);
    125. pcbs[4] = new PCB("P5",4,2);
    126. ProcessScheduling p = new ProcessScheduling();
    127. for(PCB pcb : pcbs) {
    128. p.addPCB(pcb);
    129. }
    130. p.runPCB();
    131. }
    132. }

    5)实验结果

    (二)可变分区管理方式下采用最先适应算法实现主存的分配与回收

    可变分区方式是按作业需要的主存空间大小来分割分区的。当要装人一个作业时;根据作业需要 的主存量查看是否有足够的空闲空间,若有,则按需要量分割一个分区分配给该作业;若无,则作业不能 装入。随着作业的装入、撤离、主存空间被分成许多个分区。有的分区被作业占用,而有的分区是空闲的。

    如图(A)所示.为了说明哪些区是空闲的, 可以用来装入新作业,必须要有一张空闲区说明表,格式如图(B)所示:

    起址:指出一个空闲区的主存起始地址。

    长度:指出从起始地址开始的一个连续空闲区的长度。

    状态:有两种状态。一种是“未分配”状态,指出对应的由起始地址接线员出的某个长度的区域是空闲区;另一种是“空表目”状态,表示表中对应的登记项目是空白(无效),可用来登记新的空闲区(例如,作业撤消后,它所占用的区域就成了空闲区位。应找一个“空表目”栏登记归还区的起址和长度且修改状态)。由于分区的个数不定,所以空闲区说明表中应有适量的状态为“空表目”的登记栏目,否则造成表格溢出无法登记。

    1)功能说明

    ①当有一个新作业要求装入主存时,必须查空闲区说明表,从中找出一个足够大的空闲区。有时找 到的空闲区可能大于作业需要量。这时应把原来的空闲区分成两个部分:一部分分配给作业占用,另一部 分又成为一个较小的空闲区。为了尽量减少由于分割造成的“碎片”,采用二个措施:一是在作业装入时, 尽可能地利用主存的低地址部分的空闲区,而尽量保存高地址部分有较大的连续空间区域,以有利于大型作业的装入

    ②采用最先适应法分配主存空间

    按照作业的需要量,查空闲区说明表,找到第一个满足要求的空闲区.当空闲区大于作业量与选定常量之和时,一部分用来装入作业,另一部分仍为空闲区登记在空闲区说明表中。由于本实验是模拟主存的分配,所以当把主存区分配给作业后,并不实际启动装入作业,而用输出“分配情况”来代替。

    ③当一个作业执行结束撤离时,作业所占的区域归还,归还的区域如果与其它空闲区相邻,则应合成一个较大的空闲登记在空闲区说明表中。如果一个作业撤离,归还所占主存区域时,应与上、下相邻的空闲区一起合并成一个大的空闲区后登记在空闲区说明表中。

    2)相关类的定义(结构)

    在编制程序时,可用一个链表结构分别放作业表和空闲区说明表。

    class Node{

        int start;  //起始

        int size;   //大小

        boolean state;   //状态

        int end;    //结束地址

        public Node(){

        }

        public Node(int size) {    //作业申请节点

            this.state=true;

            this.size = size;

        }

        public Node(int start, int size, boolean state) {   //空闲节点

            this.start = start;

            this.size = size;

            this.state = state;

            this.end=start+size;

        }

    3)流程

    4)源程序代码(附注解)

    1. package abc;
    2. import java.io.IOException;
    3. import java.util.Scanner;
    4. //1、初始化用户分区-》显示分区
    5. //2、选择功能
    6. //有以下功能:
    7. //1)存储作业
    8. //2)执行完显示分区
    9. //3)回收作业
    10. //4)执行完显示分区
    11. public class MemoryManagement {
    12. public static void main(String args[]) {
    13. MainClass c = new MainClass();
    14. c.execInstruction();
    15. }
    16. }
    17. class MainClass{
    18. static int SystemMemory = 500;//系统初始容量
    19. //单个作业信息类
    20. class Job{
    21. String name;
    22. int begin;
    23. int lenth;
    24. Job lastJob;
    25. Job nextJob;
    26. }
    27. //空闲分区信息类
    28. class freeSpace{
    29. freeSpace lastSpace;//指向上一个空间
    30. freeSpace nextSpace;//指向下一个空间
    31. int begin;
    32. int lenth;
    33. }
    34. //作业链表类
    35. class JobLine{
    36. Job firstJob;
    37. boolean isFull = true;
    38. }
    39. //空闲区域链表类
    40. class freeSpaceLine{
    41. freeSpace firstSpace;
    42. boolean isFull = true;
    43. }
    44. //定义一个选择菜单
    45. public int getMenu() {
    46. System.out.println("请你选择需要进行的操作:");
    47. System.out.println("1:装入作业");
    48. System.out.println("2:回收作业");
    49. System.out.println("3:显示系统当前分区情况");
    50. System.out.println("4:退出程序");
    51. Scanner scan = new Scanner(System.in);
    52. int select = scan.nextInt();
    53. if(1 == select) {
    54. System.out.println("请你选择装入作业时要使用的算法:");
    55. System.out.println("1:最先适应分配算法");
    56. Scanner scan1 = new Scanner(System.in);
    57. int select1 = scan1.nextInt();
    58. if(1 == select1)
    59. return 11;
    60. else{
    61. System.out.println("你想干嘛");
    62. return -1;
    63. }
    64. }
    65. else if(2 == select) {
    66. return 2;
    67. }
    68. else if(3 == select) {
    69. return 3;
    70. }
    71. else if(4 == select) {
    72. return 0;
    73. }
    74. return -1;
    75. }
    76. //执行用户输入的命令
    77. public void execInstruction() {
    78. int select = getMenu();
    79. initialFreeArea();
    80. while(0 != select) {
    81. if(11 == select)
    82. storeHomework(select);
    83. else if(2 == select) {
    84. System.out.println("请输入要回收的作业的名称");
    85. Scanner in = new Scanner(System.in);
    86. String temp = in.next();
    87. recycleJob(temp);
    88. printSituation();
    89. }
    90. else if(3 == select){
    91. printSituation();
    92. try {
    93. Thread.sleep(3000);
    94. e.printStackTrace();
    95. }
    96. }
    97. select = getMenu();
    98. }
    99. }
    100. freeSpaceLine freeLineOne;
    101. JobLine jobLine;
    102. //初始化用户区
    103. } catch (InterruptedException e) {
    104. // TODO Auto-generated catch block
    105. public void initialFreeArea() {
    106. freeLineOne = new freeSpaceLine();
    107. freeSpace first = new freeSpace();
    108. freeLineOne.firstSpace = first;
    109. freeLineOne.isFull = false;
    110. first.begin = 0;
    111. first.lenth = SystemMemory;
    112. first.lastSpace = null;
    113. first.nextSpace = null;
    114. }
    115. public void printSituation() {
    116. System.out.println("********************************");
    117. System.out.println(" 已分配分区表\n");
    118. System.out.println("始址\t长度\t作业号\t");
    119. System.out.println("--------------------------------");
    120. Job temp = jobLine.firstJob;
    121. while(temp != null) {
    122. System.out.println(temp.begin + "\t" + temp.lenth + "\t" + temp.name + "\t");
    123. temp = temp.nextJob;
    124. }
    125. System.out.println("--------------------------------");
    126. System.out.println(" 空闲分区表\n");
    127. System.out.println("始址\t长度\t标志位\t");
    128. System.out.println("--------------------------------");
    129. freeSpace temp2 = freeLineOne.firstSpace;
    130. while(temp2 != null) {
    131. System.out.println(temp2.begin + "\t" + temp2.lenth + "\t" + "未分配\t");
    132. temp2 = temp2.nextSpace;
    133. }
    134. System.out.println("--------------------------------");
    135. System.out.println("********************************");
    136. System.out.println("\n\n\n\n");
    137. }
    138. //输入作业
    139. //1、创建一个temp临时变量(job类)
    140. //2、将信息输入经temp类
    141. //3、在空闲用户区找合适的块
    142. //4、如果找得到则将temp存进作业链表
    143. //5、重置空闲区
    144. //6、打印情况
    145. public Job inputJob() {
    146. Job tempJob = new Job();
    147. Scanner scan1 = new Scanner(System.in);
    148. System.out.println("请输入作业的名称");
    149. tempJob.name = scan1.next();
    150. System.out.println("请输入作业的长度");
    151. tempJob.lenth = scan1.nextInt();
    152. tempJob.nextJob = null;
    153. tempJob.lastJob = null;
    154. return tempJob;
    155. }
    156. //判断内存是否足够存入作业
    157. public boolean lookForEnoughMemory(Job tempJob) {
    158. if(freeLineOne != null) {
    159. if(freeLineOne.isFull != true) {
    160. freeSpace temp = freeLineOne.firstSpace;
    161. while(temp != null) {
    162. if(temp.lenth >= tempJob.lenth)
    163. return true;
    164. temp = temp.nextSpace;
    165. }
    166. }
    167. }
    168. return false;
    169. }
    170. //如果内存足够则存入作业
    171. public void storeHomework(int selectArithmetic) {
    172. Job tempJob = inputJob();
    173. if(lookForEnoughMemory(tempJob) == true&&11 == selectArithmetic) {
    174. {
    175. freeSpace space = firstFit(tempJob);
    176. apllicationSpace(tempJob,space);
    177. insertJob(tempJob);
    178. }
    179. printSituation();
    180. }
    181. else
    182. System.out.println("系统内存不足,请稍后再试");
    183. }
    184. public void apllicationSpace(Job tempJob,freeSpace space) {
    185. int jobLenth = tempJob.lenth;
    186. int spaceBegin = space.begin;
    187. int spaceLenth = space.lenth;
    188. tempJob.begin = spaceBegin;
    189. space.begin = spaceBegin + jobLenth;
    190. space.lenth = spaceLenth - jobLenth;
    191. if(0 == space.lenth) {
    192. if(space.lastSpace != null && space.nextSpace != null)
    193. space.lastSpace.nextSpace = space.nextSpace;
    194. if(space.nextSpace != null &&space.lastSpace != null)
    195. space.nextSpace.lastSpace = space.lastSpace;
    196. }
    197. }
    198. public void insertJob(Job tempJob) {
    199. if(jobLine == null) {
    200. jobLine = new JobLine();
    201. jobLine.firstJob = tempJob;
    202. return;
    203. }
    204. else if(jobLine.firstJob == null) {
    205. jobLine.firstJob = tempJob;
    206. return;
    207. }
    208. Job temp = jobLine.firstJob;
    209. while(temp.nextJob != null) {
    210. temp = temp.nextJob;
    211. }
    212. temp.nextJob = tempJob;
    213. tempJob.lastJob = temp;
    214. }
    215. //最先适应分配算法
    216. public freeSpace firstFit(Job tempJob) {
    217. freeSpace temp = freeLineOne.firstSpace;
    218. while(temp != null) {
    219. if(temp.lenth >= tempJob.lenth){
    220. return temp;
    221. }
    222. temp = temp.nextSpace;
    223. }
    224. return null;
    225. }
    226. public void recycleJob(String name) {
    227. //1、找到与输入相同的Job
    228. Job address = jobLine.firstJob;
    229. Job sameJob = null;
    230. while(address != null) {
    231. if(address.name.equalsIgnoreCase(name)) {
    232. sameJob = address;
    233. break;
    234. }
    235. address = address.nextJob;
    236. }
    237. //按顺序插入空闲区域队列
    238. if(sameJob != null) {
    239. //改变空闲区
    240. freeSpace tempFree1 = freeLineOne.firstSpace;
    241. freeSpace tempFree2 = new freeSpace();
    242. tempFree2.begin = sameJob.begin;
    243. tempFree2.lenth = sameJob.lenth;
    244. while(tempFree1 != null) {
    245. if( tempFree1.begin < sameJob.begin ) {
    246. if(tempFree1.nextSpace == null) {
    247. tempFree1.nextSpace = tempFree2;
    248. tempFree2.lastSpace = tempFree1;
    249. tempFree2.nextSpace = null;
    250. defragmentation();
    251. break;
    252. }
    253. else if(tempFree1.nextSpace != null && tempFree1.nextSpace.begin > sameJob.begin) {
    254. freeSpace temp3 = tempFree1.nextSpace;
    255. tempFree1.nextSpace = tempFree2;
    256. tempFree2.nextSpace = temp3;
    257. tempFree2.lastSpace = tempFree1;
    258. temp3.lastSpace = tempFree2;
    259. defragmentation();
    260. break;
    261. }
    262. }
    263. else if(tempFree1.begin > sameJob.begin){
    264. tempFree2.lastSpace = null;
    265. tempFree2.nextSpace = tempFree1;
    266. tempFree1.lastSpace = tempFree2;
    267. freeLineOne.firstSpace = tempFree2;
    268. defragmentation();
    269. break;
    270. }
    271. tempFree1 = tempFree1.nextSpace;
    272. }
    273. //改变作业区
    274. if(sameJob.lastJob == null) {
    275. if(sameJob.nextJob != null) {
    276. jobLine.firstJob = sameJob.nextJob;
    277. sameJob.nextJob.lastJob = null;
    278. }
    279. else {
    280. jobLine.firstJob = null;
    281. }
    282. }
    283. else if(sameJob.nextJob == null) {
    284. sameJob.lastJob.nextJob = null;
    285. }
    286. else if(sameJob.nextJob != null) {
    287. Job temp1 = sameJob.lastJob;
    288. Job temp2 = sameJob.nextJob;
    289. temp1.nextJob = temp2;
    290. temp2.lastJob = temp1;
    291. }
    292. }
    293. }
    294. public void defragmentation() {
    295. //回收的作业在空闲区的前方
    296. freeSpace tempSpace1 = freeLineOne.firstSpace;
    297. freeSpace tempSpace2;
    298. boolean conbine = false;
    299. while(tempSpace1.nextSpace != null) {
    300. if(tempSpace1.begin + tempSpace1.lenth == tempSpace1.nextSpace.begin) {
    301. tempSpace1.lenth += tempSpace1.nextSpace.lenth;
    302. tempSpace2 = tempSpace1.nextSpace.nextSpace;
    303. tempSpace1.nextSpace = tempSpace2;
    304. if(tempSpace2 != null) {
    305. tempSpace2.lastSpace = tempSpace1;
    306. }
    307. conbine = true;
    308. }
    309. tempSpace1 = tempSpace1.nextSpace;
    310. if(conbine == true) {
    311. tempSpace1 = freeLineOne.firstSpace;
    312. conbine = false;
    313. }
    314. }
    315. }
    316. }

    5)实验结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/460156
推荐阅读
相关标签
  

闽ICP备14008679号