一、实验目的
在多到程序或多任务系统中,系统同时处于就绪状态的进程有若干个。也就是说能运行的进程数远远大于处理机个数。为了系统中的各进程有条不紊的运行,必须选择某种调度策略,以选择一进程占用处理机。要求学生设计一个模拟单处理机调度的算法,以巩固和加深处理机调度的概念。
二、 实验内容
处理机管理是操作系统中非常重要的部分。为深入理解进程管理部分的功能,设计几个调度算法,模拟实现处理机的调度。
FCFS(先来先服务)
#include<iostream>
#include<string>
#define N 5
using namespace std;
int time=0;
int sum_turnaround_time=0;
class PCB
{
public:
PCB *point;
string pname;
int run_time;
int has_run_time;
int arrive_time;
char pstate;
};
void Pqueue(PCB *head,int &pcount, PCB &p)
{
if(head==NULL)
{
head=&p;
}
else
{
head[pcount-1].point=&p;
}
}
void run(PCB *head,int &pcount)
{
time++;
if(time<head->arrive_time)
time=head->arrive_time;
head->run_time--;
head->has_run_time++;
cout<<head->pname<<" is running, and it has run "<<head->has_run_time<<", and the rest time is "<<head->run_time<<endl;
if(head->run_time==0)
{
head->pstate='C';
cout<<"the "<<head->pname<<" has finished, and its turnaround time is "<<time-head->arrive_time<<endl;
sum_turnaround_time+=time-head->arrive_time;
for(int i=0;i<pcount;i++)
if(head[i].pstate=='R')
cout<<"The process "<<head[i].pname<<" is ready."<<endl;
cout<<"----------------------------------------------------------"<<endl;
pcount--;
if(pcount==0)
cout<<"The all process have finished!"<<endl;
else
{
head=head->point;
run(head, pcount);
}
}
else
run(head, pcount);
}
int main()
{
PCB* head=new PCB();
head=NULL;
int pcount=0;
PCB P[10];
for(int i=0; i<N; i++)
{
if(i==0)
{
cout<<"please input a process's name: ";
cin>>P[i].pname;
cout<<"please input a process's time of arriving: ";
cin>>P[i].arrive_time;
cout<<"please input a process's time of running: ";
cin>>P[i].run_time;
cout<<"----------------------------------------------------------"<<endl;
P[i].pstate='R';
P[i].point=NULL;
P[i].has_run_time=0;
pcount++;
head=&P[i];
}
else
{
cout<<"please input a process's name: ";
cin>>P[i].pname;
cout<<"please input a process's time of arriving, and it should be later than "<<P[i-1].arrive_time<<" : ";
cin>>P[i].arrive_time;
cout<<"please input a process's time of running: ";
cin>>P[i].run_time;
cout<<"----------------------------------------------------------"<<endl;
P[i].pstate='R';
P[i].point=NULL;
P[i].has_run_time=0;
pcount++;
head[pcount-2].point=&P[i];
}
}
run(head, pcount);
cout<<"----------------------------------------------------------"<<endl<<"The average turnaround time is "<<sum_turnaround_time/N<<endl;
return 0;
}
实验结果
动态优先级调度
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#define N 5
using namespace std;
int stime = 0;
class PCB
{
public:
PCB *point;
string pname;
int run_time;
int prank;
int runned_time;
char pstate;
};
bool cmp(PCB a, PCB b)
{
return a.prank<b.prank;
}
void run(PCB *head, PCB* running_process, int &pcount)
{
cout<<running_process->pname<<" is running and it has run "<<running_process->runned_time<<" and the rest stime is "<<running_process->run_time<<" and its rank is "<<running_process->prank<<endl;
running_process->run_time--;
running_process->prank++;
stime++;
running_process->runned_time++;
int temp=0;
if(pcount>1)
{
cout<<"these processes is ready: ";
for(int i=1; i<pcount; i++)
cout<<head[i].pname<<" ";
cout<<endl;
sort(head, head+pcount, cmp);
for(int i=1; i<pcount; i++)
{
head[i-1].point=&head[i];
}
running_process=head;
/*
for(int i=1; i<pcount; i++)
if(head[temp].prank>head[i].prank)
temp=i;
running_process=&head[temp];
*/
}
if(running_process->run_time==0)
{
running_process->pstate='C';
//cout<<setw(20)<<setfill(' ')<<left<<head->pname<<setw(20)<<setfill(' ')<<left<<stime<<setw(20)<<setfill(' ')<<left<<stime<<setw(20)<<setfill(' ')<<left<<stime+head->run_time<<endl;
cout<<running_process->pname<<" is finished!"<<endl;
pcount--;
if(pcount!=0)
{
head=head->point;
running_process=head;
run(head, running_process, pcount);
}
else
cout<<"The all process have finished!"<<endl;
}
else
run(head, running_process, pcount);
}
int main()
{
PCB* head=new PCB();
head=NULL;
PCB* running_process=new PCB();
running_process=NULL;
int pcount=0;
PCB P[N];
for(int i=0; i<N; i++)
{
cout<<"please input a process's name: ";
cin>>P[i].pname;
cout<<"please input a process's rank: ";
cin>>P[i].prank;
cout<<"please input a process's stime of running: ";
cin>>P[i].run_time;
cout<<"----------------------------------------------------------"<<endl;
P[i].pstate='R';
P[i].point=NULL;
P[i].runned_time=0;
pcount++;
}
sort(P, P+pcount, cmp);
head=&P[0];
running_process=&P[0];
for(int i=1; i<pcount; i++)
{
P[i-1].point=&P[i];
}
//cout<<setw(20)<<setfill(' ')<<left<<"process"<<setw(20)<<setfill(' ')<<left<<"start_time"<<setw(20)<<setfill(' ')<<left<<"end_time"<<setw(20)<<setfill(' ')<<left<<"zhouzhuan"<<endl;
run(head, running_process, pcount);
return 0;
}
实验结果(动态展示处理过程)
时间片轮转
//书上的代码
#include<stdio.h>
#include <iostream>
#include<conio.h>
using namespace std;
typedef struct pcb {
char pname[50]; //进程名
char state; //进程状态
int runtime; //估计运行时间
int arrivetime; //到达时间
struct pcb* next; //链接指针
}PCB;
PCB head_input;
PCB head_run;
PCB* pcb_input;
static char R = 'r', C = 'c';
unsigned long current; //记录系统当前时间
void inputprocess(); //建立进程函数
int readyprocess(); //建立就绪队列函数
int readydata(); //判断进程是否就绪函数
int runprocess(); //运行进程函数
FILE* f;
//定义就绪队列函数
int readyprocess() {
while (1) {
if (readydata() == 0) //判断是否就绪函数
return 1;
else
runprocess(); //运行进程函数
}
}
//定义判断就绪队列是否有进程函数
int readydata() {
if (head_input.next == NULL) {
if (head_run.next == NULL)
return 0;
else
return 1;
}
PCB* p1, * p2, * p3;
p1 = head_run.next;
p2 = &head_run;
while (p1 != NULL) {
p2 = p1;
p1 = p2->next;
}
p1 = p2;
p3 = head_input.next;
p2 = &head_input;
while (p3 != NULL) {
if (((unsigned long)p3->arrivetime <= current) && (p3->state == R)) {
printf("Time slice is %8d(time %4d);Process %s start,\n",
current, (current + 500) / 1000, p3->pname);
fprintf(f, "Time slice is %8d(time %4d);Process %s start,\n",
current, (current + 500) / 1000, p3->pname);
p2->next = p3->next;
p3->next = p1->next;
p1->next = p3;
p3 = p2;
}
p2 = p3;
p3 = p3->next;
}
return 1;
}
//定义运行进程函数
int runprocess() {
PCB* p1, * p2;
if (head_run.next == NULL) {
current++;
return 1;
}
else {
p1 = head_run.next;
p2 = &head_run;
while (p1 != NULL) {
p1->runtime--;
current++;
if (p1->runtime <= 0) {
printf("Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
p1->pname);
fprintf(f,"Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
p1->pname);
p1->state = C;
p2->next = p1->next;
delete p1;
p1 = NULL;
}
else {
p2 = p1;
p1 = p2->next;
}
}
return 1;
}
}
void inputprocess() {
PCB* p1, * p2;
int num;
unsigned long max = 0;
printf("How many process do you want to run:");
fprintf(f,"How many process do you want to run:");
scanf("%d", &num);
fprintf(f, "%d\n", &num);
p1 = &head_input;
p2 = p1;
p1->next = new PCB;
p1 = p1->next;
for (int i = 0; i < num; i++) {
printf("No. %3d process input pname:", i + 1);
fprintf(f, "No. %3d process input pname:", i + 1);
scanf("%s", p1->pname);
fprintf(f, "%s\n", p1->pname);
printf(" runtime:");
fprintf(f, " runtime:");
scanf("%d", &(p1->runtime));
fprintf(f, "%d\n", &(p1->runtime));
printf(" arrivetime:");
fprintf(f, " arrivetime:");
scanf("%d", &(p1->arrivetime));
fprintf(f, "%d\n", &(p1->arrivetime));
p1->runtime = (p1->runtime) * 1000;
p1->arrivetime = (p1->arrivetime) * 1000;
p1->state = R;
if ((unsigned long)(p1->arrivetime) > max)
max = p1->arrivetime;
p1->next = new PCB;
p2 = p1;
p1 = p1->next;
}
delete p1;
p1 = NULL;
p2->next = NULL;
}
//定义建立进程函数
void inputprocess1() {
PCB* p1, * p2;
int num;
unsigned long max = 0;
printf("How many process do you want to run:");
fprintf(f, "How many process do you want to run:");
scanf("%d", &num);
fprintf(f, "%d\n", &num);
pcb_input = new PCB;
p1 = pcb_input;
for (int i = 0; i < num; i++) {
printf("No. %3d process input pname:", i + 1);
fprintf(f, "No. %3d process input pname:", i + 1);
scanf("%s", p1->pname);
printf(" runtime:");
fprintf(f, " runtime:");
scanf("%d", &(p1->runtime));
fprintf(f, "%d\n", &(p1->runtime));
printf(" arrivetime:");
fprintf(f, " arrivetime:");
scanf("%d", &(p1->arrivetime));
fprintf(f, "%d\n", &(p1->arrivetime));
//p1->runtime = (p1->runtime);
//p1->arrivetime = (p1->arrivetime);
p1->runtime = (p1->runtime) * 1000;
p1->arrivetime = (p1->arrivetime) * 1000;
p1->state = R;
if (i != num - 1) {
p1->next = new PCB;
p1 = p1->next;
}
else {
p1->next = pcb_input;
}
}
p1 = pcb_input;
while (p1->next != pcb_input) {
printf("process name is %s\n", p1->pname);
fprintf(f,"process name is %s\n", p1->pname);
p1 = p1->next;
}
printf("process name is %s\n", p1->pname);
fprintf(f, "process name is %s\n", p1->pname);
}
//定义运行进程函数
void runprocess1() {
pcb* pre, * cur;
if (pcb_input == NULL)
return;
else {
cur = pcb_input;
pre = cur->next;
while (pre->next != cur) { //find the last node in the list
pre = pre->next;
}
while ((cur->runtime >= 0) || (cur->next != cur)) {
if (current < (unsigned long)cur->arrivetime) {
pre = cur;
cur = cur->next;
}
else {
if (current == (unsigned long)cur->arrivetime) {
printf("Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
cur->pname);
fprintf(f,"Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
cur->pname);
}
cur->runtime--;
if (cur->runtime < 0) {
printf("Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
cur->pname);
fprintf(f, "Time slice is %8d(time %4d);Process %s end. \n", current, (current + 500) / 1000,
cur->pname);
if (cur == cur->next) {
delete cur;
cur = NULL;
//break;
return;
}
else {
pre->next = cur->next;
pcb* tmp = cur;
delete tmp;
cur = pre->next;
}
}
else {
cur->runtime--;
pre = cur;
cur = cur->next;
}
}
current++;
}
}
}
//TO DO
int main() {
f = fopen("result.txt", "w");
printf("\ntime 1=1000 time slice\n");
fprintf(f,"\ntime 1=1000 time slice\n");
current = 0;
inputprocess();
readyprocess();
//inputprocess1();
//runprocess1();
getch();
fclose(f);
return 0;
}
运行结果
来源:CSDN
作者:这个寒星不太冷
链接:https://blog.csdn.net/weixin_44735393/article/details/104024949