linux C 错误 :segmentation fault(core dumped)

pan__yy 2016-05-12 11:20:03

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include <unistd.h>
//#include <windows.h>
#define N 6
enum STATE{ready, run, finish}STATE;

// 待插入就绪队列的进程数据
int id[N] = { 0, 1, 2, 3, 4, 5 };
int priority[N] = { 9, 38, 17, 2, 7, 18 };
int cpuTime[N] = { 0, 0, 0, 0, 0, 0 };
int allTime[N] = { 3, 2, 3, 6, 1, 3 };

typedef struct PCB{
int pid;
int priority;
int cpuTime;
int allTime;
int state;
struct PCB *next;
}PCB;
typedef struct{
PCB *head;
PCB *tail;
PCB *maxPriority;
}linkQueue;

linkQueue readyQueue;

void initQueue(){
int maxm = -1;
int i;
PCB *head, *tail, *p;
readyQueue.head = readyQueue.tail = (PCB *)malloc(sizeof(PCB));
head->next=NULL;
for(i=0;i<N;i++){
p = (PCB *)malloc(sizeof(PCB));
p->pid = id[i];
p->priority = priority[i];
p->cpuTime = cpuTime[i];
p->allTime = allTime[i];
p->state = ready;
p->next = NULL;
readyQueue.tail->next = p;
readyQueue.tail = p;
if(priority[i] > maxm){
readyQueue.maxPriority = p;
maxm = priority[i];
}

}

return;
}

/* 查看正在运行进程的信息 */
void runningProcess(PCB *p){
printf("-------------------------------Running--------------------------------\n");
printf("Pid=%d\tPriority=%d\tCpuTime=%d\tAllTime=%d\tState=running\n", p->pid, p->priority, p->cpuTime, p->allTime);
printf("----------------------------------------------------------------------\n");
return;
}


/* 查看就绪队列的信息 */
void readyQueCheck(){
PCB *p = readyQueue.head->next;
// printf("\n%d\n", readyQueue.tail->pid);
printf("-------------------------------Ready----------------------------------\n");
if(p == NULL)
printf("Ready queue is empty!\n");
while(p != NULL){
printf("Pid=%d\tPriority=%d\tCpuTime=%d\tAllTime=%d\tState=ready\n", p->pid, p->priority, p->cpuTime, p->allTime);
p = p->next;
}
printf("----------------------------------------------------------------------\n");

}

void enQueue(PCB *p){
readyQueue.tail->next = p;
readyQueue.tail = p;
p->next = NULL;
return;
}

/* 让指定的进程出列 */
void popQueue(PCB *p){
if(readyQueue.head->next != NULL){
PCB *temp = readyQueue.head;
while(temp!=NULL){
if(temp->next == p){
temp->next = p->next;
p->next = NULL;
if(temp->next==NULL)
readyQueue.tail = temp;
return;
}
temp = temp->next;
}
printf("Process not found!\n");
} else{
printf("Queue is empty!\n");
}
return;
}

/* 更新就绪队列最大优先权进程 */
void updateMaxPriority(){
PCB *p;
int maxm = -1;
p = readyQueue.head->next;
while(p != NULL){
if(p->priority > maxm){
readyQueue.maxPriority = p;
maxm = p->priority;
}
p = p->next;
}
return;
}



void timeSlice(){
popQueue(readyQueue.maxPriority);
PCB *p = readyQueue.head->next;
runningProcess(readyQueue.maxPriority);
readyQueCheck();
readyQueue.maxPriority->priority = readyQueue.maxPriority->priority-3;
readyQueue.maxPriority->cpuTime++;
readyQueue.maxPriority->allTime--;
readyQueue.maxPriority->state = run;
while(p != NULL){
p->priority++;
p = p->next;
}
if(readyQueue.maxPriority->allTime != 0){
enQueue(readyQueue.maxPriority);
} else {
free(readyQueue.maxPriority);
}
updateMaxPriority();
}



/* 检查队列是否为空 */
int isEmpty(linkQueue q){
if(q.head->next == NULL)
return 1;
return 0;
}

int main()
{
int slices=0;
initQueue();
printf("CPU RUNNING:\n");
// Sleep(500);
while(!isEmpty(readyQueue)){
slices++;
timeSlice();
// Sleep(300);
}
printf("All processes finished! Using %d timelices\n", slices);
return 0;
}

程序功能:模拟动态优先权算法
在windows下能正常运行,在centos6.6下就报错 :segmentation fault(core dumped)了,调了一天都不知道是怎么回事,求大神帮帮忙,
...全文
392 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-05-13
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
paschen 2016-05-13
  • 打赏
  • 举报
回复
出错时断下来,通过调用堆栈、变量窗口等观察分析原因
LubinLew 2016-05-13
  • 打赏
  • 举报
回复
36 行,head是随机值,使用head->next 就是访问了非法地址
手把手讲授如何搭建成功OpenVINO框架,并且使用预训练模型快速开发超分辨率、道路分割、汽车识别、人脸识别、人体姿态和行人车辆分析。得益于OpenVINO框架的强大能力,这些例子都能够基于CPU达到实时帧率。课程的亮点在于在调通Demo的基础上更进一步:一是在讲Demo的时候,对相关领域问题进行分析(比如介绍什么是超分辨率,有什么作用)、预训练模型的来龙去脉(来自那篇论文,用什么训练的)、如何去查看不同模型的输入输出参数、如何编写对应的接口参数进行详细讲解;二是基本上对所有的代码进行重构,也就是能够让例子独立出来,并且给出了带有较详细注释的代码;三是注重实际运用,将Demo进一步和实时视频处理框架融合,形成能够独立运行的程序,方便模型落地部署;四是重难点突出、注重总结归纳,对OpenVINO基本框架,特别是能够提高视频处理速度的异步机制和能够直接部署解决实际问题的骨骼模型着重讲解,帮助学习理解;五是整个课程准备精细,每一课都避免千篇一律,前一课有对后一课的预告,后一课有对前一课的难点回顾,避免学习过程中出现突兀;六是在适当的时候拓展衍生,不仅讲OpenVINO解决图像处理问题,而且还补充图像处理的软硬选择、如何在手机上开发图像处理程序等内容,帮助拓展视野,增强对行业现状的了解。基本提纲:1、课程综述、环境配置2、OpenVINO范例-超分辨率(super_resolution_demo)3、OpenVINO范例-道路分割(segmentation_demo)4、OpenVINO范例-汽车识别(security_barrier_camera_demo)5、OpenVINO范例-人脸识别(interactive_face_detection_demo)6、OpenVINO范例-人体姿态分析(human_pose_estimation_demo)7、OpenVINO范例-行人车辆分析(pedestrian_tracker_demo)8、NCS和GOMFCTEMPLATE9、课程小结,资源分享

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧