操作系统 时间片轮转法 用到STL

包含文件:os2.in

5
0 1 2 3 4
6 2 5 9 8
4(时间片的大小)
#include<queue>
#include<iostream>
#include<cstdio>
#include<fstream>
#define MAX 100
using namespace std;
struct ps{
  bool Finished;
  char c;
  int ArrivalTime;
  int ServiceTime;
  int PServiceTime;
  int FinishTime;
  int WholeTime;
  double WeightWholeTime;
};

//int FinishTime[MAX];
//int WholeTime[MAX];

double AverageWT,AverageWWT;
//bool Finished[MAX];
void input(ps psr[],int &n,int &p)
{
  ifstream cin("os2.in");
  cin>>n;
  for(int i=0;i<n;i++)
      cin>>psr[i].ArrivalTime;
  for(int i=0;i<n;i++)
    {
  cin>>psr[i].ServiceTime;
  //  printf("ServiceTime=%d ",psr[i].ServiceTime);
  psr[i].PServiceTime=0;
  psr[i].c='A'+i;
    }
  for(int i=0;i<n;i++)
    psr[i].Finished=false;
  cin>>p;
}

void RR(ps psr[],int &n,int &p)
{
  //  printf("%d %d/n",n,p);
  queue<ps>q;
   int sumWhole=0;
   double sumWWhole=0.0;
  int sumTime=0;
  int timeTmp,i;
  for(i=0;i<n;i++)
    {
      sumTime+=psr[i].ServiceTime;
    }
  int time=0;
  i=0;
  int flag=1;
  while(time<sumTime)
    {
      if(i<=n)
      {
      while(time>=psr[i].ArrivalTime)
    {
      if(i==n)   break;
//              printf("Ariva=%d/n",psr[i].ArrivalTime);
      q.push(psr[i]);
          printf("ps %c arrival/n",psr[i].c);
      i++;
    }
      }


    if(!q.front().Finished && time>0 &&flag)
      {
      q.push(q.front());
             q.pop();
      flag=1;
      }
    //if(time>0)
    //

        timeTmp=q.front().ServiceTime-q.front().PServiceTime;
    printf("c=%c Ser=%d ",q.front().c,q.front().ServiceTime);
          printf("timetmp=%d/n",timeTmp);
      if(timeTmp>0 && timeTmp<p )
    {
      flag=0;
      q.front().PServiceTime+=timeTmp;
      time+=timeTmp;

      q.front().FinishTime=time;
      printf("1c=%c ",q.front().c);//
      printf("FinishTime1=%d ",time);
      //      printf("c=%c ",q.front().c);
      q.front().WholeTime=q.front().FinishTime-q.front().ArrivalTime;//ŒÆËãÖÜתʱŒä
      printf("WholeTime1=%d ",q.front().WholeTime);
      q.front().WeightWholeTime=(double)q.front().WholeTime/q.front().ServiceTime;
      printf("WeightWholeTime1=%.2f ",q.front().WeightWholeTime);
      sumWhole+=q.front().WholeTime;
      sumWWhole+=q.front().WeightWholeTime;
      printf("sumWhole1=%d ",sumWhole);
  printf("sumWWhole1=%.2f/n",sumWWhole);
    q.front().Finished=true;
    q.pop();
    flag=0;
    }
      else if(timeTmp==0)
    {

      q.front().FinishTime=time;
      //      q.front().PServiceTime += p;
      q.front().WholeTime=q.front().FinishTime-q.front().ArrivalTime;
      printf("$%d ",q.front().FinishTime);
      printf("Arivaltime=%d ",q.front().ArrivalTime);
      printf("Servicetiem=%d ",q.front().ServiceTime);
      printf("c0=%c ",q.front().c);
      printf("  #%d /n",q.front().ArrivalTime);//ÕâÒÑŸ­ÊÇCµÄµœŽïʱŒäÁË¡£

      printf("FinishTime0=%d ",q.front().FinishTime);//=12?
      printf("WholeTime0=%d ",q.front().WholeTime);

      q.front().WeightWholeTime=(double)q.front().WholeTime/q.front().ServiceTime;
      printf("WeightWholeTime0=%.2f ",q.front().WeightWholeTime);
      sumWhole+=q.front().WholeTime;
      sumWWhole+=q.front().WeightWholeTime;
      printf("sumWhole1=%d ",sumWhole);
  printf("sumWWhole0=%.2f/n",sumWWhole);
    q.front().Finished=true;
           q.pop();
    flag=0;
    //    q.front().PServiceTime+=p;
    }
      else
    {
      flag=1;
      time+=p;
            printf("time=%d/n/n ",time);
      q.front().PServiceTime+=p;
      if(q.front().PServiceTime==q.front().ServiceTime)
        q.front().Finished=true;
      //      printf("F=%d ",q.front().FinishTime);
      //      printf("c=%c ",q.front().c);
      //      printf("ps=%d ",q.front().PServiceTime);
    }
      //      printf("ÐòÁÐΪ£º%c/n",q.front().c);

    }
  printf("sumWWhole=%.2f/n",sumWWhole);
  AverageWT=(double)(sumWhole/n);
  printf("AverageWT=%.2f/n",AverageWT);
  AverageWWT=(double)(sumWWhole/n);
  printf("AvageWWT=%.2f/n",AverageWWT);
}

int main()
{
  ifstream cin("os2.in");
  ps psr[MAX];
  int n,p;
  input(psr,n,p);
  RR(psr,n,p);
  return 0;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭