操作系统 时间片轮转法 用到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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值