資源簡介
該程序實現作業調度的RR算法,只要輸入時間片,進程號,到達時間,運行所需時間即可,輸出的是一條時間軸和對應的進程運行流程,表示一個時間點里哪個進程在運行。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
const?int?MAXN?=?1000;//假設能夠容納的進程最多的個數
?
struct?Process
{
????int?pos;??????????????????????????//代表第幾個輸入的進程
????char?Process_name[50];????????????//進程名字,默認最長長度占用50字符
????double?Arrival_time;?????????????//進程到達時間
????double?Service_time;?????????????//服務時間???????進程所需要的時間
????double?Start_time;???????????????//服務開始時間
????double?End_time;?????????????????//服務結束的時間
????double?Turnaround_time;??????????//周轉時間????進程結束的時間-進程到達時間
????double?Weight_Turnaround_time;???//帶權周轉時間???????周轉時間?/?服務時間
}process[MAXN];
double?every_time;
int?n;
double?temp[100];
double?tmp;
double?sumwait;
?
void?Init()
{
????printf(“請輸入時間片的時間:“);
????scanf(“%lf“&every_time);
????printf(“請輸入進程的數量:“);
????scanf(“%d“&n);
????for(int?i?=?0;i?????{
????????cout<<“請輸入第“<????????scanf(“%s“process[i].Process_name);
????????scanf(“%lf“&process[i].Arrival_time);
????????scanf(“%lf“&process[i].Service_time);
????????process[i].pos?=?i;
????}
}
bool?cmp1(const?Process?&aconst?Process?&b)
{
????return?a.Arrival_time?}
?
void?Solve_TSRA()
{
????/*
????時間片輪轉算法:
????*/
????printf(“時間片輪轉算法:\n“);
????queue?que;
????sort(processprocess+ncmp1);
????bool?vis[MAXN];??????//表示這個進程有沒有在完成,完成使用true表示
????Process?ans[MAXN];
????double?time?=?max(0.0process[0].Arrival_time);int?index?=?0;
????memset(visfalsesizeof(vis));
????que.push(process[0]);vis[process[0].pos]?=?1;
????for(int?i?=?0;i?????{
????????while(!que.empty())
????????{
????????????Process?temp?=?que.front();???que.pop();
????????????temp.Start_time?=?time;
????????????temp.End_time?=?temp.Service_time?>=?every_time???time?+?every_time?:?time?+?temp.Service_time;
????????????for(int?j?=?0;j?????????????????if(!vis[process[j].pos]?&&?process[j].Arrival_time?<=?temp.End_time)
????????????????{
????????????????????vis[process[j].pos]?=?1;
????????????????????que.push(process[j]);
????????????????}
????????????if(temp.Service_time?>?every_time)
????????????{
????????????????temp.Service_time?-=?every_time;
????????????????que.push(temp);
????????????????time?+=?every_time;
????????????}
????????????else
????????????????time?+=?temp.Service_time;???????//這里面的時間都是有聯系的,所以不用再次更新time
????????????ans[index++]?=?temp;?????????????//將順序存儲到最終的答案序列中
????????}
????????bool?flag?=?false;?//判斷是否所有的進程都已經完成
????????for(int?j?=?0;j?????????????if(!vis[process[j].pos])
????????????{
????????????????que.push(process[j]);//將一個時間最靠前的添加到隊列中
????????????????time?=?process[j].Arrival_time;???//這里就要更新time了,因為這里的時間和上面的有些脫節!
????????????????flag?=?true;break;
????????????}
????????if(!flag)?break;
????}
????printf(“進程的運行順序為:\n“);
????for(int?i?=?0;i?????????printf(“%.0lf?-?%s?-?“ans[i].Start_timeans[i].Process_name);
if(i==index-1)
?pr
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????92672??2018-12-14?12:04??RR\Debug\操作系統6.4.exe
?????文件?????421600??2018-12-14?12:04??RR\Debug\操作系統6.4.ilk
?????文件????1002496??2018-12-14?12:04??RR\Debug\操作系統6.4.pdb
?????文件???????3920??2018-12-14?11:32??RR\操作系統6.4\6.4.cpp
?????文件?????277061??2018-12-14?12:04??RR\操作系統6.4\Debug\6.4.obj
?????文件???????1414??2018-12-14?12:04??RR\操作系統6.4\Debug\cl.command.1.tlog
?????文件??????26442??2018-12-14?12:04??RR\操作系統6.4\Debug\CL.read.1.tlog
?????文件????????470??2018-12-27?11:16??RR\操作系統6.4\Debug\CL.write.1.tlog
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
?????文件??????????2??2018-12-14?12:04??RR\操作系統6.4\Debug\li
............此處省略50個文件信息
- 上一篇:五子棋MFC 五子棋MFC 五子棋MFC
- 下一篇:基于對話框的多線程進度條更新
評論
共有 條評論