进程管理操作系统实验java
时间: 2023-11-29 08:46:19 浏览: 135
进程管理是操作系统中的一个重要组成部分,Java作为一种面向对象的编程语言,也可以用于实现进程管理。下面是一个简单的Java进程管理实验示例:
```java
package os.test1;
import java.util.*;
public class Algorithm {
private PCB min=null; //满足运行条件的进程
private int num=0; //一批进程周转时间和
private int start=0; //更新系统开始时间
private double nums=0; //一批带权周转时间
private int count=0; //统计一批进程的进程个数
private static Create create=new Create(); //实例化,以方便创建和增加进程
// 进程调度算法
public void algorithm() {
ArrayList<PCB> list=create.getList();
while(list.size()>0) {
for(int i=0;i<list.size();i++) {
PCB pcb=list.get(i);
if(pcb.getArriveTime()<=start) {
if(min==null) {
min=pcb;
}else {
if(min.getPriority()>pcb.getPriority()) {
min=pcb;
}
}
}
}
if(min!=null) {
min.setStartTime(start);
start+=min.getNeedTime();
min.setFinishTime(start);
min.setTurnaroundTime(min.getFinishTime()-min.getArriveTime());
min.setWeightTurnaroundTime(min.getTurnaroundTime()/min.getNeedTime());
num+=min.getTurnaroundTime();
nums+=min.getWeightTurnaroundTime();
count++;
list.remove(min);
min=null;
}else {
start++;
}
}
}
// 输出结果
public void printResult() {
System.out.println("进程名\t到达时间\t需要时间\t优先级\t开始时间\t完成时间\t周转时间\t带权周转时间");
ArrayList<PCB> list=create.getList();
for(int i=0;i<list.size();i++) {
PCB pcb=list.get(i);
System.out.println(pcb.getName()+"\t"+pcb.getArriveTime()+"\t"+pcb.getNeedTime()+"\t"+pcb.getPriority()+"\t"+pcb.getStartTime()+"\t"+pcb.getFinishTime()+"\t"+pcb.getTurnaroundTime()+"\t"+pcb.getWeightTurnaroundTime());
}
System.out.println("平均周转时间:"+num/count);
System.out.println("平均带权周转时间:"+nums/count);
}
public static void main(String[] args) {
Algorithm algorithm=new Algorithm();
algorithm.algorithm();
algorithm.printResult();
}
}
class Create {
private ArrayList<PCB> list=new ArrayList<PCB>();
public Create() {
PCB pcb1=new PCB("P1",0,5,3);
PCB pcb2=new PCB("P2",1,3,2);
PCB pcb3=new PCB("P3",2,4,1);
PCB pcb4=new PCB("P4",3,2,4);
PCB pcb5=new PCB("P5",4,4,5);
list.add(pcb1);
list.add(pcb2);
list.add(pcb3);
list.add(pcb4);
list.add(pcb5);
}
public ArrayList<PCB> getList() {
return list;
}
}
class PCB {
private String name; //进程名
private int arriveTime; //到达时间
private int needTime; //需要时间
private int priority; //优先级
private int startTime; //开始时间
private int finishTime; //完成时间
private int turnaroundTime; //周转时间
private double weightTurnaroundTime; //带权周转时间
public PCB(String name,int arriveTime,int needTime,int priority) {
this.name=name;
this.arriveTime=arriveTime;
this.needTime=needTime;
this.priority=priority;
}
public String getName() {
return name;
}
public int getArriveTime() {
return arriveTime;
}
public int getNeedTime() {
return needTime;
}
public int getPriority() {
return priority;
}
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime=startTime;
}
public int getFinishTime() {
return finishTime;
}
public void setFinishTime(int finishTime) {
this.finishTime=finishTime;
}
public int getTurnaroundTime() {
return turnaroundTime;
}
public void setTurnaroundTime(int turnaroundTime) {
this.turnaroundTime=turnaroundTime;
}
public double getWeightTurnaroundTime() {
return weightTurnaroundTime;
}
public void setWeightTurnaroundTime(double weightTurnaroundTime) {
this.weightTurnaroundTime=weightTurnaroundTime;
}
}
```
阅读全文