请问这个程序有没有问题啊???
#include<stdio.h>#include<stdlib.h>
int n,m,t,use_cpu = 0,x = 0,unuse_cpu = 0;
struct PCB_type
{
char name[1];
int state;
int cpu_time;
};
struct QueueNode
{
struct PCB_type PCB;
struct QueueNode *next;
};
struct QueueNode *ready_head = NULL,
*ready_tail = NULL,
*blocked_head = NULL,
*blocked_tail = NULL;
void creatList1();
void creatList2();
void del(struct QueueNode* p);
void start_state();
void dispath(struct QueueNode* s1,struct QueueNode* s2,int x);
void calculate();
void start_state() //输入假设的数据
{
printf("请输入处于就绪状态进程的个数:");
scanf("%d",&n);
printf("请输入处于阻塞状态进程的个数:");
scanf("%d",&m);
printf("请输入时间片数:");
scanf("%d",&t);
}
void creatList1() //创建链表,记录所有进程的信息
{
int i;
struct QueueNode* head1 = (struct QueueNode*)malloc(sizeof(struct QueueNode));
struct QueueNode* p;//= (struct QueueNode*)malloc(sizeof(struct QueueNode));
struct QueueNode* s1 = (struct QueueNode*)malloc(sizeof(struct QueueNode));
p = head1;
printf("**********就绪进程的信息输入**********\n");
for(i=0;i<n;i++)
{
printf("请输入进程名:");
scanf("%s",s1->PCB.name);
printf("请输入进程状态(2-表示“执行”,1-表示“就绪”,0-表示“阻塞”):");
scanf("%d",&(s1->PCB.state));
printf("请输入此进程需要CPU时间片数:");
scanf("%d",&(s1->PCB.cpu_time));
s1->next = NULL;
p->next = s1;
p = p->next;
}
ready_head = head1;
ready_tail = p;
}
void creatList2()
{
int i;
struct QueueNode* head2=(struct QueueNode*)malloc(sizeof(struct QueueNode));
struct QueueNode* p;// = (struct QueueNode*)malloc(sizeof(struct QueueNode));
head2->next=NULL;
p = head2;
printf("**********阻塞进程的信息输入**********\n");
for(i=0;i<m;i++)
{
struct QueueNode* s2 = (struct QueueNode*)malloc(sizeof(struct QueueNode));
printf("请输入进程名:");
scanf("%s",s2->PCB.name);
printf("请输入进程状态(2-表示“执行”,1-表示“就绪”,0-表示“阻塞”):");
scanf("%d",&(s2->PCB.state));
printf("请输入此进程需要CPU时间片数:");
scanf("%d",&(s2->PCB.cpu_time));
p->next = s2;
p = p->next;
}
blocked_head = head2;
blocked_tail = p;
}
void del(struct QueueNode* p) //删除p节点
{
free(p);
}
void calculate()
{
double rate;
rate = use_cpu/(use_cpu + unuse_cpu)*100;
printf("CPU利用率为:%f%",rate);
}
void dispath(struct QueueNode* s1,struct QueueNode* s2,int x)
{
while(s1->next != NULL || s2->next != NULL)
{
struct QueueNode* p1 = (struct QueueNode*)malloc(sizeof(struct QueueNode));
if(s1->next != NULL)
{
p1 = s1->next;
//s1->PCB.state = 2;
printf("%s",&(p1->PCB.name));
use_cpu++;
p1->PCB.cpu_time--;
if(p1->PCB.cpu_time > 0)
{
s1->next = p1->next;
ready_tail = p1;
p1->next = NULL;
//ready_tail->next = s1;//
s1->next = s1->next->next;
//ready_tail = ready_tail->next;
}
else
{
s1->next = p1->next;
del(p1);
}
}
else
unuse_cpu++;
x++;
if(x == t)
{
// ready_tail->
ready_tail->next = blocked_head->next;
blocked_head->next = blocked_head->next->next;
ready_tail = ready_tail->next;
x = 0;
}
}
}
void main()
{
start_state();
creatList1();
creatList2();
dispath(ready_head,blocked_head,x);
calculate();
}