进程调度------最高优先级算法模拟

本程序是操作系统的一个实验,程序本身就是一个数据结构的队列问题,

比较简单,就不进行说明了。 

#include <iostream>
using namespace std;
#define error 0
#define ok 1
#define NULL 0
enum process_state{W,R,F};   //状态枚举类型(等待,运行,就绪)

typedef struct PCBNode              //PCB结构体
{
 char name;   //名字
 int priority;  //权限
 int arrive;  
 int demand;
 int use;
 process_state state; //状态
}PCBNode,*pcb;

InitPcb(pcb &L)                    //初始化PCB指针
{
 L=(PCBNode*)malloc(sizeof(PCBNode));
 if(!L)
 return error;
 return ok;
}

typedef struct PNode             //PCB链表结点结构体
{
    pcb process;
 struct PNode *next;
}PNode;

typedef struct                  //带头结点的PCB链表指针
{
PNode* head;
}pcblist;
               
InitPcbList(pcblist &L)         //初始化PCB链表
{
 L.head=(PNode*)malloc(sizeof(PNode));
 if (L.head)
  L.head->next=NULL;
 else
  return error;
 return ok;
}

int pcblistinsert(pcblist &L,pcb e)         //PCB链表中插入新的结点
{
 PNode *p,*s;
 p=L.head;
 while (p->next&&e->priority < p->next->process->priority)
 {
    p=p->next;
 }
 s=(PNode*)malloc(sizeof(PCBNode));
 s->process=e;
 s->next=p->next;
 p->next=s;
 return ok;
}

int main()
{
 pcblist L;
 InitPcbList(L);
 pcb x;
 x=(pcb)malloc(sizeof(PCBNode));
 char c,d;
 int i=1,j;
 PNode* h;
 pcb P[5];
 for (j=0;j<5;j++)
      InitPcb(P[j]);
 for (j=0;j<5;j++)
{
     cout<<"进程名:";cin>>P[j]->name;
     cout<<"优先级:";cin>>P[j]->priority;
     cout<<"到达时间:";cin>>P[j]->arrive;
     cout<<"需要运行时间:";cin>>P[j]->demand;
     P[j]->use=0;
     P[j]->state=W;
    /* cout<<"use:";cin>>P[j]->use;
     cout<<"state:";cin>>(int&)P[j]->state;*/       //枚举类型的输入
     cout<<endl;  
}

while (P[0]->state!=F||P[1]->state!=F||P[2]->state!=F||P[3]->state!=F||P[4]->state!=F)
{
     h=L.head;
     c='a';
     for (j=0;j<5;j++)
    if (P[j]->arrive==i)
      pcblistinsert(L,P[j]);
   if(h->next==NULL)
    {
        i++;
        continue;
   }
     x=h->next->process;
     x->state=R;
     cout<<"~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
     cout<<"name    priority     arrive     demand     used     state "<<endl;
     for (j=0;j<5;j++)
     {   
      switch (P[j]->state)
      {
      case W:d='W';break;
      case R:d='R';break;
      case F:d='F';break;
      }
      cout<<P[j]->name<<"           "<<P[j]->priority<<"             "<<P[j]->arrive<<"           "<<P[j]->demand<<"         "<<P[j]->use<<"         "<<d<<endl;
     }
     x->priority--;
     x->use++;
     if(x->use==x->demand)
  x->state=F;
     else x->state=W;
  L.head=L.head->next;
     if(x->state!=F)
     {
  pcblistinsert(L,x);
     }
     i++;                //时间片加1
     cout<<"按任意键继续"<<endl;
     c=getchar();        //控制时间片间隔显示
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值