C语言使用free报错
在删除线程操作中一使用free就会引起中断,怎么回事,百思不得其解啊?
#include
#include
typedef struct node{
char name[5];//进程的名字
int needTime;//进程所需要的时间
int priority;//优先权
int roundTime;//进程轮流状态一共所需占用时间
int occupiedTime;//已经占用cpu的时间
struct node *next;//下一个进程节点
}PCB;
typedef struct {
PCB *head;
PCB *tail;
}LinkedQueue;
PCB pcbs[10];//创建线程的数组
int count;//线程的个数
LinkedQueue *ready;//就绪队列
void addQueue();
void priorityCreate(n);
/*对队列排序*/
void sort() {
for (int i = 0; i
if (i == 0) {
pcbs[i] = *ready->head;
}
else {
pcbs[i] = *pcbs[i - 1].next;
}
}
for (int i = 0; i
for (int j = count - 1; j > i; j--) {
if (pcbs[j].priority > pcbs[j - 1].priority) {
PCB temp;
temp = pcbs[j];
pcbs[j] = pcbs[j-1];
pcbs[j - 1] = temp;
}
}
}
addQueue();//对拍好序的数组加入到队列
}
void initReady() {
ready = (LinkedQueue *)malloc(sizeof(LinkedQueue));
ready->head = ready->tail = NULL;
}
/*把线程数组加入到队列中*/
void addQueue() {
ready->head = ready->tail = NULL;
for (int i = 0; i
if (i == 0) {
ready->head = &pcbs[i];
}
else if(i==count-1) {
ready->tail = &pcbs[count-1];
}
if (i
pcbs[i].next = &pcbs[i+1];
}
}
}
/*入队操作*/
void insert(PCB *pcb) {
pcb->next = NULL;
ready->tail->next = pcb;
ready->tail = pcb;
}
/*删除线程操作*/
void delete() {
PCB *pb;
pb = ready->head;
ready->head = pb->next;
//free(pb);
count--;
if (ready->head == NULL) {
ready->tail = ready->head;
}
}
//运行
void priorityRun(int n) {
priorityCreate(n);
initReady();
addQueue();
sort();
while (count != 0) {
PCB *pcb = ready->head;//取出队列头部的
pcb->needTime--;
pcb->priority = pcb->priority - 3;
printf("队列%s 进程剩余需要的时间为%d优先级剩余为%d \n", pcb->name, pcb->needTime, pcb->priority);
if (pcb->needTime == 0) {
delete();
}
else {
sort();
}
}
}
/*按照优先权法创建线程*/
void priorityCreate(int n) {
char name[5];
PCB *p;
printf("请输入进程每个进程的名字:\n");
for (int i = 0; i
gets_s(name,5);
p = (PCB *)malloc(sizeof(PCB));
strcpy(p->name, name);
p->priority = rand() % 10 + 10;
p->needTime = rand() % 5 + 5;
p->next = NULL;
pcbs[i] = *p;
count++;
}
}
int main()
{
priorityRun(5);
return 0;
}
------解决思路----------------------
/*对队列排序*/
void sort() {
//何用?
//for (int i = 0; i
//if (i == 0) {
//pcbs[i] = *ready->head;
//}
//else {
//pcbs[i] = *pcbs[i - 1].next; //
//}
//}
for (int i = 0; i
for (int j = count - 1; j > i; j--) {
if (pcbs[j].priority > pcbs[j - 1].priority) {
PCB temp;
temp = pcbs[j];
pcbs[j] = pcbs[j - 1];
pcbs[j - 1] = temp;
}
}
}
addQueue();//对拍好序的数组加入到队列
}
------解决思路----------------------
/*把线程数组加入到队列中*/
void addQueue() {
ready->head = ready->tail = NULL;
for (int i = 0; i
if (i == 0) {
ready->head = &pcbs[i];
pcbs[0].next = &pcbs[1]; //加一句
}
else if (i == count - 1) {
ready->tail = &pcbs[count - 1];
}
if (i
pcbs[i].next = &pcbs[i + 1];
}
}
}
------解决思路----------------------
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
仅供参考://带表头结点的单向链表
#include
#include
#include
#include
struct NODE {
int data;
struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
srand(time(NULL));
//填写头节点数据
H.data=-1;
H.next=NULL;
head=&H;
//创建10个节点的单链表
p=head;
for (i=0;i<10;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
p->next=q;
p=q;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//将值为5的结点插入到单链表的第k个结点前
k=3;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=5;
q->next=p->next;
p->next=q;
break;
}
p=p->next;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//删除第k个节点
k=5;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=p->next;
if (q) {
p->next=q->next;
free(q);
}
break;
}
p=p->next;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//从小到大排序
for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
if (p->next->data > q->next->data) {
//交换data
// printf("swap %02d %02d\n",p->next->data,q->next->data);
// t=p->next->data;p->next->data=q->next->data;q->next->data=t;
//或者
//交换next
// printf("swap %02d %02d\n",p->next->data,q->next->data);
s1=p->next;
s2=p->next->next;
s3=q->next;
s4=q->next->next;
if (s2!=s3) {
p->next=s3;
s3->next=s2;
q->next=s1;
s1->next=s4;
} else {
p->next=s3;
s3->next=s1;
q=s3;
s1->next=s4;
}
//输出整个单链表
// s=head->next;
// while (1) {
// if (NULL==s) {
// printf("\n");
// break;
// }
// printf("%02d->",s->data);
// s=s->next;
// }
// getchar();
}
}
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
m=4;
n=6;
k=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
k++;
if (m+1==k) {
q=p;
}
s=p;
p=p->next;
}
s1=head->next;
head->next=q->next;
s->next=s1;
q->next=NULL;
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//释放所有节点
p=head->next;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//18->94->58->17->27->20->43->57->75->78->
//18->94->05->58->17->27->20->43->57->75->78->
//18->94->05->58->27->20->43->57->75->78->
//05->18->20->27->43->57->58->75->78->94->
//43->57->58->75->78->94->05->18->20->27->
//