时间片轮转调度 java_java OS时间片轮转调度基本算法

java OS时间片轮转调度基本算法实例源码讲解。

public class OSprocess{

private String name; //进程名

private OSprocess next;//指向下一个进程

private int run_time; //要求运行时间

private int allo_time;//已运行时间

private int req_time;//还需要运行时间

private char state;//状态

private int pri;//优先数

//构造方法2  时间片轮转方法

public OSprocess(String name, int run_time, char state, int allo_time) {

this.name = name;

this.run_time = run_time;

this.allo_time = allo_time;

this.state = state;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public OSprocess getNext() {

return next;

}

public void setNext(OSprocess next) {

this.next = next;

}

public int

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
时间片轮转调度算法是一种常见的进程调度算法,它是一种基于时间片的调度算法,可以保证每个进程都能够公平地使用CPU。在Java中,可以通过以下方式实现时间片轮转调度算法: ```java import java.util.LinkedList; import java.util.Queue; public class RoundRobin { public static void main(String[] args) { int[] process = {1, 2, 3, 4, 5}; int[] arrivalTime = {0, 1, 2, 3, 4}; int[] burstTime = {5, 3, 8, 6, 4}; int quantum = 3; roundRobin(process, arrivalTime, burstTime, quantum); } public static void roundRobin(int[] process, int[] arrivalTime, int[] burstTime, int quantum) { Queue<Integer> queue = new LinkedList<>(); int n = process.length; int[] remainingTime = new int[n]; int[] waitingTime = new int[n]; int[] turnaroundTime = new int[n]; int totalWaitingTime = 0; int totalTurnaroundTime = 0; for (int i = 0; i < n; i++) { remainingTime[i] = burstTime[i]; } int time = 0; queue.add(0); while (!queue.isEmpty()) { int processIndex = queue.poll(); if (remainingTime[processIndex] <= quantum) { time += remainingTime[processIndex]; remainingTime[processIndex] = 0; } else { time += quantum; remainingTime[processIndex] -= quantum; } while (processIndex < n && arrivalTime[processIndex] <= time) { queue.add(processIndex++); } if (remainingTime[processIndex] > 0) { queue.add(processIndex); } waitingTime[processIndex] = time - burstTime[processIndex] - arrivalTime[processIndex]; turnaroundTime[processIndex] = time - arrivalTime[processIndex]; } for (int i = 0; i < n; i++) { totalWaitingTime += waitingTime[i]; totalTurnaroundTime += turnaroundTime[i]; } System.out.println("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time"); for (int i = 0; i < n; i++) { System.out.println(process[i] + "\t\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnaroundTime[i]); } System.out.println("Average Waiting Time: " + (float) totalWaitingTime / n); System.out.println("Average Turnaround Time: " + (float) totalTurnaroundTime / n); } } ``` 上述代码中,我们使用了一个队列来存储进程,每次从队列中取出一个进程,并根据时间片的大小来执行进程。如果进程的剩余时间小于等于时间片,则执行完该进程;否则,将该进程重新加入队列中,等待下一次执行。同时,我们还计算了每个进程的等待时间和周转时间,并输出了平均等待时间和平均周转时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值