zoukankan      html  css  js  c++  java
  • 使用Java模拟操作系统高优先级算法

    最近做操作系统的课程设计,网上看到一些动态调度的算法都是基于C写的,下午闲来无事,用Java写了一个高优先级调度的算法玩玩,这个算法首先有这几条要注意
    • 优先级是否可以为负的,答案是肯定的,如果有一个线程阻塞了另外一个线程一直去运行就可能一直减去,让优先级变成负数了
    • 阻塞的时机:可以说阻塞的时机特别的重要,一旦到达时间需要线程进行阻塞的状态就要立刻将状态改变掉,让它产生调度
    • 恢复成就绪状态的问题:因为是有可能是线程进行I/O操作,让其阻塞了,所以说,一个时间片就需要让恢复阻塞的时间常数+1,当变为0的时候让他进入调度状态
    • 如果在系统中都是阻塞的线程怎么办?那没有办法,操作系统只能干等着让他们变为就绪状态的时候才能运行
    先上一个PCB结构体函数块
    1. public class PCB {
    2. //进程的ID
    3. private long id;
    4. //优先级顺序
    5. private int priority;
    6. // 运行的cpu时间
    7. private long cputime;
    8. // 需要运行的时间
    9. private long alltime;
    10. // 即将阻塞的时间
    11. private long startblock;
    12. // 从阻塞中回复的时间
    13. private long blocktime;
    14. // 现在线程的状态
    15. private String state;
    16. public PCB(long id, int priority, long cputime, long alltime,
    17. long startblock, long blocktime, String state) {
    18. super();
    19. this.id = id;
    20. this.priority = priority;
    21. this.cputime = cputime;
    22. this.alltime = alltime;
    23. this.startblock = startblock;
    24. this.blocktime = blocktime;
    25. this.state = state;
    26. }
    27. public String getState() {
    28. return state;
    29. }
    30. public void setState(String state) {
    31. this.state = state;
    32. }
    33. public long getBlocktime() {
    34. return blocktime;
    35. }
    36. public void setBlocktime(long blocktime) {
    37. this.blocktime = blocktime;
    38. }

    39. // 运行的三个状态
    40. public final static String STATE_READY = "READY";
    41. public final static String STATE_RUN = "RUN";
    42. public final static String STATE_BLOCK = "BLOCK";

    43. public long getId() {
    44. return id;
    45. }
    46. public void setId(long id) {
    47. this.id = id;
    48. }
    49. public int getPriority() {
    50. return priority;
    51. }
    52. public void setPriority(int priority) {
    53. this.priority = priority;
    54. }
    55. public long getCputime() {
    56. return cputime;
    57. }
    58. public void setCputime(long cputime) {
    59. this.cputime = cputime;
    60. }
    61. public long getAlltime() {
    62. return alltime;
    63. }
    64. public void setAlltime(long alltime) {
    65. this.alltime = alltime;
    66. }
    67. public long getStartblock() {
    68. return startblock;
    69. }
    70. public void setStartblock(long startblock) {
    71. this.startblock = startblock;
    72. }
    73. }
    好了,现在是主要的线程调度算法,注意优先级的调整:
    1. import java.util.ArrayList;
    2. import java.util.Arrays;
    3. import java.util.Comparator;
    4. import java.util.Iterator;
    5. import java.util.List;
    6. public class OrderImitate {
    7. // 按优先级次序排列的数组
    8. private ArrayList<PCB> priorityArrayList = null;
    9. /**
    10. * 优先级排序
    11. */
    12. public void sortArray(){
    13. PCB[] pcbs = (PCB[])priorityArrayList.toArray(new PCB[priorityArrayList.size()]);
    14. Arrays.sort(pcbs, new MyComprator());
    15. List<PCB> list = Arrays.asList(pcbs);
    16. this.priorityArrayList = new ArrayList<PCB>(list);
    17. }
    18. /**
    19. * 初始化,按照书中的数据进行初始化操作
    20. */
    21. private void init(){
    22. priorityArrayList = new ArrayList<PCB>();
    23. PCB zero = new PCB(0, 9, 0, 3, 2, 3, PCB.STATE_READY);
    24. priorityArrayList.add(zero);
    25. PCB one = new PCB(1, 38, 0, 3, -1, 0, PCB.STATE_READY);
    26. priorityArrayList.add(one);
    27. PCB two = new PCB(2, 30, 0, 6, -1, 0, PCB.STATE_READY);
    28. priorityArrayList.add(two);
    29. PCB three = new PCB(3, 29, 0, 3, -1, 0, PCB.STATE_READY);
    30. priorityArrayList.add(three);
    31. PCB four = new PCB(4, 0, 0, 4, -1, 0, PCB.STATE_READY);
    32. priorityArrayList.add(four);
    33. sortArray();
    34. }
    35. /**
    36. * 优先级执行算法
    37. * @return
    38. */
    39. public boolean hightpPriority(){
    40. // 获得第一个元素
    41. PCB first = priorityArrayList.get(0);
    42. if(!first.getState().equals(PCB.STATE_BLOCK)){
    43. // 运行一次优先级顺序-3
    44. first.setPriority(first.getPriority()-3);
    45. first.setCputime(first.getCputime()+1);
    46. first.setAlltime(first.getAlltime()-1);
    47. // 是否进入阻塞状态
    48. if(first.getStartblock() > 0){
    49. first.setStartblock(first.getStartblock() - 1);
    50. }
    51. // 当是阻塞状态的时候,将状态变为阻塞
    52. if(first.getStartblock() == 0){
    53. first.setState(PCB.STATE_BLOCK);
    54. }
    55. }
    56. // 所需要的时间片完成,结束运行
    57. if(first.getAlltime() == 0){
    58. System.out.println("----------------------------------------------");
    59. System.out.println("ID " + first.getId() + " have done !");
    60. System.out.println("----------------------------------------------");
    61. priorityArrayList.remove(0);
    62. }
    63. // 后面的任务优先级+1
    64. Iterator<PCB> iterator = priorityArrayList.iterator();
    65. int count = 0;
    66. while(iterator.hasNext()){
    67. if(count == 0){
    68. count++;
    69. iterator.next();
    70. continue;
    71. }
    72. PCB pcb = iterator.next();
    73. pcb.setPriority(pcb.getPriority()+1);
    74. // PCB的阻塞状态改变
    75. if(pcb.getBlocktime() != 0 && pcb.getState().equals(PCB.STATE_BLOCK)){
    76. pcb.setBlocktime(pcb.getBlocktime() -1);
    77. if(pcb.getBlocktime() == 0){
    78. pcb.setState(PCB.STATE_READY);
    79. }
    80. }
    81. }
    82. sortArray();
    83. return true;
    84. }
    85. /**
    86. * 执行程序
    87. */
    88. public void startRuning(){
    89. while(hightpPriority()){
    90. if(this.priorityArrayList.size() == 0){
    91. System.out.println("-----------------> ALL Works have done !");
    92. break;
    93. }
    94. // 输出数据
    95. Iterator<PCB> iterator = priorityArrayList.iterator();
    96. System.out.println("-----------------------------------------------------------------------------------------");
    97. System.out.println("ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE");
    98. while(iterator.hasNext()){
    99. PCB pcb = iterator.next();
    100. System.out
    101. .printf("%d %8d %12d %12d %13d %14d %18s ", pcb.getId(),
    102. pcb.getPriority(), pcb.getCputime(),
    103. pcb.getAlltime(), pcb.getStartblock(),
    104. pcb.getBlocktime(), pcb.getState());
    105. }
    106. System.out.println("-----------------------------------------------------------------------------------------");
    107. }
    108. }
    109. public static void main(String[] args) {
    110. OrderImitate orderImitate = new OrderImitate();
    111. orderImitate.init();
    112. orderImitate.startRuning();
    113. }
    114. // 优先级次序调整,优先级高且没有阻塞的进程在前面
    115. class MyComprator implements Comparator<PCB> {
    116. public int compare(PCB o1, PCB o2) {
    117. if((o1.getState().equals(PCB.STATE_READY) && o2.getState().equals(PCB.STATE_READY)) || (o1.getState().equals(PCB.STATE_BLOCK) && o2.getState().equals(PCB.STATE_BLOCK))){
    118. if(o1.getPriority() == o2.getPriority() ){
    119. return 0;
    120. }
    121. else if(o1.getPriority() > o2.getPriority()){
    122. return -1;
    123. }else {
    124. return 1;
    125. }
    126. }else if(o1.getState().equals(PCB.STATE_BLOCK) && o2.getState().equals(PCB.STATE_READY)){
    127. return 1;
    128. }else{
    129. return -1;
    130. }
    131. }
    132. }
    133. }
    一开始是想用优先队列的,但是碰上个问题,如果有多个线程同时进入阻塞状态,那么就可能第二个到后面那个都无法运行,这样没有完全排好序的优先队列就没有办法使用了,而且优先队列碰上一个大的问题,不知道什么,中途修改了队列中的值没有触发队列的排序,导致后面的一系列次序无法进行。


    好的,现在看看最后的结果:
    1. -----------------------------------------------------------------------------------------
    2. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    3. 1 35 1 2 -1 0 READY
    4. 2 31 0 6 -1 0 READY
    5. 3 30 0 3 -1 0 READY
    6. 0 10 0 3 2 3 READY
    7. 4 1 0 4 -1 0 READY
    8. -----------------------------------------------------------------------------------------
    9. -----------------------------------------------------------------------------------------
    10. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    11. 1 32 2 1 -1 0 READY
    12. 2 32 0 6 -1 0 READY
    13. 3 31 0 3 -1 0 READY
    14. 0 11 0 3 2 3 READY
    15. 4 2 0 4 -1 0 READY
    16. -----------------------------------------------------------------------------------------
    17. ----------------------------------------------
    18. ID 1 have done !
    19. ----------------------------------------------
    20. -----------------------------------------------------------------------------------------
    21. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    22. 2 32 0 6 -1 0 READY
    23. 3 32 0 3 -1 0 READY
    24. 0 12 0 3 2 3 READY
    25. 4 3 0 4 -1 0 READY
    26. -----------------------------------------------------------------------------------------
    27. -----------------------------------------------------------------------------------------
    28. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    29. 3 33 0 3 -1 0 READY
    30. 2 29 1 5 -1 0 READY
    31. 0 13 0 3 2 3 READY
    32. 4 4 0 4 -1 0 READY
    33. -----------------------------------------------------------------------------------------
    34. -----------------------------------------------------------------------------------------
    35. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    36. 3 30 1 2 -1 0 READY
    37. 2 30 1 5 -1 0 READY
    38. 0 14 0 3 2 3 READY
    39. 4 5 0 4 -1 0 READY
    40. -----------------------------------------------------------------------------------------
    41. -----------------------------------------------------------------------------------------
    42. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    43. 2 31 1 5 -1 0 READY
    44. 3 27 2 1 -1 0 READY
    45. 0 15 0 3 2 3 READY
    46. 4 6 0 4 -1 0 READY
    47. -----------------------------------------------------------------------------------------
    48. -----------------------------------------------------------------------------------------
    49. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    50. 2 28 2 4 -1 0 READY
    51. 3 28 2 1 -1 0 READY
    52. 0 16 0 3 2 3 READY
    53. 4 7 0 4 -1 0 READY
    54. -----------------------------------------------------------------------------------------
    55. -----------------------------------------------------------------------------------------
    56. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    57. 3 29 2 1 -1 0 READY
    58. 2 25 3 3 -1 0 READY
    59. 0 17 0 3 2 3 READY
    60. 4 8 0 4 -1 0 READY
    61. -----------------------------------------------------------------------------------------
    62. ----------------------------------------------
    63. ID 3 have done !
    64. ----------------------------------------------
    65. -----------------------------------------------------------------------------------------
    66. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    67. 2 25 3 3 -1 0 READY
    68. 0 18 0 3 2 3 READY
    69. 4 9 0 4 -1 0 READY
    70. -----------------------------------------------------------------------------------------
    71. -----------------------------------------------------------------------------------------
    72. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    73. 2 22 4 2 -1 0 READY
    74. 0 19 0 3 2 3 READY
    75. 4 10 0 4 -1 0 READY
    76. -----------------------------------------------------------------------------------------
    77. -----------------------------------------------------------------------------------------
    78. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    79. 0 20 0 3 2 3 READY
    80. 2 19 5 1 -1 0 READY
    81. 4 11 0 4 -1 0 READY
    82. -----------------------------------------------------------------------------------------
    83. -----------------------------------------------------------------------------------------
    84. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    85. 2 20 5 1 -1 0 READY
    86. 0 17 1 2 1 3 READY
    87. 4 12 0 4 -1 0 READY
    88. -----------------------------------------------------------------------------------------
    89. ----------------------------------------------
    90. ID 2 have done !
    91. ----------------------------------------------
    92. -----------------------------------------------------------------------------------------
    93. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    94. 0 17 1 2 1 3 READY
    95. 4 13 0 4 -1 0 READY
    96. -----------------------------------------------------------------------------------------
    97. -----------------------------------------------------------------------------------------
    98. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    99. 4 14 0 4 -1 0 READY
    100. 0 14 2 1 0 3 BLOCK
    101. -----------------------------------------------------------------------------------------
    102. 可以看到这里因为线程阻塞产生了调度:
    103. -----------------------------------------------------------------------------------------
    104. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    105. 4 11 1 3 -1 0 READY
    106. 0 15 2 1 0 2 BLOCK
    107. -----------------------------------------------------------------------------------------
    108. -----------------------------------------------------------------------------------------
    109. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    110. 4 8 2 2 -1 0 READY
    111. 0 16 2 1 0 1 BLOCK
    112. -----------------------------------------------------------------------------------------
    113. -----------------------------------------------------------------------------------------
    114. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    115. 0 17 2 1 0 0 READY
    116. 4 5 3 1 -1 0 READY
    117. -----------------------------------------------------------------------------------------
    118. ----------------------------------------------
    119. ID 0 have done !
    120. ----------------------------------------------
    121. -----------------------------------------------------------------------------------------
    122. ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
    123. 4 5 3 1 -1 0 READY
    124. -----------------------------------------------------------------------------------------
    125. ----------------------------------------------
    126. ID 4 have done !
    127. ----------------------------------------------
    128. -----------------> ALL Works have done !





  • 相关阅读:
    pat00-自测5. Shuffling Machine (20)
    Spiral Matrix
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Best Time to Buy and Sell Stock II
    4Sum
    3Sum Closest
    3Sum
    MySQL存储过程、函数和游标
    Word Ladder
  • 原文地址:https://www.cnblogs.com/sober-reflection/p/4192315.html
Copyright © 2011-2022 走看看