#include<stdio.h>
#include<stdlib.h>
#define true 1
#define false 0
//程序中进程可用PCB表示,其类型描述如下
struct PCB_type
{
char name ; //进程名
int state ; //进程状态
// 2——表示“执行”状态
// 1——表示“就绪”状态
// 0——表示“阻塞”状态
int cpu_time ; //运行需要的CPU时间(需运行的时间片个数)
};
//设置三个队列,队列类型描述如下
struct QueueNode
{
struct PCB_type PCB;
struct QueueNode *next;
};
struct Queue
{
struct QueueNode *head, *tail;
};
//设三条队列指针:
struct Queue *rq, *bq, *runq; //ready队列、blocked队列、run队列
//创建队列
void createq(struct Queue **queue)
{
int i;
LinkQueueNode *NewNode;
pcb *pcbNode;
NewNode = (LinkQueueNode * )malloc(sizeof(LinkQueueNode));
pcbNode = (pcb * )malloc(sizeof(pcb));
for(i=0;i<ready;i++)
NewNode = (LinkQueueNode * )malloc(sizeof(LinkQueueNode));
pcbNode = (pcb * )malloc(sizeof(pcb));
for(i=0;i<blocked;i++)
{
printf("PCB.name:");
scanf("%c",&pcbNode->name);
printf("PCB.cpu_time:");
scanf("%d",&pcbNode->cpu_time);
printf("PCB.state:");
scanf("%d",&pcbNode->state);
copy(&NewNode->PCB,pcbNode);
enterq(&bq, *NewNode);
printf("\n");
getchar();
}
printf("\n");
display(rq);
display(bq);
}
//x结点入队列q
int enterq(struct Queue *q, struct QueueNode x)
{
struct QueueNode * NewNode;
NewNode=(struct QueueNode*)malloc(sizeof(struct QueueNode));
if(NewNode!=NULL)
{
NewNode->PCB=x.PCB;
NewNode->next=NULL;
q->tail->next=NewNode;
q->tail=NewNode;
return(true);
}
return(false);
}
//删除队首结点
struct QueueNode deleteq(struct Queue *q,struct QueueNode x)
{
struct QueueNode * p;
if(q->head!=q->tail)
{
p=q->head->next;
q->head->next=p->next;
if(q->tail==p)
{
q->tail=q->head;
}
x.PCB=p->PCB;
free(p);
return x;
}
}
//模拟调度函数
void dispatch(struct Queue *rq,struct Queue *bq,struct Queue *runq,int counter)
{
int count=0;
struct QueueNode p;
while(rq->head!=rq->tail||bq->head!=bq->tail)
{
if(rq->head!=rq->tail)
{
p=deleteq(rq,p);
enterq(runq,p);
runq->head->next->PCB.state=2;
printf("process:%c\n",runq->head->next->PCB.name);
runq->head->next->PCB.cpu_time--;
if(runq->head->next->PCB.cpu_time>0)
{
p=deleteq(runq,p);
p.PCB.state=1;
enterq(rq,p);
}
else
{
p=deleteq(runq,p);
}
}
count++;
if(count==counter&&bq->head!=bq->tail)
{
p=deleteq(bq,p);
p.PCB.state=1;
enterq(rq,p);
count=0;
}
}
}
//主函数
int main()
{
{
struct QueueNode p1,p2,p3,p4,p5;
int ready=2,blocked=3,counter=5;
int i;
createq(&rq);
createq(&bq);
createq(&runq);
printf("\n");
printf("\n");
enterq(rq,bp);
dispatch(rq,bq,runq,counter);
printf("调度结束!");}
}
有相关需求会好些,这种对照转代码,有点不好转。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632