main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @Title ProcessSimulator
  2. // @Description 模拟进程调用算法
  3. // @Author Shellmiao
  4. // @Update 2021.10.21
  5. package main
  6. import (
  7. "fmt"
  8. "sort"
  9. "strconv"
  10. )
  11. // PrintProcessQueue 用于最终输出结果
  12. func PrintProcessQueue(processQueue ProcessQueue) {
  13. processQueue.typeOfAl = 5
  14. sort.Sort(processQueue)
  15. fmt.Println("[-]进程ID\t进程名称\t到达时间\t服务时间\t开始时间\t结束时间\t周转时间\t带权周转时间")
  16. for i := 0; i < len(processQueue.ProcessQueue); i++ {
  17. fmt.Println("[-]" + strconv.Itoa(i) + "\t\t" + processQueue.ProcessQueue[i].name + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].arriveTime) + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].serveTime) + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].beginTime) + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].finishTime) + "\t\t" + strconv.Itoa(processQueue.ProcessQueue[i].roundTime) + "\t\t" + strconv.FormatFloat(processQueue.ProcessQueue[i].aveRoundTime, 'f', 2, 64))
  18. }
  19. }
  20. // CompleteProcess 完成一个进程
  21. func CompleteProcess(process *Process, time *int) {
  22. process.beginTime = *time
  23. *time += process.serveTime
  24. process.isFinished = true
  25. process.finishTime = *time
  26. process.roundTime = process.finishTime - process.arriveTime
  27. process.aveRoundTime = float64(process.roundTime) / float64(process.serveTime)
  28. }
  29. // WaitTillProcessArrive 在当前时间没有进程工作时,等待直到有新的进程工作加入
  30. func WaitTillProcessArrive(processQueue ProcessQueue, time int) int {
  31. for i := 0; i < len(processQueue.ProcessQueue); i++ {
  32. if !processQueue.ProcessQueue[i].isFinished {
  33. if processQueue.ProcessQueue[i].arriveTime > time {
  34. return processQueue.ProcessQueue[i].arriveTime
  35. } else {
  36. return time
  37. }
  38. }
  39. }
  40. return time
  41. }
  42. // FCFS FCFS算法
  43. func FCFS(processQueue ProcessQueue) {
  44. time := 0
  45. for i := 0; i < len(processQueue.ProcessQueue); i++ {
  46. time = WaitTillProcessArrive(processQueue, time)
  47. if !processQueue.ProcessQueue[i].isFinished {
  48. CompleteProcess(processQueue.ProcessQueue[i], &time)
  49. }
  50. }
  51. PrintProcessQueue(processQueue)
  52. }
  53. // SJF_and_HRRF_and_HPF 同时实现SJF,HRRF,HPF算法,通过flag区分
  54. func SJF_and_HRRF_and_HPF(processQueue ProcessQueue, flag int) {
  55. time := 0
  56. length := len(processQueue.ProcessQueue)
  57. for {
  58. var tempQueue ProcessQueue
  59. tempQueue.typeOfAl = flag
  60. tempQueue.time = time
  61. for i := 0; i < length; i++ {
  62. if processQueue.ProcessQueue[i].arriveTime > time {
  63. break
  64. }
  65. if !processQueue.ProcessQueue[i].isFinished {
  66. tempQueue.ProcessQueue = append(tempQueue.ProcessQueue, processQueue.ProcessQueue[i])
  67. }
  68. }
  69. if len(tempQueue.ProcessQueue) == 0 {
  70. temp := WaitTillProcessArrive(processQueue, time)
  71. if temp == time {
  72. break
  73. } else {
  74. time = temp
  75. tempQueue.time = time
  76. continue
  77. }
  78. }
  79. sort.Sort(tempQueue)
  80. CompleteProcess(tempQueue.ProcessQueue[0], &time)
  81. tempQueue.time = time
  82. }
  83. PrintProcessQueue(processQueue)
  84. }
  85. func main() {
  86. fmt.Println("[-]开始模拟调度算法...")
  87. var processQueue = initProcessQueue()
  88. processQueue.typeOfAl = 1
  89. sort.Sort(processQueue)
  90. fmt.Print("[-]请输入需要的调度算法(1[FCFS]、2[SJF]、3[HRRF]、4[HPF]): ")
  91. var choose int
  92. fmt.Scan(&choose)
  93. switch choose {
  94. case 1:
  95. FCFS(processQueue)
  96. case 2:
  97. SJF_and_HRRF_and_HPF(processQueue, 2)
  98. case 3:
  99. SJF_and_HRRF_and_HPF(processQueue, 3)
  100. case 4:
  101. for i := 0; i < len(processQueue.ProcessQueue); i++ {
  102. var temp int
  103. fmt.Print("[+]请输入第" + strconv.Itoa(i) + "个进程的信息(优先值): ")
  104. fmt.Scan(&temp)
  105. processQueue.ProcessQueue[i].priority = temp
  106. }
  107. SJF_and_HRRF_and_HPF(processQueue, 4)
  108. }
  109. }