c - FCFS simulator I think my pop function is not working? -


heres c code. trying simulate fcfs scheduler. reading in command line arguments create timer simulate time. everytime increases check if process arrived (using array), if did put linkedlist represent ready queue. every time timer increases, checks if process done, if pops off front of linked list. problem lays. incorrectly popping off.

i using ./a.out input.dat text.txt 5 0.4 test executing. heres input.dat:

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

here 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 termination[n]; 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]; 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_termination; 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_termination termination;     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_termination ttermination); //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_termination ttermination); //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 find_at(struct job jobss[],int length); void fcfs(); void terminationfcfs(); void checktermination(pcb **pcb_head, int i);  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"   //start of fcfs int timer=0; int locator = 0;   terminationfcfs();//get termination times       for(timer=0;timer<100;timer++){     printf("time %d : ", timer);      checktermination(&pcb_head,timer);                 for(locator=0;locator<10;locator++){//every second checks each 10 values if term(using linked list) or arrival time using array.         //if arrival time add linked list working 5-25 2:14, same print, wrong check termination             if (jobs[locator].at2==timer){                 ll_append(&pcb_head,jobs[locator].pid2,jobs[locator].at2,jobs[locator].priority2,jobs[locator].bt2,termination[locator]);                            }          }          ll_print(&pcb_head);//this works       }//end of fcfs    //ll_print(&pcb_head); /*test find @  add_jobs(full);  find_at(jobs,10); int f=0; while(f<10){printf("%d ", at[f]); f++;} */    /*test if add jobs works) add_jobs(full); int f=0; while(f<10){ printf("%d " , jobs[f].pid2); printf("%d " , jobs[f].at2); printf("%d " , jobs[f].priority2); printf("%d " , jobs[f].bt2); printf("\n"); f++; }*/   /*testing popping array info linked list     jobs[0].pid2=5;     jobs[0].at2=4;     jobs[0].priority2=3;     jobs[0].bt2=2;     ll_append(&pcb_head, jobs[0].pid2,jobs[0].at2,jobs[0].priority2,jobs[0].bt2);  ll_print(&pcb_head); */     /*     //example usage:     ll_add(&pcb_head, 7,7,7,7,10); //push value 7 onto stack     printf("%d\n", pcb_head -> pid); //show stack head value     ll_add(&pcb_head, 21,21,21,21,31); //push value 21 onto stack     ll_print(&pcb_head); //print stack     if(ll_elem(&pcb_head, 7)) puts("found 7"); //does 7 belong stack?     ll_append(&pcb_head, 0,0,0,0,50); //append 0 end of stack     ll_print(&pcb_head); //print stack     ll_pop(&pcb_head); //pop stack's head     ll_print(&pcb_head); //print stack     ll_clear(&pcb_head); //clear stack     ll_print(&pcb_head); //print stack   */     getchar();     return 0; }  int ll_length(pcb *pcb_head) {     pcb *curr = pcb_head;//set temp value head     int len = 0;      while(curr)     {         ++len;//increase length variable         curr = curr -> next;//go next , keep looping whole length     }     return len; }  void ll_add(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt, ll_termination ttermination) {     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 -> termination = ttermination;     pcb_new -> next = *pcb_head;//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("process %d terminating", pcb_temp->pid);         *pcb_head = pcb_temp -> next;//get next in list , set head   }    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 executing...", 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 ll_clear(pcb **pcb_head) {     while(*pcb_head)//this every pcb pop below sets head value next after deleting         ll_pop(pcb_head); }  void ll_append(pcb **pcb_head,ll_pid ppid, ll_at aat, ll_priority ppriority, ll_bt bbt, ll_termination ttermination) {     pcb *pcb_temp = *pcb_head;//get head value      if(!pcb_temp)//if nothing in head value add pcb list empty         ll_add(pcb_head, ppid, aat, ppriority, bbt, ttermination);     else     {         while(pcb_temp -> next)//get last pcb             pcb_temp = pcb_temp -> next;          ll_add(&(pcb_temp -> next), ppid, aat, ppriority, bbt, ttermination);//add onto last     } }  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;  }  void find_at(struct job jobss[], int length){     int i=0;      for(i=0;i<length;i++){         if(jobss[i].bt2>0)         at[i]=jobss[i].at2;     } }    void terminationfcfs(){ //find termination int h =1; int storage; storage= jobs[0].at2+jobs[0].bt2; termination[0]=storage; while(h<10){     storage=storage+jobs[h].bt2;     termination[h]=storage;     h++; } } void checktermination(pcb **pcb_head, int i){      pcb *pcb_temp = *pcb_head;//get head                   while(pcb_temp){                          if(pcb_temp->termination==i)                          ll_pop(&pcb_temp);                          else pcb_temp=pcb_temp->next;                  } }  int ll_elem(pcb **pcb_head, ll_pid x) {     pcb *pcb_temp = *pcb_head;//get head      while(pcb_temp)     {         if(pcb_temp -> pid == x) //set numbers, modifiable             return 1;         else             pcb_temp = pcb_temp -> next;     }     return 0; } 

your error not ll_pop itself, okay, except should free node memory after popping:

void ll_pop(pcb **pcb_head) {     pcb *pcb_temp = *pcb_head;       printf("process %d terminating\n", pcb_temp->pid);     *pcb_head = pcb_temp -> next;     free(pcb_temp); } 

(remember each malloc should have corresponding free. when pushing allocates, popping should free allocated resources.)

the error in routine checks termination, call ll_pop:

ll_pop(&pcb_temp); 

here, pass address of local variable. updating local pointer update iterator pointer, not head of list or next pointers.

do without temporary local pointer , iterate pointer pointer passed in head. add 1 level of indirection. pointer pointer should either hold address of head pointer first node or address of next pointer subsequent nodes. hold address of pointer current node, , therefore update correct "incoming" link:

void checktermination(pcb **pcb_head, int i) {      while (*pcb_head){          if((*pcb_head)->termination == i) {              ll_pop(pcb_head);          } else {              pcb_head = &(*pcb_head)->next;          }      } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -