ljylyx 发表于 2022-6-21 17:00:37

孩子期末救救

#include <iostream>
#include<ctime>
using namespace std;
int Num; //定义总进程数
int Time = 0;   //定义系统时间
int slice = 1; //定义时间片长度
#define MAXSIZE 10   //定义就绪队列最大长度
#define false 0
#define true 1

typedef struct PCB
{
      char name;
      int arrivetime;
      int runtime;
      char state;//
}PCB;
typedef struct
{
      PCB* base;
      int front;
      int rear;
}LinkQueue;
void initqueue(LinkQueue L)
{
      if (!L.base)
                exit(1);
      L.front = L.rear = 0;
}
void init(PCB p[])
{
      for (int i = 0; i < Num; i++)
      {
                p.name = 'A' + i;
                p.arrivetime = rand() % 8 + 1;
                p.runtime = rand() % 5 + 1;
                p.state = 'R';
      }
}
void print(PCB p[])
{
      cout << "进程名\t" << "到达时间\t" << "估计运行时间\t" << "进程状态" << endl;
      for (int i = 0; i < Num; i++)
      {
                cout << p.name << "\t" << p.arrivetime << "\t\t" << p.runtime << "\t\t" << p.state << endl;
      }
}
void sort(PCB p[])//插入排序
{
      for (int i = 1; i < Num; i++)
      {
                PCB key = p;
                int j = i - 1;
                while ((j >= 0) && (key.arrivetime < p.arrivetime))
                {
                        p = p;
                        j--;
                }
                p = key;
      }
}
void show(LinkQueue& L)
{
      while (1)
      {
                if (L.front == L.rear)
                {
                        cout << "队列为空" << endl;
                        break;
                }
                else
                {
                        cout << "进程名" << L.base.name << "\t" << "到达时间" << L.base.arrivetime << "\t"
                              << "运行时间" << L.base.runtime << "\t" << "进程状态" << L.base.state << endl;
                        L.front = (L.front + 1) % MAXSIZE;
                }
      }
}
void time_slice(LinkQueue& L, PCB p[])
{
      int count = 0;
      int* record = new int;
      for (int i = 0; i < Num; i++)
      {
                record = false;
      }
      while (count != Num)
      {
                for (int i = 0; i < Num; i++)
                {
                        if (Time >= p.arrivetime && record == false)
                        {
                              L.base = p;
                              L.rear = (L.rear + 1) % MAXSIZE;
                              record = true;
                        }
                }
                if (L.front == L.rear)//队列为空
                {
                        cout << "*************************系统时间为" << Time << "************************" << endl;
                        cout << "就绪队列为空" << endl;
                        Time+=slice;
                }
                else
                {
                        if (L.base.runtime == slice)
                        {
                              cout << "*************************系统时间为" << Time << "************************" << endl;
                              cout << "当前运行进程是" << L.base.name << ",即将运行完成,估计运行时间由"
                                        << L.base.runtime <<"变为"<<L.base.runtime-1<< endl;
                              L.base.state = 'F';
                              Time += slice;
                              L.base.runtime--;
                              show(L);
                              L.front = (L.front + 1) % MAXSIZE;//出队
                              count++;
                  }
                        else if (L.base.runtime != slice)
                        {
                              cout << "*************************系统时间为" << Time << "************************" << endl;
                              cout << "当前运行进程是" << L.base.name <<",估计运行时间由"
                                        << L.base.runtime << "变为" << L.base.runtime - 1 << endl;
                              L.base.state = 'r';
                              Time += slice;
                              L.base.runtime--;
                              show(L);
                              L.base.state = 'R';
                              L.base = L.base;//队头元素放进队尾
                              L.front = (L.front + 1) % MAXSIZE;//队头加一
                              L.rear = (L.rear + 1) % MAXSIZE;//队尾加一
                        }
                }
      }
}
int main()
{
      LinkQueue L;
      srand(unsigned int(time(NULL)));
      Num = rand() % 3 + 5;
      PCB* p = new PCB;
      L.base = new PCB;
      initqueue(L);
      init(p);
      print(p);
      sort(p);
      print(p);
      time_slice(L, p);
      system("pause");
      return 0;
}
L.base = p;这一行出现问题,写入访问权限冲突
页: [1]
查看完整版本: 孩子期末救救