• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-27
  • 语言: 其他
  • 标签:

资源简介

进程调度模拟设计--先来先服务、强占式短进程优先算法

资源截图

代码片段和文件信息


#include
#include
using namespace std;
struct PCB
{
string name;//进程名
int ta;//进程到达时间
int ts;//进程估计运行的时间
int tb;//进程开始运行时间
int tm;//进程仍需运行的时间
int to;//进程完成的时间
int rn;//进程运行的次数
int totalTime;//周转时间
double weightTotalTime;//带权周转时间(周转时间/估计运行时间)
PCB *next;//定义指向下一个进程的指针
};

string pname[100];//保存进程调度队列
int pronum;//定义进程数为pronum
int total;//记录所有进程的总周转时间
double weight;//记录所有进程的带权周转时间

PCB *create(PCB *head);//创建进程队列
void del(PCB *p);//删除p的下一个节点
void sort(PCB *head);//将进程按到达的先后顺序排列
int getCount(PCB *headint time);//察看在time之前到达但未移动到运行队列的进程数量
void FCFS(PCB *head);//先来先服务算法
PCB *SJF(PCB *headint count);//在头节点后的count个节点中选择需时间最小的返回
void SJFrun(PCB *head);//强占式短进程优先算法

void main()
{
int choice;
cout<<“*进程调度模拟设计——先来先服务、强占式短进程优先算法*“< cout<<“***********1.先来先服务算法***************************“< cout<<“***********2.强占式短进程优先算法*********************“< cout<<“***********3 退出*************************************“<l1: cout<<“请输入您的选择:“<l2: cin>>choice;
PCB *head=NULL;
switch(choice)
{
case 1:head=create(head);FCFS(head);goto l1;
case 2:head=create(head);SJFrun(head);goto l1;
case 3:break;
default:cout<<“输入错误!\n请重新输入:“< }
}

PCB *create(PCB *head)
{
PCB *p1*p2;
p1=p2=new PCB;
head=p1;
cout<<“请输入进程数:“;
cin>>pronum;
for(int i=0;i {
p2=p1;
p1=new PCB;
p1->next=NULL;
cout<<“请依次输入第“< 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 del(PCB * p)//删除p的下一个节点
{
PCB *tmp;
tmp=p->next;
p->next=tmp->next;
free(tmp);
}

int getCount(PCB *headint time)//察看在time之前到达但未移动到运行队列的进程数量
{
int count=0;
PCB *s*t;
s=head;
t=s->next;
while(t!=NULL&&t->ta<=time)
{
   s=t;
   t=t->next; 
   count++;//count记录当前时刻到达的进程数
}
return count;
}

void FCFS(PCB *head)//先来先服务算法
{
int i=0j=0;
total=0;
weight=0;
sort(head);
int time=0count;
PCB *p*q;
while(head->next!=NULL)
{
count=getCount(headtime);
if(count==0)time++;
else
{
p=head;
q=p->next;
cout<<“******************************************************“< cout<<“进程名:“<name< pname[i]=q->name;
i++;
cout<<“到达时间:“<ta< q->tb=time;
cout<<“进程开始运行时间:“<tb< time=q->tb+q->ts;
cout<<“进程运行结束时间:“< q->totalTime=time-q->ta;
cout<<“周转时间:“<totalTime< total+=q->totalTime;
q->weightTotalTime=q->totalTime/(double)q->ts;
weight+=q->weightTotalTime;
cout<<“带权周转时间:“<weightTotalTime< cou

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6165  2010-01-25 21:32  fcfs&sjf.cpp

----------- ---------  ---------- -----  ----

                 6165                    1


评论

共有 条评论