malloc申请失败
CPP文件#include <stdio.h>
#include "ProcessScheduling.h"
void main()
{
PCBList FCFS_L;
PCBList SJF_L;
PCBNode p[M],q[M];
int num;
InitPCBList(FCFS_L);
InitPCBList(SJF_L);
printf("进程数:");
scanf("%d",&num);
Input(p,num);
FCFS(FCFS_L,p,num);
SJF(SJF_L,p,num);
printf("下列为 FCFS 算法结果:\n");
print(FCFS_L,num);
printf("下列为 SJF 算法结果:\n");
print(SJF_L,num);
}
头文件
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define M 50
typedef struct PCBNode
{
char name[10];
double ArriveTime;
double ServiceTime;
double StartTime;
double EndTime;
double TurnaroundTime;
double TurnaroundTime_R;
struct PCBNode *next;
}PCBNode;
typedef struct PCBList
{
PCBNode *head,*tail;
int length;
}PCBList;
/*
void InitPCBList(PCBList &L);
void FCFS(PCBList &l,PCBNode &process[],int num);
void SJF(PCBNode &process[]);
*/
void InitPCBList(PCBList &L)
{
L.head=NULL;
L.tail=NULL;
L.length=0;
}
void Input(PCBNode p[],int num)
{
int i=0;
printf("输入进程名、到达时间、服务时间\n");
for (i=0;i<num;i++)
{
scanf("%s%lf%lf",p[i].name,&p[i].ArriveTime,&p[i].ServiceTime);
}
}
void FCFS(PCBList &l,PCBNode s[],int num)
{
int i;
PCBNode *q,*qre,*p;
q=(PCBNode *)malloc(sizeof(PCBNode));
qre=(PCBNode *)malloc(sizeof(PCBNode));
p=(PCBNode *)malloc(sizeof(PCBNode));
for(i=0;i<num;i++)
{
q=l.head;
qre=NULL;
strcpy(p->name,s[i].name);
p->ArriveTime=s[i].ArriveTime;
p->ServiceTime=s[i].ServiceTime;
while (q&&q->ArriveTime<p->ArriveTime)
{
qre=q;
q=q->next;
}
if (!qre)
{
p->next=l.head;
l.head=p;
}
else
{
p->next=q;
qre->next=p;
}
p++;
}
l.head->StartTime=l.head->ArriveTime;
l.head->EndTime=l.head->StartTime+l.head->ServiceTime;
l.head->TurnaroundTime=l.head->EndTime-l.head->ArriveTime;
l.head->TurnaroundTime_R=l.head->TurnaroundTime/l.head->ServiceTime;
q=l.head->next;
qre=l.head;
while(q)
{
q->StartTime=qre->EndTime;
q->EndTime=q->StartTime+q->ServiceTime;
q->TurnaroundTime=q->EndTime-q->ArriveTime;
q->TurnaroundTime_R=q->TurnaroundTime/q->ServiceTime;
qre=q;
q=q->next;
}
}
void SJF(PCBList &l,PCBNode s[],int num)
{
int i,j=0;
PCBNode *q1,*qre,*p2,*q;;
q=(PCBNode *)malloc(sizeof(PCBNode));
qre=(PCBNode *)malloc(sizeof(PCBNode));
p2=(PCBNode *)malloc(sizeof(PCBNode));
for (i=0;i<num;i++)
{
if (s[i].ArriveTime<s[j].ArriveTime)
{
j=i;
}
}
for(i=0;i<num;i++)
{
q=l.head;
qre=NULL;
strcpy(p2->name,s[i].name);
p2->ArriveTime=s[i].ArriveTime;
p2->ServiceTime=s[i].ServiceTime;
if (i==j)
{
p2->next=l.head;
l.head=p2;
p2++;
continue;
}
while (q&&q->ArriveTime<p2->ArriveTime)
{
qre=q;
q=q->next;
}
if (!qre)
{
p2->next=l.head;
l.head=p2;
}
else
{
p2->next=q;
qre->next=p2;
}
p2++;
}
l.head->StartTime=l.head->ArriveTime;
l.head->EndTime=l.head->StartTime+l.head->ServiceTime;
l.head->TurnaroundTime=l.head->EndTime-l.head->ArriveTime;
l.head->TurnaroundTime_R=l.head->TurnaroundTime/l.head->ServiceTime;
q=l.head->next;
qre=l.head;
while(q)
{
q->StartTime=qre->EndTime;
q->EndTime=q->StartTime+q->ServiceTime;
q->TurnaroundTime=q->EndTime-q->ArriveTime;
q->TurnaroundTime_R=q->TurnaroundTime/q->ServiceTime;
qre=q;
q=q->next;
}
}
void print(PCBList &l,int m)
{
PCBNode *q=l.head;
int i;
for(i=0;i<m;i++)
{
printf("进程名 到达时间 服务时间 开始执行时间 完成时间 周转时间 带权周转时间\n");
printf(" %s %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf\n",q->name,q->ArriveTime,q->ServiceTime,q->StartTime,q->EndTime,q->TurnaroundTime,q->TurnaroundTime_R);
q=q->next;
}
}