⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 最高优先级程序源码.txt

📁 这是两个优先级调度算法的源码
💻 TXT
字号:
#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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -