#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)了,调了一天都不知道是怎么回事,求高手帮帮忙,
解决方案
40
36 行,head是随机值,使用head->next 就是访问了非法地址