操作系统实验——程序调度
操作系统实验要求写程序调度,百度查了一下,借鉴了一位老哥的先来先服务,自己写了后面的
先来先服务
只要写一个循环,把process集合中的每个process取出,把process结束时间置为当前时间加上服务时间,
然后当前时间加上服务时间即可。
private void calc() {
int timeNow = 0;
Process opProcess;
for (int i = 0; i < processNumber; i++) {
opProcess = processList.get(i);
int waitTime = timeNow - opProcess.getArrivalTime();
int completionTime = timeNow + opProcess.getServicesTime();
int turnAroundTime = completionTime
- opProcess.getArrivalTime();
double turnAroundTimeWithWeight = (double) turnAroundTime
/ opProcess.getServicesTime();
opProcess.setStartTime(timeNow);
opProcess.setWaitTime(waitTime);
opProcess.setCompletionTime(completionTime);
opProcess.setTurnAroundTime(turnAroundTime);
opProcess.setTurnAroundTimeWithWeight(
turnAroundTimeWithWeight);
timeNow += opProcess.getServicesTime();
}
}
这是那老哥博客
[link]https://www.cnblogs.com/daheww/p/10154455.html
时间片轮转法
运用一个队列,存储所有等待队列,每次循环先把后来的加入到队尾,再把运行完的加入到队尾,只要进程数大于零且队列不为空,取出队首元素。当前进程剩余运行时间等于总需要服务时间-时间片。然后判断剩余运行时间是否小于等于0.小于等于则结束时间等于当前时间加上进循环之前的剩余服务时间。进程数-1。
private void calc() {
int timeNow = 0;
Queue queue=new LinkedList();
int processRemain = processNumber;
boolean noProcessRunInThisTurn=true;
Process opProcess;Process linshi=null;
int extime=0;
while (processRemain != 0) {
if(arrtime.contains(timeNow)){
queue.add(processList.get(arrtime.indexOf(timeNow)));
}
if(timeNow==extime&&linshi!=null){
noProcessRunInThisTurn=true;
if(timeNow!=0&&linshi.getRemainServiceTime()>0)
queue.add(linshi);
}
if(noProcessRunInThisTurn&&!queue.isEmpty()) {
opProcess = (Process) queue.