试验一:
时间:20##-11-29
计算机实验机房2
朱蓉蓉
第一题:先来先服务算法
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct PCB
{
string name;//进程名
float ta;//进程到达时间
float ts;//进程估计运行的时间
float tb;//进程开始运行时间
float tm;//进程仍需运行的时间
float to;//进程完成的时间
float rn;//进程运行的次数
float totalTime;//周转时间
double weightTotalTime;//带权周转时间(周转时间/估计运行时间)
PCB *next;//定义指向下一个进程的指针
};
#define MAX_NUM 15
int pronum;//定义进程数为pronum
float total;//记录所有进程的总时间
double weight;//记录所有进程的带权周转时间
PCB *create(PCB *head);//创建进程队列
void deal(PCB *head);//FCFS记录处理
void sort(PCB *head);//将进程按到达的先后顺序排列
void fcfsrun(PCB *head);//先来先服务算法
PCB *create(PCB *head)
{
PCB *p1,*p2;
p1=p2=new PCB;
head=p1;
cout<<"请输入进程数:";
cin>>pronum;
for(int i=0;i<pronum;i++)
{
p2=p1;
p1=new PCB;
p1->next=NULL;
cout<<"请依次输入第"<<i+1<<"个进程的信息(进程名、到达时间、估计运行时间):";
cin>>p1->name>>p1->ta>>p1->ts;
p1->tm=p1->ts;
p1->rn=1;
p2->next=p1;
}
return head;
}
void sort(PCB *head)//将进程按到达的先后顺序排列
{
PCB *p,*q,*r,*s;
if(head->next!=NULL)
{
p=head->next->next;
head->next->next=NULL;
}
while(p)
{
q=p;
p=p->next;
r=head;
s=head->next;
while(s&&s->ta<=q->ta)
{
r=s;
s=s->next;
}
r->next=q;
q->next=s;
}
}
void deal(PCB *head)//FCFS记录处理
{
sort(head);
PCB *p,*q;
q=head->next;
q->tb=q->ta;
q->to=q->tb+q->ts;
q->totalTime=q->to-q->ta;
q->weightTotalTime=q->totalTime/(double)q->ts;
total+=q->totalTime;
weight+=q->weightTotalTime;
p=q->next;
while(p!=NULL)
{
p->tb=q->to;
p->to=p->tb+p->ts;
p->totalTime=p->to-p->ta;
p->weightTotalTime=p->totalTime/(double)p->ts;
total+=p->totalTime;
weight+=p->weightTotalTime;
q=p;
p=p->next;
}
}
void fcfsrun(PCB *head)//先来先服务算法
{
deal(head);
PCB *p,*q,*s;
p=head->next;
cout<<"进程执行顺序为:";
while(p!=NULL)
{
cout<<"--"<<p->name;
p=p->next;
}
cout<<endl;
cout<<"进程名 提交时间 开始时间 结束时间 周转时间 带权周转时间\n";
s=head->next;
while(s!=NULL)
{
cout<<setw(4)<<s->name<<setw(7)<<s->ta<<setw(10)<<s->tb<<setw(11)<<s->to<<setw(10)<<s->totalTime<<setw(10)<<s->weightTotalTime<<endl;
s=s->next;
}
cout<<endl;
cout<<" 平均周转时间:"<<total/(double)pronum<<endl;
cout<<"平均带权周转时间:"<<weight/(double)pronum<<endl;
cout<<"******************************************************"<<endl;
total=0;
weight=0;
}
void main()
{
cout<<"*E01114336-朱蓉蓉-先来先服务调度算法*"<<endl;
PCB *head=NULL;
head=create(head);
fcfsrun(head);
}
第二题:
//优先级调度算法
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定义进程控制块PCB */
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
void input() /* 建立进程控制块函数*/
{
int i,num;
system("cls"); /*清屏*/
printf("\n 请输入进程数: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='W';
p->link=NULL;
sort(); /* 调用sort函数*/
}
}
int space()
{
int l=0;
PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n 进程名\t 状态\t 优先数\t 需要运行时间\t 已经运行时间\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:\n"); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n **** 当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->super)--;
p->state='W';
sort(); /*调用sort函数*/
}
}
void main() /*主函数*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("-----------------------------------------------------");
printf("\n 现在是第%d次运行: \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任意键继续......\n");
}
printf("\n\n 进程已经完成.\n");
}
第三题:
按时间片轮法调度
#define N 10
#include<stdio.h>
#include<conio.h>
typedef struct pcb
{
char pname[N];
int runtime;
int arrivetime;
char state;
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 readydata();
int runprocess();
int readyprocess();
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 is %4d ; Process %s start,\n",\
(current+500)/1000,p3->pname);
fprintf(f,"Time is %4d ; Process %s start,\n",\
(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 is %4d;Process %s end.\n",(current+500)/1000,p1->pname);
fprintf(f,"Time is %4d;Process %s end.\n",(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;
printf("How many processes do you want to run:");
fprintf(f,"How many processes 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("Input NO.%3d process pname:",i+1);
fprintf(f,"Input NO.%3d process 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;
p1->next=new PCB;
p2=p1;
p1=p1->next;
}
delete p1;
p1=NULL;
p2->next=NULL;
}
/* 主函数 */
void main()
{
f=fopen("result.txt","w");
printf("\n time l=1000 time slice\n");
fprintf(f,"\n time l=1000 time slice\n");
current=0;
inputprocess();
readyprocess();
getch();
fclose(f);
}