使用 valgrind 找到内存错误。有时,在实际遇到段错误之前,对内存的错误处理就会开始。
valgrind
它对我来说没问题 - 在代码中找不到任何错误。
它会立即失败,还是在运行一段时间后失败?你确定吗? init_q 函数实际上在开始使用队列之前运行?你有没有试过检查你是否得到了 NULL 作为参数 new_insert 要么 re_insert 功能?
init_q
NULL
new_insert
re_insert
顺便说一句,这个程序没有以多线程模式运行,对吧?如果是,您需要在关键部分保护全局变量。
的 EDIT4: 强> 现在我觉得我明白了! ready_q_tail 从队列中删除最后一个任务时,永远不会设置为新值。
ready_q_tail
这是(希望)修复的 schedule_next 功能:
schedule_next
PCB_entry * schedule_next (void) { /* if ready_q is null, there are no processes in the system */ if (ready_q == NULL) { return NULL; } else { PCB_entry *next; PCB_entry *best_proc; next = ready_q; best_proc = next; while (next != NULL) { /* traverse to the end of the list */ if (next->current_priority current_priority) { best_proc = next; } next = next->f_link; } if (best_proc == NULL) { return NULL; } // Remove task from queue { int procHasNext = best_proc->f_link != NULL; int procHasPrev = best_proc->b_link != NULL; if (!procHasNext && !procHasPrev) { // There's only one task in the queue ready_q = NULL; ready_q_tail = NULL; } else { if (procHasNext) { if (procHasPrev) { best_proc->b_link->f_link = best_proc->f_link; } else { // Proc to remove is the first in queue ready_q = best_proc->f_link; ready_q->b_link = NULL; } } if (procHasPrev) { if (procHasNext) { best_proc->f_link->b_link = best_proc->b_link; } else { // Proc to remove is last in queue ready_q_tail = best_proc->b_link; ready_q_tail->f_link = NULL; } } } } // Ensure that the links in PCB to remove doesn't cause any problems best_proc->f_link = NULL; best_proc->b_link = NULL; return best_proc; } }