c - Append to linked list not working? -


i trying make round robin scheduling simulator in c. made program reads input file , stores of processes info array. put info array of "jobs". made counter function increases time , checks if of jobs arrival times time. if gets inserted "ready queue" linked list. , increases waiting time of in linked list.

then made function round robin. gets head, , calls counter function twice. removes head , puts end of list. think there problem here. process 3 somehow ending before process 7.

i using command line arguments so:

./a.out input.dat text.txt 5 0.4 

and input.dat follows:

1 0 10 3  2 2 0 4  3 3 20 5  4 4 7 3  5 5 10 5  6 6 0 4  7 7 20 5  8 9 7 3  9 10 10 5  10 12 0 4 

here necessary part of code:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define n 10 char *crr;//get argument rr time char *calpha;//get argument alpha  int full[40]; int arraylength; int rr;//to hold rr time int int i=0;//used count amount of numbers in test file double alpha;//hold alpha time double int at[n];  int global_timer=-1;   typedef int ll_pid; // pid of pcb typedef int ll_at;//the arrival time of pcb typedef int ll_priority;//the priority of pcb typedef int ll_bt;//the burst time of pcb typedef int ll_remainingtime; typedef int ll_wt; typedef struct ll pcb; //the pcb struct ll //linked list structure(pcb) {     ll_pid pid;     ll_at at;     ll_priority priority;     ll_bt bt;     ll_remainingtime rt;     ll_wt wt;     pcb *next;//points next pcb in linked list }; struct job//used job queue {     int pid2;     int at2;     int priority2;     int bt2; }; int ll_length(pcb *pcb_head); //linked list length void ll_add(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt, ll_remainingtime rrt,ll_wt wtt); //adds pcb front of list void ll_pop(pcb **pcb_head); //removes head list & returns value void ll_print(pcb **pcb_head); //prints processes void ll_clear(pcb **pcb_head); //deletes processes queue void ll_append(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt,ll_remainingtime rrt,ll_wt wtt); //adds process end of list int ll_elem(pcb **pcb_head, ll_pid d); //checks pcb via pid void add_jobs(int jobss[]);  struct job jobs[n];//contains jobs array void append(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt, ll_remainingtime rrt,ll_wt wtt); void counter(pcb **pcb_head); void round_robin(pcb **pcb_head);   int main ( int argc, char *argv[] ) { pcb *pcb_head=null;  //*****start command line args if ( argc != 5 )//if incorrectly inputed arguments     {         /* print argv[0] assuming program name */         printf( "usage: %s filename, output file name, rr quantum int, alpha value float", argv[0] );     }     else     {         crr=argv[3];//get arg rr time         calpha=argv[4];//get arg alpha         alpha=atof(calpha);//convert alpha float         rr=atoi(crr);//convert rr int           // assume argv[1] filename open         file *file = fopen( argv[1], "r" );//open file 2nd arg         // fopen returns 0, null pointer, on failure          if ( file == 0 )         {             printf( "could not open file\n" ); } else         {         int y=0;//used while loop below          while  ( fscanf(file,"%d",&y)!=eof )//scan file , find numbers             {               full[i]=y;//insert numbers array                 i++;             }             fclose( file );//done using file         } /*test printing in file int testprint=0; for(testprint=0;testprint<i;testprint++){printf("%d\n",full[testprint]);} printf("\nend of array check");*/ //write file    file *fp;     fp = fopen(argv[2], "w+");    fprintf(fp, "this testing fprintf...\n");     fclose(fp);      } /////////////////end of command argument things   add_jobs(full);//adds inputs file array "jobs" counter(&pcb_head); round_robin(&pcb_head);        getchar();     return 0; }    void ll_add(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt, ll_remainingtime rrt, ll_wt wtt) {     pcb *pcb_new = malloc(sizeof(pcb));//allocate memory structure (pcb)      pcb_new -> pid = ppid;//create pcb pid inserted     pcb_new -> @ = aat;     pcb_new -> priority = ppriority;     pcb_new -> bt = bbt;     pcb_new -> rt = rrt;     pcb_new -> wt = wtt;     pcb_new -> next = null;//set next head(inserting in front of list)     *pcb_head = pcb_new;//set pointer of head since in front of list }  void ll_pop(pcb **pcb_head) {     pcb *pcb_temp = *pcb_head;       printf("\nprocess %d terminating\n", pcb_temp->pid);     *pcb_head = pcb_temp -> next;     free(pcb_temp); }      void ll_print(pcb **pcb_head) {     pcb *pcb_temp = *pcb_head;      if(!pcb_temp)         puts("the list empty");     else     {       // while(pcb_temp)        //{             printf("process %d has terminated...", pcb_temp -> pid);// print pids        // printf("%d ", pcb_temp -> at);        // printf("%d ", pcb_temp -> priority);        // printf("%d ", pcb_temp -> bt);            //pcb_temp = pcb_temp -> next;//loop next pcb*/         //}         putchar('\n');     } }    void append(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt, ll_remainingtime rrt,ll_wt wtt){ pcb* current = *pcb_head; pcb* newpcb = null; newpcb = malloc(sizeof(pcb)); newpcb->pid = ppid; newpcb->at = aat; newpcb->priority = ppriority; newpcb->bt = bbt; newpcb->rt = rrt; newpcb->wt = wtt; newpcb->next = null; if(current == null){ *pcb_head = newpcb; }else{ while(current->next != null){ current = current->next; } current->next = newpcb; } }   void add_jobs(int jobss[]){ int = 0; int i2 = 1; int i3 = 2; int i4 = 3; int x =0; int x2=0; int x3=0; int x4=0;     while(x<10){     jobs[x].pid2=jobss[i];     x++;         i+=4;     }      while(x2<10){     jobs[x2].at2=jobss[i2];     i2+=4;     x2++;     }           while(x3<10){         jobs[x3].priority2=jobss[i3];         i3+=4;         x3++;         }          while(x4<10){         jobs[x4].bt2=jobss[i4];         i4+=4;         x4++;         }        arraylength=x4;  }    //need change 2 real round bobin quantum, need add if rt less quantum, print rt void round_robin(pcb **pcb_head){ int x; (x=0;x<250;x++){      if(*pcb_head){//if there in list pcb *pcb_temp = *pcb_head; pcb *next = (*pcb_head)->next;//get next value since next head int storepid = pcb_temp->pid; int storeat = pcb_temp->at; int storepriority=pcb_temp->priority; int storebt=pcb_temp->bt; int storert=pcb_temp->rt; int storewt=pcb_temp->wt;         int h=0;             if(storert>=2){//if remaining time greater quantum                 for(h=0;h<2;h++){//2 represented time quantum right                     printf("\nprocess %d running", (*pcb_head)->pid);                     counter(pcb_head);//even tho increases wt stored original wt in storewt                     //print quantum                 }                 ll_pop(pcb_head);//get rid of head                 storert-=2;//its remaining time decreases quantum                 if(storert>0){//if still has remaining time append                     append(&next,storepid,storeat,storepriority,storebt,storert,storewt);}//append list}                 else{printf("process has finished");}//if doesnt type finished             }             else{//if remaining time less quantum                 for(h=0;h<storert;h++){//do how remaining                     printf("\nprocess %d running", (*pcb_head)->pid);                     counter(pcb_head);                 }                ll_pop(pcb_head);//get rid of head because finished             printf("process has finished");                  }         }     else{     printf("....");     counter(pcb_head);}//run counter , see if new processes  }    }  void counter(pcb **pcb_head){ int locator; pcb *pcb_temp = *pcb_head; global_timer++;//increase time printf("\ntime %d:", global_timer);         for(locator=0;locator<10;locator++){             if (jobs[locator].at2==global_timer){//see if processes ready enter ready queue linked list                 append(pcb_head,jobs[locator].pid2,jobs[locator].at2,jobs[locator].priority2,jobs[locator].bt2,jobs[locator].bt2,0); }                        } while(pcb_temp){ pcb_temp-> wt +=1;//increase everyones waiting time in linked list ready queue pcb_temp = pcb_temp->next; }    } 

it's same error made earlier. when say:

pcb *next = (*pcb_head)->next; 

both pointers point same location, addresses different. &next local stack address, &(*pcb_head)->next address inside existing, heap-allocated list.

when call

append(&next, ...); 

you update local variable, list not affected. use:

append(pcb_head, ...); 

to update links inside list or list head.

caveat: haven't tested this, i'm pretty sure that's it. append function isn't wrong, far can see. "streamline" code , rid of many local pointers if learned "iterate pointer" technique:

void append(node **head, ...) {     while (*head) {         head = &(*head)->next;     }      *head = new_node(...); } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -