gpt4 book ai didi

c - 我需要让 Round Robin 工作,但它不能正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:49:40 24 4
gpt4 key购买 nike

我正试图让循环调度程序正常工作,但不幸的是,尽管流下了血泪,它似乎仍然在我的代码中的某个地方出现错误。这是我的功能:

//function that processes the head of the CPU queue (RR version)
int process2 (NodePtr *headcpu, NodePtr *tailcpu, int *count, int timeslice) {

NodePtr temp2;
NodePtr temp3;

(*headcpu)->cpu_time = (*headcpu)->cpu_time - timeslice;

(*count) = (*count) + timeslice;

//if statement starts
if ((*headcpu)->cpu_time <= 0){

temp3 = dequeue_cpu(headcpu, tailcpu);

print_result (temp3, count);//call to print_result function

}//if statement ends

//else statement starts
else {

temp2 = *headcpu;
*headcpu = (*headcpu)->next;
(*tailcpu)->next = temp2;
*tailcpu = temp2;
(*tailcpu)->next = NULL;

}//else statement ends

return 0;//successful termination

}//end process2 function

这是我的输出:

3788   230   31

5001 401 39

5002 402 41

7979 461 63

7919 461 65

1008 72 75

3784 230 87

5000 400 97

7999 456 111

7909 458 115

7989 460 117

7929 462 119


Program received signal SIGSEGV, Segmentation fault.

此输出是使用 36 个样本长的测试文件生成的,如您所见,似乎只打印了几个值,而且打印不正确。我遇到了段错误。

有人可以确定问题的根源吗?

最佳答案

这看起来不太正常:

(*tailcpu)->next = temp2;
*tailcpu = temp2;
(*tailcpu)->next = NULL;

看看你在做什么 - 第一个赋值集 (*tailcpu)->nexttemp2。从现在开始,您将丢失对 (*tailcpu)->next 中的任何内容的引用。不仅如此,下一个赋值 (*tailcpu = temp2) 会破坏您想要通过上一个赋值实现的任何目标。

关于c - 我需要让 Round Robin 工作,但它不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21741860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com