求解,C语言,改错
怒码 2012-10-31 02:30:56 #include <stdio.h>
#include <stdlib.h>
typedef struct PCB_type
{
char name ; //进程名
int state ; //进程状态
// 2--表示"执行"状态
// 1--表示"就绪"状态
// 0--表示"阻塞"状态
int cpu_time ; //运行需要的CPU时间(需运行的时间片个数)
}PCB_type;
typedef struct QueueNode
{
struct PCB_type PCB;
struct QueueNode *next;
}QueueNode;
typedef struct Queue
{
struct QueueNode *head, *tail;
}Queue;
struct Queue rq,bq,runq; //ready队列、blocked队列、run队列
void createq(struct Queue *q) //创建队列
{
q->head=(QueueNode*) malloc(sizeof(QueueNode));
q->tail=q->head;
q->head->next=NULL;
}
void enterq(struct Queue *q, struct QueueNode *x) //x结点入队列q
{
x->next=NULL;
q->tail->next=x;
q->tail=x;
}
struct QueueNode* deleteq(struct Queue *q) //删除队首结点
{
QueueNode *p;
if(q->head==q->tail)return NULL;
p=q->head->next;
q->head->next=p->next;
if(q->tail==p)q->tail=q->head;
return p;
}
void dispatch() //模拟调度函数
{
int count=0;
int counter=5;//唤醒处于阻塞队列队首进程的时间片
QueueNode *p,*m;
while((rq.head!=rq.tail)||(bq.head!=bq.tail))
{
if(rq.head!=rq.tail)
{
p=deleteq(&rq);
enterq(&runq,p);
p->PCB.state=2;
printf("%c",p->PCB.name);
p->PCB.cpu_time--;
if(p->PCB.cpu_time>0)
{
p->PCB.state=1;
enterq(&rq,p);
runq.head =runq.tail;
}
else
{
runq.head =runq.tail;
free(p);
}
}
count++;
if(count==counter)
{
if(bq.head!=bq.tail)
{
m=deleteq(&bq);
m->PCB.state=1;
enterq(&rq,m);
count=0;
}
}
}
}
void main() //主函数,ready=2,blocked=3
{
QueueNode r[2];
QueueNode b[3];
createq(&rq);
createq(&bq);
createq(&runq);
for(int i=0;i<2;i++)
{
printf("请输入就绪队列成员:");
scanf("%c,%d,%d",&r[i].PCB.name,&r[i].PCB.cpu_time,&r[i].PCB.state);
getchar();
enterq(&rq,&r[i]);
}
for(int j=0;j<3;j++)
{
printf("请输入阻塞队列成员:");
scanf("%c,%d,%d",&b[j].PCB.name,&b[j].PCB.cpu_time,&b[j].PCB.state);
getchar();
enterq(&bq,&b[j]);
}
dispatch();
}