进程调度算法模拟程序设计

2025-01-03 20:58:17
推荐回答(4个)
回答1:

public class PrivilegeProcess {
public static void main(String[] args) {
MyQueue myqueue = new MyQueue();//声明队列
PCB[] pcb = {new PCB(001,8,1),new PCB(002,7,9),new PCB(003,3,8),new PCB(004,1,7),new PCB(005,7,4)};
PCB para = new PCB();
for(int i=0;i for(int j=i;j if(pcb[i].privilege < pcb[j].privilege){
para = pcb[i];
pcb[i] = pcb[j];
pcb[j] = para;
}
}
}
System.out.println("初次入队后各进程的顺序:");
for(int i=0;i System.out.println("初次入队后 # processname : " + pcb[i].name + " totaltime : " + pcb[i].totaltime + " privilege :" + pcb[i].privilege);
}
System.out.println();
myqueue.start(pcb);
}
}

class MyQueue {
int index = 0;
PCB[] pc = new PCB[5];
PCB[] pc1 = new PCB[4];
PCB temp = new PCB();

public void enQueue(PCB process){//入队算法
if(index==5){
System.out.println("out of bounds !");
return;
}
pc[index] = process;
index++;
}

public PCB deQueue(){//出队算法
if(index==0)
return null;
for(int i=0;i pc1[i] = pc[i+1];
}
index--;
temp = pc[0];
for(int i=0;i pc[i] = pc1[i];
}
return temp;
}

public void start(PCB[] pc){//显示进程表算法
while(pc[0].isNotFinish==true||pc[1].isNotFinish==true||pc[2].isNotFinish==true||pc[3].isNotFinish==true||pc[4].isNotFinish==true){
//*注意:||运算符,所有表达式都为false结果才为false,否则为true
for(int i=0;i pc[i].run(this);
}
System.out.println();
for(int i=0;i for(int j=i;j if(pc[i].privilege < pc[j].privilege){
temp = pc[i];
pc[i] = pc[j];
pc[j] = temp;
}
}
}
}
}
}

class PCB {//声明进程类
int name,totaltime,runtime,privilege;
boolean isNotFinish;

public PCB(){

}

public PCB(int name, int totaltime, int privilege){
this.name = name;//进程名
this.totaltime = totaltime;//总时间
this.privilege = privilege;//优先级别
this.runtime = 2;//时间片,这里设值为2
this.isNotFinish = true;//是否执行完毕
System.out.println("初始值: processname : " + name + " totaltime : " + totaltime + " privilege :" + privilege );
System.out.println();
}

public void run (MyQueue mq){//进程的基于时间片的执行算法
if(totaltime>1){
totaltime-=runtime;//在总时间大于1的时候,总时间=总时间-时间片
privilege--;
System.out.println(" processname : " + name + " remaintime : " + totaltime + " privilege :" + privilege );
}else if(totaltime==1){
totaltime--;//在总时间为1时,执行时间为1
privilege--;
System.out.println(" processname : " + name + " remaintime : " + totaltime + " privilege :" + privilege );
}else{
isNotFinish = false;//总时间为0,将isNotFinish标记置为false
}
if(isNotFinish==true){
mq.deQueue();
mq.enQueue(this);
}
}
}

回答2:

#include
using namespace std;
#include
typedef struct pcb
{
string name;
int number;
int ntime;
int runtime;
int priority;
} PCB,*PPCB;
void Input(PPCB p,int num);
void Sort(PPCB p , int num);
bool RunOnce(PPCB p,int num);
int main ()
{
PPCB p;
int num;
cout<<"请输入要执行的进程个数:";
cin>>num;
p = new PCB[num];
Input(p,num);
bool bRun = true;
while (bRun)
{
bRun = RunOnce(p,num);
if (p[num-1].ntime == 0)
num --;
system("pause");
}
return 0;
}
bool RunOnce(PPCB p,int num)
{
for (int i = 0 ; i < num ; i ++)
{
if (i == 0)
{
p[i].runtime++;
p[i].ntime--;
cout<<"运行进程\n";
cout<<"进程名称\t进程号\t\t需要时间\t运行时间\t优先级\n";
cout< p[i].priority--;
if (p[i].ntime <= 0)
{
p[i].priority = 0;
}
cout<<"\n就绪队列\n";
cout<<"进程名称\t进程号\t\t需要时间\t运行时间\t优先级\n";
}
else
{
cout< }

}
Sort(p,num);
if (p[0].ntime <= 0)
return false;
else
return true;
}
void Input(PPCB p,int num)
{
for (int i = 0 ; i < num ; i ++)
{
p[i].number = i+1;
cout<<"进程号: NO"< cin>>p[i].name;
cout<<"需要时间:";
cin>>p[i].ntime;
p[i].runtime = 0;
cout<<"优先级:";
cin>>p[i].priority;
}
cout<<"进程名称\t进程号\t\t需要时间\t运行时间\t优先级\n";
for (i = 0 ; i < num ; i ++)
{
cout< }
Sort(p,num);
}

void Sort(PPCB p , int num){
int i,j,temp;
string str;
for(i = 0 ; i < num - 1; i++)
{
for(j = num - 1 ; j > i; j--)
{
if (p[j].priority > p[j - 1].priority)
{
str = p[j].name;
p[j].name = p[j - 1].name;
p[j - 1].name = str;

temp = p[j].ntime;
p[j].ntime = p[j - 1].ntime;
p[j - 1].ntime = temp;

temp = p[j].number;
p[j].number = p[j - 1].number;
p[j - 1].number = temp;

temp = p[j].priority;
p[j].priority = p[j - 1].priority;
p[j - 1].priority = temp;

temp = p[j].runtime;
p[j].runtime = p[j - 1].runtime;
p[j - 1].runtime = temp;
}
}
}
}

回答3:

这么有技术含量的问题,应该要更有技术含量的回答,你给个30分悬赏,高手都不睬你~!

回答4:

网页链接里面有java编译 进程调度算法 详细代码,可以输出结果并且绘制折线图