Question

I'm trying to get a round robin scheduler working properly and unfortunately, despite bloody tears having been spilled, it still seems to bug somewhere in my code. Here's my function:

//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

And this is my output:

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.

This output has been produced using a 36 sample-long test file and as you can see, only a few values seem to be printed and incorrectly so. And I got a segfault.

Can someone please identify the source of the problem?

No correct solution

OTHER TIPS

This doesn't look ok:

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

See what you're doing - the first assignment sets (*tailcpu)->next to temp2. From now on, you lost reference to whatever was in (*tailcpu)->next. Not only that, the next assignment (*tailcpu = temp2) destroys whatever you wanted to achieve with the previous assignment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top