操作

来源:互联网 发布:linux踩内存排查 编辑:程序博客网 时间:2024/04/20 11:56

试验三

#include "stdio.h"

#define MAX 300

struct STAT{

 char name;    /*进程的名称*/

 int num;      /*num表示第几次运行该任务*/

 int period;   /*period为该任务的周期时间*/

 int run;      /*run表示该任务的运行时间*/

 int remain;   /*remain表示该任务的剩余时间*/

 int begin;    /*begin表示该任务可以开始运行的时间*/

 int end;     /*表示该任务的最早截止时间*/

};

main()

{static struct STAT a={'A',1,20,10,0,0},b={'B',1,50,15,0,0},c={'C',1,60,10,0,40};

  struct STAT *p;

  int i,j,k,k1,t=0,t1,x;

  FILE *fp;

  if((fp=fopen("os3data.txt","w"))==NULL) { printf("cannot creat this file/n");exit(0);}

  a.end=a.period-a.run;b.end=b.period-b.run;c.end=c.begin+10;

  while (t<MAX)     /*t用来作时间计数器,t1是任务可以开始运行的时间,此时t1-tCPU空闲时间*/

  { i=j=MAX;         /*i是第一个最早截止时间,j是第二个最早截止时间*/

  /*以下先检查A任务 */

    if (a.begin<=t) { i=a.end;j=a.end+a.period;p=&a;t1=MAX;} else {j=a.end;t1=a.begin;}

/*检查B任务*/

    if (b.begin<=t && i!=MAX)  { if (b.end<i) {j=i;i=b.end;p=&b;} else j=(b.end<j)?b.end:j;}

    else if (b.begin<=t && i==MAX){i=b.end;j=(j<b.end+b.period)?j:b.end+b.period; p=&b;t1=MAX;}

    else if (b.begin>t && i!=MAX) j=(j<b.end)?j:b.end;

    else if (b.begin>t && i==MAX) {j=(j<b.end)?j:b.end;t1=(t1<b.begin)?t1:b.begin;}

 /*检查C任务*/

    if (c.begin<=t && i!=MAX)  { if (c.end<i) {j=i;i=c.end;p=&c;} else j=(c.end<j)?c.end:j;}

    else if (c.begin<=t && i==MAX){i=c.end;j=(j<c.end+c.period)?j:c.end+c.period; p=&c;t1=MAX;}

    else if (c.begin>t && i!=MAX) j=(j<c.end)?j:c.end;

    else if (c.begin>t && i==MAX) {j=(j<c.end)?j:c.end;t1=(t1<c.begin)?t1:c.begin;}

/*判断当前是否有任务执行,i==MAX 表示没有任务*/

    if (i==MAX) {fprintf(fp,"The CPU free at time %5d --%5d/n",t,t1);t=t1;}

    else

       { if(p->remain==0) k1=p->run;else k1=p->remain;    /*k1为当前任务需要执行的时间*/

      if (t+k1>j) k=j-t;else k=k1;                     /*k是当前任务可以运行的时间*/

      p->remain=k1-k;

      if (p->remain==0) {p->begin+=p->period;p->end+=p->period;} else {p->begin=t+k;p->end+=k;}

      fprintf(fp,"the task %3c%2d  at time %5d --%5d  run/n",p->name,p->num,t,t+k);

      t+=k;p->num++;

      if (c.num==3) c.begin=c.end=MAX+2;

        }

  }

  fclose(fp);

}

 

原创粉丝点击