當前位置:首頁 » 課程大全 » 操作系統課程設計進程調度模擬演算法

操作系統課程設計進程調度模擬演算法

發布時間: 2021-02-18 09:27:04

Ⅰ 操作系統進程調度演算法模擬

這是主要演算法:你可以參考一下
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而餓死。

Ⅵ 操作系統課程設計任務書:進程調度演算法的設計

哥們不是華航的吧,,怎麼都一樣的作業啊

熱點內容
武漢大學學生會輔導員寄語 發布:2021-03-16 21:44:16 瀏覽:612
七年級學生作文輔導學案 發布:2021-03-16 21:42:09 瀏覽:1
不屑弟高考成績 發布:2021-03-16 21:40:59 瀏覽:754
大學畢業證會有成績單 發布:2021-03-16 21:40:07 瀏覽:756
2017信陽學院輔導員招聘名單 發布:2021-03-16 21:40:02 瀏覽:800
查詢重慶2018中考成績查詢 發布:2021-03-16 21:39:58 瀏覽:21
結業考試成績怎麼查詢 發布:2021-03-16 21:28:40 瀏覽:679
14中醫醫師資格筆試考試成績查分 發布:2021-03-16 21:28:39 瀏覽:655
名著賞析課程標准 發布:2021-03-16 21:27:57 瀏覽:881
北京大學商業領袖高端培訓課程 發布:2021-03-16 21:27:41 瀏覽:919