操作系统课程设计进程调度模拟算法
Ⅰ 操作系统进程调度算法模拟
这是主要算法:你可以参考一下
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<pcb.length;i++){//初始化后首先执行一次排序,这里使用的是选择排序,优先级高的先入队
for(int j=i;j<pcb.length;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<pcb.length;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.length;i++){
pc1[i] = pc[i+1];
}
index--;
temp = pc[0];
for(int i=0;i<pc1.length;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.length;i++){
pc[i].run(this);
}
System.out.println();
for(int i=0;i<pc.length;i++){//所有进程每执行完一次时间片长度的运行就重新按优先级排列一次
for(int j=i;j<pc.length;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);
}
}
}
Ⅱ (操作系统)编写进程调度算法程序
#include<iostream>
#include<string>
#include<iomanip>
#include<windows.h>
using namespace std;
typedef struct Process
{ string id;
int arrive_time;
int sever_time;
int finish_time;
int turnover_time;
Process * next;
}Process,* Linkp; class FCFS_schele
{ public: FCFS_schele()
{
Creat_queue();
}
~FCFS_schele();
void Creat_queue();
void Insert_queue();
void orderInsert_queue();
void Out_queue();
void Printall();
void Sort_queue();
Process Gethead();
private:
Linkp head,tail;
int num;
Process Creat_process();
};///////////////////////////////////////////////////////方法的具体实现void FCFS_schele::Creat_queue()
{ head=new Process;
head->next=0;
tail=head;
num=0;
}
ostream& operator <<(ostream& out,Process& a) //对插入流运算符<<进行重载
{ out<<"process id:"<<a.id <<" arrivetime:"<<a.arrive_time
<<" severtime:"<<a.sever_time<<endl;
return(out);
}Process FCFS_schele::Creat_process()
{ Process a;
cout<<"please input process id"<<num+1<<":";
cin>>a.id ;
cout<<"please input process arrivetime:";
cin>>a.arrive_time ;
cout<<"please input process severtime:";
cin>>a.sever_time ;
a.finish_time=0;
a.turnover_time=0;
a.next =0;
return(a);
} void FCFS_schele::Insert_queue ()
{ Linkp p;
p=new Process;
*p=Creat_process();
if(num==0)
{p->finish_time=p->arrive_time+p->sever_time;<br> p->turnover_time=p->finish_time-p->arrive_time ;<br> }
else
{p->finish_time=tail->finish_time + p->sever_time ;<br> p->turnover_time=p->finish_time - p->arrive_time ;<br> }
tail->next=p;
tail=p;
num++;
}
void FCFS_schele::Out_queue() //进程调度出队
{ Linkp p;
p=head->next;
if(!p) cout<<"empty!\n";
else
{ head->next=p->next;
if(p->next==NULL) tail=head;
cout<<"process id:"<<p->id<<" arrivetime:"<<p->arrive_time
<<" severtime:"<<p->sever_time<<" finishtime:"<<p->finish_time
<<" turnovertime:"<<p->turnover_time <<endl;
delete p;
num--;
}
}
Process FCFS_schele::Gethead()throw(int)
{ Linkp p;
p=head->next;
if(p) return(*p);
else throw 1; //当队空无法返回Process类型返回值时抛出异常错误整形值1
}
void FCFS_schele::Printall()
//打印进程队列所有进程信息
{ Linkp p;
float sum_wghtime=0;
p=head->next;
cout<<" Process Information\n";
cout<<"process id arrivetime severtime finishtime turnovertime weightime\n";
while(p)
{ cout<<p->id<<setw(14)<<p->arrive_time
<<setw(14)<<p->sever_time<<setw(14)<<p->finish_time<<setw(14)<<p->turnover_time
<<setw(14)<<float(p->turnover_time)/float(p->sever_time )<<endl;
sum_wghtime=sum_wghtime+(float)p->turnover_time/p->sever_time;
p=p->next;
}
cout<<"平均带权周转时间为:"<<sum_wghtime/num<<endl;
}
void FCFS_schele::Sort_queue ()
//采用选择法将队列中的进程按servertime长短进行换值不换址的排序{ Linkp location,search,record,track;
Process temp;
track=head->next;
location=track->next ;
while(location && location->next )
{ record=search=location;
while(search)
{ if(search->sever_time <record->sever_time ) record=search;
search=search->next ;
} if(record!=location)
{
temp=*record;
record->arrive_time =location->arrive_time ;
record->id =location->id ;
record->sever_time =location->sever_time ;
location->id =temp.id;
location->sever_time =temp.sever_time ;
location->arrive_time =temp.arrive_time ;
location->finish_time =track->finish_time + location->sever_time ;
location->turnover_time =location->finish_time - location->arrive_time ;
}
track=location;
location=location->next ;
}
if(tail==location)
{tail->finish_time = track->finish_time +tail->sever_time ;<br> tail->turnover_time =tail->finish_time -tail->arrive_time ;<br> }}
FCFS_schele::~FCFS_schele ()
{ Linkp p;
while(p=head)
{
head=head->next ;
delete p;
}
cout<<"the storage of memory has been distory!\n";
} void FCFS_schele::orderInsert_queue () //进程插入排序
{ Linkp p,pr;
p=new Process;
*p=Creat_process(); if(num==0)
{
head->next=p;
tail=p;
p->finish_time =p->arrive_time + p->sever_time ;
p->turnover_time =p->finish_time - p->arrive_time ;
}
else
{
pr=head->next ;
while(pr->next && p->sever_time >= pr->next->sever_time )
pr=pr->next;
if(pr->next==0)
{
p->finish_time = tail->finish_time + p->sever_time ;
p->turnover_time =p->finish_time - p->arrive_time ;
tail->next=p;
tail=p;
}
else
{
p->next =pr->next ;
pr->next=p;
while(p)
{
p->finish_time =pr->finish_time + p->sever_time ;
p->turnover_time = p->finish_time - p->arrive_time ;
pr=p;
p=p->next ;
}
}
}
num++;
} void main()
{
// DWORD start=GetTickCount();
FCFS_schele os;
os.orderInsert_queue ();
os.orderInsert_queue ();
os.orderInsert_queue ();
os.orderInsert_queue ();
os.orderInsert_queue ();
// os.orderInsert_queue ();
// os.orderInsert_queue ();
os.Printall ();
// os.Sort_queue ();
os.Sort_queue ();
os.Printall ();
os.Out_queue ();
os.Out_queue ();
os.Out_queue ();
os.Out_queue ();
/* try{
cout<<os.Gethead () ;
}
catch(int i)
{ if(i==1) cout<<"empty!\n";
}*/
//
// DWORD end=GetTickCount();
// cout<<"spend time:"<<(end-start)<<endl;
/*
os.Out_queue ();
*/
}
Ⅲ 进程调度模拟程序
#include<windows.h>
#include<iostream.h>
#include<string.h>
#define P_NUM 3 //进程数
#define P_TIME 1//时间片长度
#define MIN -9999
enum state //进程状态
{
ready, //就绪
run, //执行
wait, //阻塞
finish //完成
};
class Pcb
{
public:
static void print(){};
~Pcb();
protected:
char* name; //进程名
int allTime; //需要运行时间
int cpuTime; //已用cpu时间
state process; //进程状态
};
class HPcb:public Pcb
{
public:
static void print();
static void highS();
static int getFirst();
private:
int firstNum;
};
HPcb hpcb[P_NUM];
class FPcb:public Pcb
{
public:
static void print();
static void fcfs();
private:
int comeTime;
};
FPcb fpcb[P_NUM];
int HPcb::getFirst() //得到优先级最高的进程
{
int k=0;
for(int i=1;i<P_NUM;i++)
if(hpcb[k].firstNum<hpcb[i].firstNum)
k=i;
return k;
}
void HPcb::highS() //最高优先数优先的调度算法
{
int ii,f,i=0;
for(;i<P_NUM;i++)
{
char* ch;
ch=new char[1];
cout<<"请输入第"<<i+1<<"个进程的“进程名”、“优先数”、“需要运行的时间”:"<<endl;
cin>>ch;
hpcb[i].name=new char[strlen(ch)+1];
strcpy(hpcb[i].name,ch);
cin>>hpcb[i].firstNum>>hpcb[i].allTime;
hpcb[i].cpuTime=0;
hpcb[i].process=ready;
}
do
{
f=getFirst();
hpcb[f].cpuTime+=P_TIME;
hpcb[f].firstNum--;
hpcb[f].process=run;
if(hpcb[f].cpuTime>=hpcb[f].allTime)//该进程执行完成
{
hpcb[f].firstNum=MIN;
hpcb[f].process=finish;
hpcb[f].cpuTime=hpcb[f].allTime;//防止所用时间超过总的时间
system("cls");
print();
Sleep(1000);
}
else
{
hpcb[f].firstNum++;//为了输出改变前的相关信息
system("cls");
print();
Sleep(1000);
hpcb[f].firstNum--;
hpcb[f].process=ready;
}
for(ii=0;ii<P_NUM;ii++)//用于判断是否还有进程未完成
if(hpcb[ii].firstNum!=MIN)
break;
}while(ii<P_NUM);//还有进程未完成
cout<<"所有进程已运行完成!"<<endl;
}
Pcb::~Pcb()
{
delete [] name;
}
void FPcb::fcfs() //先来先服务算法
{
int i=0;
for(;i<P_NUM;i++)
{
char* ch;
ch=new char[1];
cout<<"请输入第"<<i+1<<"个进程的“进程名”、“需要运行的时间”:"<<endl;
cin>>ch;
fpcb[i].name=new char[strlen(ch)+1];
strcpy(fpcb[i].name,ch);
cin>>fpcb[i].allTime;
fpcb[i].comeTime=i+1;
fpcb[i].cpuTime=0;
fpcb[i].process=ready;
}
for(i=0;i<P_NUM;i++) //P_NUM个进程
{
for(int j=0;j<fpcb[i].allTime;j+=P_TIME) //每个进程所用时间
{
fpcb[i].cpuTime+=P_TIME; //第i个进程所用时间加1个时间片
if(fpcb[i].cpuTime<fpcb[i].allTime) //第i个进程还未完成
fpcb[i].process=run; //将其状态设为就绪态
else
{
fpcb[i].cpuTime=fpcb[i].allTime; //防止所用时间超过总时间,因为时间片不定
fpcb[i].process=finish; //将状态设为完成态
}
if(j+P_TIME>=fpcb[i].allTime)
{
if((i+1)!=P_NUM) //如果第i+1个进程不是最后一个进程
{
fpcb[i+1].cpuTime=fpcb[i].cpuTime-fpcb[i].allTime;
fpcb[i].cpuTime=fpcb[i].allTime;
fpcb[i].process=finish;
fpcb[i+1].process=run;
}
else
{
fpcb[i].process=finish;
fpcb[i].cpuTime=fpcb[i].allTime;
}
}
system("cls");
print();
Sleep(1000);
}
}
cout<<"所有进程已运行完成!"<<endl;
}
void HPcb::print()
{
cout<<"*********************************************************************"<<endl;
cout<<"进程名"<<"\t"<<"还需运行时间\t"<<"已用CPU时间"<<"\t"<<"优先级"<<"\t"<<"状态"<<endl;
for(int i=0;i<P_NUM;i++)
{
cout<<hpcb[i].name<<"\t\t"<<hpcb[i].allTime-hpcb[i].cpuTime<<"\t\t"<<hpcb[i].cpuTime<<"\t"<<hpcb[i].firstNum<<"\t";
switch(hpcb[i].process)
{
case wait:cout<<"阻塞态"<<endl;break;
case ready:cout<<"就绪态"<<endl;break;
case run:cout<<"运行态"<<endl;break;
case finish:cout<<"完成态"<<endl;break;
}
}
cout<<"---------------------------------------------------------------------"<<endl;
cout<<endl;
}
void FPcb::print()
{
cout<<"*********************************************************************"<<endl;
cout<<"进程名"<<"\t"<<"还需运行时间\t"<<"已用CPU时间"<<"\t"<<"状态"<<endl;
for(int i=0;i<P_NUM;i++)
{
cout<<fpcb[i].name<<"\t\t"<<fpcb[i].allTime-fpcb[i].cpuTime<<"\t\t"<<fpcb[i].cpuTime<<"\t";
switch(fpcb[i].process)
{
case wait:cout<<"阻塞态"<<endl;break;
case ready:cout<<"就绪态"<<endl;break;
case run:cout<<"运行态"<<endl;break;
case finish:cout<<"完成态"<<endl;break;
}
}
cout<<"---------------------------------------------------------------------"<<endl;
cout<<endl;
}
int main()
{
char ch;
cout<<"请选择算法:\n1. 先来先服务算法\n2. 最高优先数优先的调度算法\n0. 退出"<<endl;
cin>>ch;
if(ch=='1')
FPcb::fcfs();
else if(ch=='2')
HPcb::highS();
return 0;
}
Ⅳ 操作系统 作业调度算法与进程调度算法 题目
进入主存的时间是指进程到达后主存需求得到满足的时间,分析内存占用就好了版
进程一执权行完了,也就是第8.5秒,2,3,4到达,4的需计算时间最短,被执行,执行完是第8.7秒
进程5在4执行时也到达,执行进程5,执行完是8.8秒,然后就是执行进程3了,再然后是进程2所以2的开始时间是9.1秒
这里一个作业就是一个进程,应该没什么区别
进程2在8.2s被输入,此时有空闲内存85k,进程2需要60k,立即被满足,所以是8.2
所谓的调度是调度的cpu,毕竟cpu才是用来计算的~~
Ⅳ 《操作系统》—进程调度算法
抢占式调度算法可能导致高优先级的进程一直占用CPU,而那些低优先级的进程可能一直得不到CPU而饿死。
Ⅵ 操作系统课程设计任务书:进程调度算法的设计
哥们不是华航的吧,,怎么都一样的作业啊