@@ -5,7 +5,7 @@ import utils.ProgramData
55import utils.Task
66import java.util.*
77
8- internal fun calculateTasks (tasks : List <Task >, compare : (Task , Task ) -> Int , isNonPreemptive : Boolean ): ProgramData {
8+ internal fun calculateTasks (tasks : List <Task >, compare : (Task , Task ) -> Int , isPreemptive : Boolean ): ProgramData {
99 val info: MutableList <ChartInfo > = mutableListOf (ChartInfo (- 1 , tasks[0 ].arrivalTime, ranTime = 0 ))
1010
1111 var currentRunTime = tasks[0 ].arrivalTime
@@ -16,12 +16,18 @@ internal fun calculateTasks(tasks: List<Task>, compare: (Task, Task) -> Int, isN
1616 while (idx < tasks.size) {
1717 while (idx < tasks.size && currentRunTime == tasks[idx].arrivalTime) { // 동일한 시간에 들어오는 경우 처리를 위해 if 가 아닌 while
1818
19- if (currentRunTask == = null ) { currentRunTask = tasks[idx] }
20- else if (compare(currentRunTask, tasks[idx]) <= 0 || isNonPreemptive) { // 기존 Task 의 우선순위가 더 크면, 변화 X, current 계속 실행
19+ if (currentRunTask == = null ) {
20+ currentRunTask = tasks[idx]
21+ } else if (compare(currentRunTask, tasks[idx]) <= 0 || ! isPreemptive) {
22+ // If It's not preemptive, simply add it to the ready pool without comparing it to other tasks.
23+ // Or if It's preemptive, then compare the priority of 2 tasks.
24+ // 기존 Task 의 우선순위가 더 크면, 변화 X, current 계속 실행
2125 readyPool.add(tasks[idx])
2226 } else { // If the priority of the new task is more urgent. 새로 비교할 Task 의 우선순위가 더 크면
2327 val ranTime = currentRunTime - info.last().timestamp
24- if (ranTime != 0 ) info + = ChartInfo (currentRunTask.pid, currentRunTime, ranTime) // To ignore if ranTime is zero
28+ if (ranTime != 0 ) {
29+ info + = ChartInfo (currentRunTask.pid, currentRunTime, ranTime)
30+ } // To ignore if ranTime is zero
2531
2632 tasks[idx].responseTime = currentRunTime // 새로 들어온 Task 의 첫 응답 시간 설정
2733 readyPool.add(currentRunTask)
@@ -56,7 +62,9 @@ internal fun calculateTasks(tasks: List<Task>, compare: (Task, Task) -> Int, isN
5662 val newRun = readyPool.remove()
5763
5864 info + = ChartInfo (newRun.pid, currentRunTime + newRun.remainedTime, newRun.remainedTime)
59- if (newRun.responseTime == - 1 ) { newRun.responseTime = currentRunTime }
65+ if (newRun.responseTime == - 1 ) {
66+ newRun.responseTime = currentRunTime
67+ }
6068 currentRunTime + = newRun.remainedTime
6169 newRun.remainedTime = 0
6270 newRun.waitedTime = currentRunTime - newRun.arrivalTime - newRun.executionTime
0 commit comments