Round Robin C scheduling simulator -
i making simulator round robin scheduling algorithm in c. right made time quantum 2. every 2 seconds takes "process" front of list, reduces remaining time 2, , sticks end of list , grabs next one. also, every second, increases others' waiting time 1. when try remove process front of list (so can put in end of list) in function round_robin()
, removes it. not append end of linked list. doing wrong? thank you! using command line arguments :
./a.out input.dat text.txt 5 0.4
and here input.dat
:
1 0 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
and 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 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 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); /* //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_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 = *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("\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 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_remainingtime rrt,ll_wt wtt) { 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, rrt, wtt); else { while(pcb_temp -> next)//get last pcb pcb_temp = pcb_temp -> next; ll_add(&(pcb_temp -> next), ppid, aat, ppriority, bbt, rrt, wtt);//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; } //need change 2 real round bobin quantum, need add if rt less quantum, print rt void round_robin(pcb **pcb_head){ pcb *pcb_temp = *pcb_head; pcb *next = (*pcb_head)->next;//get next value since next head int x; //store heads values 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; (x=0;x<100;x++){ if(*pcb_head){ int h=0; 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 } ll_pop(pcb_head);//get rid of head storert-=2;//its remaining time decreases ll_append(&next,storepid,storeat,storepriority,storebt,storert,storewt);//append list } 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 ll_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; } } 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; }
edit :
#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 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); /* //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_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 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_remainingtime rrt,ll_wt wtt) { 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, rrt, wtt); else { while(pcb_temp -> next)//get last pcb pcb_temp = pcb_temp -> next; ll_add(&(pcb_temp -> next), ppid, aat, ppriority, bbt, rrt, wtt);//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; } //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++){ 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; if(*pcb_head){//if there in list 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 ll_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 ll_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; } } 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; }
this code:
pcb *pcb_temp = *pcb_head; pcb *next = (*pcb_head)->next;//get next value since next head //store heads values 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;
needs done on every loop not once before loop. otherwise keep appending same item end instead of current head item.
also, ll_add()
can't used append list because assumes adding head:
pcb_new -> next = *pcb_head; *pcb_head = pcb_new;
if appending tail, need code looks this:
pcb_new -> next = null; *tail_ptr = pcb_new;
edit:
it seems ll_add()
ever used append tail , not head. in case, change line:
pcb_new -> next = *pcb_head;
to this:
pcb_new -> next = null;
however, if make change, don't try insert head using modified version because won't work.
Comments
Post a Comment