实验3

实验3、第三次实验

一、        实验目的

用高级语言完成一个进程调度程序,以加深对进程的概念及进程调度算法的理解。

二、        实验内容和要求

  二、实验要求

设计一个有 N个进程并发执行的进程调度模拟程序。

1.模拟进程数据的生成

允许用户指定作业的个数(2-24),默认值为5。

允许用户选择输入每个进程的到达时间,所需运行时间,进程的运行时间以时间片为单位。

2. 模拟调度程序的功能

2.1 按照模拟数据的到达时间和所需运行时间,能分别执行以下调度算法。

FCFS

SJ

HRRN

RR

 

2.2 显示每种算法下各进程的调度执行顺序。

 

2.3计算各进程的开始执行时间,各作业的完成时间,周转时间和带权周转时间(周转系数)。

 

2.4模拟数据结果分析:对同一组模拟数据,比较各算法的平均周转时间,周转系数。

实验方法、步骤及结果测试

 代码:

#include<stdio.h>

#include<stdlib.h>

#define N 100

 

 

typedef struct process

{

       int num;

       int time;

       int start;

       int end;

       int begin;

       int freetime;

       int arrivetime;

       int finish;

       int turn;

       double reaction;

       double average;

}P;

 

P a[N],b;

int wordtime=0;

 

void fcfs(int i,int t,int n){

       a[0].finish=a[0].arrivetime+a[0].freetime;//给予a[0]初值

       wordtime=a[0].finish;

    a[0].turn=a[0].time;

  a[0].average=(float)a[0].turn/a[0].time;

       for(i=1;i<n;i++)//执行

       {

       if(a[i].arrivetime<a[i-1].finish)

       {

       a[i].finish=a[i-1].finish+a[i].freetime;

       a[i].turn=a[i].finish-a[i].arrivetime;

       }

       else

       {

              a[i].finish=a[i].arrivetime+a[i].freetime;

              a[i].turn=a[i].time;

       }

 

   a[i].average=(float)a[i].turn/a[i].time;

       wordtime=a[i].finish;

       }

 

}

 

void hrrn(int i,int j,int t,int n){

       a[0].finish=a[0].arrivetime+a[0].freetime;//给予a[0]初值

       wordtime=a[0].finish;

    a[0].turn=a[0].time;

       a[0].average=(float)a[0].turn/a[0].time;

       for(i=1;i<n;i++)//执行

       {

              for(j=i+1;j<n;j++)

              {

                     if(a[j].arrivetime<=a[i-1].finish)

                     {

                            for(t=i;t<=j;t++)

                            {  

                                   a[t].reaction=(float)(a[t-1].finish-a[t].arrivetime+a[t].time)/a[t].time;

                                   if(a[t].reaction<a[j].reaction)

                                   {

                                          b=a[j];

                                          a[j]=a[t];

                                          a[t]=b;

                                   }

                            }

                     }

              }     

              if(a[i].arrivetime<a[i-1].finish)

              {

                     a[i].finish=a[i-1].finish+a[i].freetime;

                     a[i].turn=a[i].finish-a[i].arrivetime;

              }

              else

              {

                     a[i].finish=a[i].arrivetime+a[i].freetime;

                     a[i].turn=a[i].time;

              }

              a[i].average=(float)a[i].turn/a[i].time;

              wordtime=a[i].finish;

       }

      

}

 

int decide(n)//判断算法

{

       int i,t=0;

       for(i=0;i<n;i++)

       {

              if(a[i].end==1)

              {

                     t=1;

      

              }

              else

              {

                     t=0; 

                     break;

       }

       }

if(t==0)

              return 0;

else

       return 1;

}

 

void rr(int i,int j,int n,int x){

       while(decide(n)==0)

       {            

              //j=1;

              for(i=0;i<n;i++)

              {

                     if(a[i].end==1)

                     {

                           

                     }

                     else if(a[i].end==0)

                     {

                            if(a[i].arrivetime<=wordtime)

                            {

                                  

                                   if(a[i].freetime>0)

                                   {

                                          a[i].freetime--;

                                          wordtime++;

                                   }

                                    if(a[i].freetime==0)

                                   {

                                          a[i].end=1;

                                          a[i].finish=wordtime;

                                         

                                   }

                                  

                            }

                            else {

                                          for(j=i;j<n;j++)

                                          {

                                         

                                                

                                                 if(a[j].arrivetime<=wordtime&&a[j].end==0)

                                                 {

                                                       

                                                        x=0;

                                                        i=j-1;

                                                        break;

                                                 }

                                                 else if(j==i-1)

                                                 {

                                                 x=1;

                            break;

                                                 }

                                                 else                                          

                                                 x=1;

                          if(j==n-1&&i!=0)                                               

                                            j=-1;

                                          }     

                     if(x==1)

                            wordtime++;                                                             

                            }

                     }

       }            

       }

       for(i=0;i<n;i++)

       {

              a[i].turn=a[i].finish-a[i].arrivetime;

              a[i].average=(float)a[i].turn/a[i].time;

       }

}

 

void sjf(int i,int j,int t,int n){

       a[0].finish=a[0].arrivetime+a[0].freetime;//给予a[0]初值

       wordtime=a[0].finish;

    a[0].turn=a[0].time;

       a[0].average=(float)a[0].turn/a[0].time;

       for(i=1;i<n;i++)//执行

       {

              for(j=i+1;j<n;j++)

              {

                     if(a[j].arrivetime<=a[i-1].finish)

                     {

                            for(t=i;t<=j;t++)

                            {

                                   if(a[t].time>a[j].time)

                                   {

                                          b=a[j];

                                          a[j]=a[t];

                                          a[t]=b;

                                   }

                            }

                     }

              }     

              if(a[i].arrivetime<a[i-1].finish)

              {

                     a[i].finish=a[i-1].finish+a[i].freetime;

                     a[i].turn=a[i].finish-a[i].arrivetime;

              }

              else

              {

                     a[i].finish=a[i].arrivetime+a[i].freetime;

                     a[i].turn=a[i].time;

              }

              a[i].average=(float)a[i].turn/a[i].time;

              wordtime=a[i].finish;

       }

      

}

 

void main()

{

       int n=5,i,t,sum=0,num=2,j,x=1,y;

       double average=0,turn=0;

    printf("选择你需要的算法 1.fcfs  2.sjf  3.hrrn  4.rr\n");

       scanf("%d",&y);

       printf("是否输入进程个数 1.||2.默认个数为5\n");

       scanf("%d",&num);

       if(num==2)

       {

             

       }

       else

       {

              printf("请输入进程个数1<n<25 \n");

              scanf("%d",&n);

       }

       for(i=0;i<n;i++){

              printf("请输入第%d个进程时间n>0 \n",i+1);

              scanf("%d",&a[i].time);

              printf("请输入第%d个进程到达时间n>0 \n",i+1);

              scanf("%d",&a[i].arrivetime);

              a[i].start=a[i].arrivetime;

              a[i].freetime=a[i].time;//剩余时间赋值

              a[i].num=i+1;

              a[i].start=a[i].end=0;

       }//输入数据

      

       for(i=0;i<n;i++)

       {

              for(t=i+1;t<n;t++)

              {

                     if(a[t].arrivetime<a[i].arrivetime)

                     {

                            b=a[i];

                            a[i]=a[t];

                            a[t]=b;

                     }

                     else if(a[t].arrivetime==a[i].arrivetime)

                     {

                            if(a[i].time>a[t].time)

                            {

                                   b=a[i];

                                   a[i]=a[t];

                                   a[t]=b;

                            }

                     }

              }

       }//排序

      

 

      

       switch(y)

       {

       case 1:

fcfs(i,t,n);

break;

       case 2:

sjf(i,j,t,n);

              break;

       case 3:

hrrn(i,j,t,n);

              break;

       case 4:

rr(i,j,n,x);

              break;

       }

       printf("进程号,进程完成时间,进程周转时间,带权周转时间\n");

       for(i=0;i<n;i++)

              printf("%3d,%13d,%13d,     %f\n",a[i].num,a[i].finish,a[i].turn,a[i].average);

      

       for(i=0;i<n;i++)

       {

              turn=turn+(float)a[i].turn;

              average=average+(float)a[i].turn/a[i].time;

       }

       turn=turn/n;

       average=average/n;

       printf("平均周转时间,平均带权周转时间\n");

       printf("%13f         ,%13f\n",turn,average);

      

}

 

三、        实验总结

难在算法过程,有多种不同情况需要出现,写算法时应该考虑全面防止出错

 

posted @ 2016-05-23 09:08  handmooc  阅读(190)  评论(0编辑  收藏  举报