发一些大三操作系统的实验代码吸引阅读量吧,当时做实验的时候看见网上很多人写的代码并不好,而且很多人都有错误的地方。如果好的话希望能点赞关注。
常见的进程调度算法有:先来先服务 (FCFS,first come first served) 、最短作业优先(SJF, Shortest Job First) 、
时间片轮转算法(RR,Round-Robin) 、多级反馈队列(Multilevel Feedback Queue) 、
最高响应比优先法(HRRN,Highest Response Ratio Next)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include //setw
#include //malloc
#include
using namespace std;
struct FCFS_SJF_r//先来先服务FCFS,短作业优先算法SJF,优先级算法r的结构体
{
char name; //进程名
float arrivetime; //到达时间
float servetime; //服务时间
float finishtime; //完成时间
float roundtime; //周转时间
float daiquantime; //带权周转时间
struct FCFS_SJF_r *link;//结构体指针
};
FCFS_SJF_r *head = NULL;
struct FCFS_SJF_r a[100];//初始化指针和数组
struct FCFS_SJF_r *sortarrivetime(struct FCFS_SJF_r a[], int n); //声明到达时间冒泡排序函数
struct FCFS_SJF_r *sortservetime(struct FCFS_SJF_r a[], int n);//声明服务时间冒泡排序函数
void FCFS(struct FCFS_SJF_r a[], int n, float &t1, float &t2);//先来先服务算法
void SJF(struct FCFS_SJF_r a[], int n, float &t1, float &t2);//短作业优先调度算法
void r(struct FCFS_SJF_r a[], int n, float &t1, float &t2);//优先级算法
struct FCFS_SJF_r *sortarrivetime(struct FCFS_SJF_r a[], int n)//按到达时间进行冒泡排序
{
int i, j;
struct FCFS_SJF_r t;
int flag;
for (i = 1; ia[j + 1].arrivetime) //将到达时间短的交换到前边
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
flag = 1;//交换
}
}
if (flag == 0)//如果一趟排序中没发生任何交换,则排序结束
{
break;
}
}
return a; //返回排序后进程数组
}
//按服务时间进行冒泡排序
struct FCFS_SJF_r *sortservetime(struct FCFS_SJF_r a[], int n)
{
int i, j;
struct FCFS_SJF_r t;
int flag;
for (i = 1; ia[j + 1].arrivetime) //将到达时间短的交换到前边
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
flag = 1;//交换
}
}
for (j = 0; ja[j + 1].servetime)&&(a[j].arrivetime>=a[j + 1].arrivetime))//将服务时间短的交换到前边
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
flag = 1;//交换
}
}
if (flag == 0)//如果一趟排序中没发生任何交换,则排序结束
{
break;
}
}
return a; //返回排序后进程数组
}
//先来先服务算法
void FCFS(struct FCFS_SJF_r a[], int n, float &t1, float &t2)
{
int i;
a[0].finishtime = a[0].arrivetime + a[0].servetime; //完成时间=到达时间-服务时间
a[0].roundtime = a[0].finishtime - a[0].arrivetime; //周转时间=完成时间-提交时间
a[0].daiquantime = a[0].roundtime / a[0].servetime; //带权时间=周转时间/服务时间
for (i = 1; i= a[c].arrivetime)&&(a[i - 1].finishtime >= a[d].arrivetime) && (a[c].servetime > a[d].servetime))
{
t = a[c];
a[c] = a[d];
a[d] = t;
}
}
if (a[i].arrivetime= a[c].arrivetime) && (a[i - 1].finishtime >= a[d].arrivetime) && (a[c].servetime > a[d].servetime))
{
t = a[c];
a[c] = a[d];
a[d] = t;
}
}
if (a[i].arrivetimebuffer;
int i, j, flag;
RR1 t;
float temp = 0.0f; //缓存最后一个正数rest
float nowtime = 0.0f;
float sum_zhouzhuan = 0.0f, sum_daiquan = 0.0f; //所有进程总周转时间,所有进程总带权周转时间
float avr_zhouzhuan = 0.0f, avr_daiquan = 0.0f;
for (i = 0; i < a; i++)
{
printf("%d 进程名: ", i + 1);
scanf("%s", &b[i].name);
printf("到达时间:");
scanf("%f", &b[i].arrive);
printf("服务时间:");
scanf("%f",&b[i].run);
b[i].rest = b[i].run;
b[i].state = "wait for coming";
}
float slice = 0.0f;
printf("请输入时间块大小: ");
scanf("%f", &slice);
flag = 0;
for (i = 1; i < a; i++)//按到达时间排序
{
for (j = 0; j < a - i; j++)
{
if (b[j].arrive > b[j + 1].arrive) //将到达时间短的交换到前边
{
t = b[j];
b[j] = b[j + 1];
b[j + 1] = t;
flag ++;//交换
}
}
if (flag == 0)//如果一趟排序中没发生任何交换,则排序结束
{
break;
}
}
cout << "按到达时间排序共交换" << flag << "次"<= b[m].arrive)
{
b[m].state = "ready";
buffer.push(&b[m]);
n++;
}
}
temp = buffer.front()->rest;
nowtime += slice;
buffer.front()->rest = buffer.front()->rest - slice;
buffer.front()->state = "running";
cout << "\n-----------------------------------------------\n";
cout << "round:" << round<<" "<name<<" runing" << endl;
if (buffer.front()->rest <= 0)
{
buffer.front()->finish = nowtime - slice + temp;
nowtime = buffer.front()->finish;
buffer.front()->zhouzhuan = nowtime - buffer.front()->arrive;
buffer.front()->daiquan = buffer.front()->zhouzhuan / buffer.front()->run;
sum_zhouzhuan += buffer.front()->zhouzhuan;
sum_daiquan += buffer.front()->daiquan;
buffer.front()->rest = 0;
buffer.front()->state = "run and end";
cout << "note:process " << buffer.front()->name << " " << "end at " << buffer.front()->finish << " zhouzhuantime=" << buffer.front()->zhouzhuan <<" daiquanzhouzhuan="<< buffer.front()->daiquan <<" round="<= b[m].arrive)
{
b[m].state = "ready";
buffer.push(&b[m]);
n++;
}
if ((nowtime < b[m].arrive) && (buffer.empty()))
{
cout << "\n---------------bad status--------------\n";
cout << "name\t";
cout << setw(2) << "arrive\t";
cout << setw(2) << "run\t";
cout << setw(2) << "rest\t";
cout << setw(2) << "state\t" << endl;
for (int p = 0; p < a; p++)
{
cout << b[p].name << "\t";
cout << b[p].arrive << "\t";
cout << b[p].run << "\t";
cout << b[p].rest << "\t";
cout << b[p].state;
cout << endl;
}
cout << "there is a bad status" << endl;
cout << "there is(are) " << b[m].arrive - nowtime << " free time(times)" << endl;
cout << "\n---------------bad status--------------\n";
cout << "now when " << b[m].name << " is coming." << endl << "time is " << b[m].arrive << endl;
b[m].state = "ready";
nowtime = b[m].arrive;
buffer.push(&b[m]);
n++;
}
}
}
else
{
for (int m = n; m< a; m++)//考虑进程在时间片结束前到达的情况
{
if (nowtime >= b[m].arrive)
{
b[m].state = "ready";
buffer.push(&b[m]);
n++;
}
/*if ((nowtime < b[m].arrive) && (buffer.empty()))//永远不会走到这里
{
cout << "\n---------------bad status--------------\n";
cout << "name\t";
cout << setw(2) << "arrive\t";
cout << setw(2) << "run\t";
cout << setw(2) << "rest\t";
cout << setw(2) << "state\t" << endl;
for (int p = 0; p < a; p++)
{
cout << b[p].name << "\t";
cout << b[p].arrive << "\t";
cout << b[p].run << "\t";
cout << b[p].rest << "\t";
cout << b[p].state;
cout << endl;
}
cout << "there is a bad status" << endl;
cout << "there is(are) " << b[m].arrive - nowtime << " free time(times)" << endl;
cout << "\n---------------bad status--------------\n";
cout << "now when " << b[m].name << " is coming."<state = "ran and waiting for next time";
buffer.push(buffer.front());
buffer.pop();
}
cout << "name\t";
cout << setw(2) << "arrive\t";
cout << setw(2) << "run\t";
cout << setw(2) << "rest\t";
cout << setw(2) << "state\t" << endl;
for (int p = 0; p < a; p++)
{
cout << b[p].name << "\t";
cout << b[p].arrive << "\t";
cout << b[p].run << "\t";
cout << b[p].rest << "\t";
cout << b[p].state;
cout << endl;
}
round++;
}
printf("\n总周转时间: ");
printf("%f", sum_zhouzhuan);
printf("\n总带权周转时间: ");
printf("%f\n", sum_daiquan);
avr_zhouzhuan = sum_zhouzhuan / a;
avr_daiquan = sum_daiquan / a;
printf("\n平均周转时间: ");
printf("%f", avr_zhouzhuan);
printf("\n平均带权周转时间: ");
printf("%f\n", avr_daiquan);
}
int main()
{
float t1 = 0.0f; //总周转时间
float t2 = 0.0f; //总带权周转时间
float avr_t1 = 0.0f; //平均周转时间
float avr_t2 = 0.0f; //平均带权周转时间
int n, i;
char select = ' '; //选择算法变量标识
while (select != 'e' && select != 'E') //不为退出标识,保持循环
{
system("cls");
printf("请选择算法:\na.先来先服务算法\nb.短作业优先算法\nc.时间片轮转算法\nd.优先级算法\ne.退出程序\n输入选择字符标号: ");
scanf("%c", &select);
if (select == 'a' || select == 'A') //先来先服务算法
{
printf("\n\n=================先来先服务算法FCFS================\n\n");
printf("请输入进程数:");
scanf("%d", &n);
for (i = 0; i