Scheduling algorithms in C Shortest Remain Time Next -
Scheduling algorithms in C Shortest Remain Time Next -
i working on shortest remaining time next scheduling, must check every 1 time unit see if there job has shorter time remaining left, if equal maintain current process. input use:
pid arrivaltime burst/executiontime 1 0 6 2 3 2 3 5 1 4 9 7 5 10 5 6 12 3 7 14 4 8 16 5 9 17 7 10 19 2
and output: (on left getting, right should be):
pid wait turnaround pid wait turnaround 1 0 9 1 0 9 2 0 2 2 0 2 3 0 1 3 0 1 4 0 26 4 0 26 5 0 14 5 0 5 6 0 3 6 3 6 7 1 5 7 4 10 8 8 13 8 8 13 9 18 25 9 18 25 10 0 2 10 0 2 average wait: 2.7 ave turnaround 10.0 average wait: 3.3 average turnaround 9.9
i have not been able narrow downwards problem in srtn function, 3 of outputs correct, more confusing! help appreciated!
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <termios.h> #include <signal.h> #include <fcntl.h> #include <sys/types.h> #define linelen 512 #define max_process 100 #define time_quantum 1 typedef struct process { int id; int arrival_time; int time_to_completion; double wait_time; double turn_around; double time_wait; int active; }process; void fcfs(struct process[max_process], int); void sjf (struct process[max_process], int); void srtn(struct process[max_process], int); void rr (struct process[max_process], int); void rrc(struct process[max_process], int); void print_info(struct process[max_process], int); void sort_by_time(struct process array[max_process], int num_valid_pid); int main(int ac,char *av[]) { int counter=0; int p1=0, p2=0, p3=0; process array[max_process]; while ( scanf("%d %d %d", &p1, &p2, &p3) != eof ){//get info available , set in array of structs array[counter].id = p1; array[counter].arrival_time = p2; array[counter].time_to_completion = p3; array[counter].active = 0; counter++; } //fcfs(array, counter); //sjf (array, counter); srtn(array, counter); /*rr (array, counter); rrc(array, counter);*/ homecoming 0; } void srtn(struct process array[max_process], int num_pid){ printf("shortest remaining time next\n");//for output know algorithm //create array of pids valid search. int num_valid_processes = 0, current_time=0, i,j, next_process, counter = 0, fin_pid = 0, keep_going=0;//declarations process to_sort[max_process]; //we want next loop many processes have, or num_pid while(keep_going!=1){ //adds available processes to sort array sorted //available means has arrived, means <= current_time //after gets processes, breaks out of loop for(i=counter; i<num_pid; i++){ if(array[i].arrival_time<=current_time){ to_sort[num_valid_processes]=array[i]; num_valid_processes++; counter++; } else break; } //sort to_sort array time_to_completion sort_by_time(to_sort,num_valid_processes); //set wait time , turnaround time next process next_process = to_sort[0].id-1; if(array[next_process].active==0){//the id hasn't had wait time calculated yet array[next_process].wait_time = current_time-array[next_process].arrival_time; array[next_process].active=1; array[next_process].time_wait = current_time; } if(array[next_process].time_to_completion <= time_quantum){ array[next_process].turn_around = array[next_process].wait_time + (current_time-array[next_process].time_wait)+array[next_process].time_to_completion; fin_pid++; //delete process worked on don't duplicates. num_valid_processes--; for(i=0;i<num_valid_processes;i++){ to_sort[i]=to_sort[i+1]; } } else{ array[next_process].time_to_completion = array[next_process].time_to_completion - time_quantum; //to_sort[0].time_to_completion = to_sort[next_process].time_to_completion - time_quantum; } current_time = current_time+time_quantum; if(fin_pid==num_pid) keep_going=1; } print_info(array, num_pid); } void print_info(struct process array[max_process], int num_pid){ int i; double tot_wait=0, tot_turn = 0; printf("\x1b[04mpid\twait\tturnaround\n\x1b[24m"); for(i=0; i<num_pid; i++){ printf("%d\t%.0f\t%.0f\n", array[i].id, array[i].wait_time, array[i].turn_around); tot_wait=tot_wait+array[i].wait_time; tot_turn = tot_turn +array[i].turn_around; } printf("average wait: %.1f average turnaround %.1f\n", tot_wait/num_pid, tot_turn/num_pid); } void sort_by_time(struct process array[max_process], int num_valid_pid) { int i,j; (i = 0; < num_valid_pid; i++) { int min = i; (j = i+1; j < num_valid_pid; j++){ if (array[j].time_to_completion < array[min].time_to_completion) min = j; if (array[j].time_to_completion == array[min].time_to_completion){ if(array[j].id<array[min].id) min = j; } } process temp = array[i]; array[i] = array[min]; array[min] = temp; } }
the problem occurs @ time 12 -- pid 6 shows needing 3 seconds , pid 5 running 3 seconds left. how resolve tie between processes need same remaining time? in favor of pid 6 gives result on left , in favor of pid 5 gives result on right. given weak problem definition, either correct.
c scheduling
Comments
Post a Comment