I have code that outputs data as so
Input file name: file.txt
Input text:
Process id 1, Quantum No 2, Priority 0
Process id 2, Quantum No 3, Priority 1
Process id 3, Quantum No 3, Priority 0
Process id 4, Quantum No 1, Priority 2
Start of processing --->
Process id 1 -> Quantum No: 0 Quantum left: 2 -> Quantum Total:2 -> Priority:0 -> Total time:0
Process id 1--> wt is 0
Process id 1 -> Quantum No: 1 Quantum left: 1 -> Quantum Total:2 -> Priority:0 -> Total time:1
Process id 1 -> Quantum No: 2 Quantum left: 0 -> Quantum Total:2 -> Priority:0 -> Total time:2
Process id 1--> trt is 2
Process id 3 -> Quantum No: 0 Quantum left: 3 -> Quantum Total:3 -> Priority:0 -> Total time:3
Process id 3--> wt is 3
Process id 3 -> Quantum No: 1 Quantum left: 2 -> Quantum Total:3 -> Priority:0 -> Total time:4
Process id 3 -> Quantum No: 2 Quantum left: 1 -> Quantum Total:3 -> Priority:0 -> Total time:5
Process id 3 -> Quantum No: 3 Quantum left: 0 -> Quantum Total:3 -> Priority:0 -> Total time:6
Process id 3--> trt is 6
Process id 2 -> Quantum No: 0 Quantum left: 3 -> Quantum Total:3 -> Priority:1 -> Total time:7
Process id 2--> wt is 7
Process id 2 -> Quantum No: 1 Quantum left: 2 -> Quantum Total:3 -> Priority:1 -> Total time:8
Process id 2 -> Quantum No: 2 Quantum left: 1 -> Quantum Total:3 -> Priority:1 -> Total time:9
Process id 2 -> Quantum No: 3 Quantum left: 0 -> Quantum Total:3 -> Priority:1 -> Total time:10
Process id 2--> trt is 10
Process id 4 -> Quantum No: 0 Quantum left: 1 -> Quantum Total:1 -> Priority:2 -> Total time:11
Process id 4--> wt is 11
Process id 4 -> Quantum No: 1 Quantum left: 0 -> Quantum Total:1 -> Priority:2 -> Total time:12
Process id 4--> trt is 12
Total time taken: 13
wt total =21
trt total =30
Average Wait time = 5.0000
Average Turn around time = 7.0000
It sorts out the prioritys into ques 0 highest, 20 lowest
What i want it to do is instead of printing out the data from the que
in a fcfs basis (e.g. making it pure priority)
to print out in a round rob basis (e.g. making it priority ques)
so my output data would turn from (for just priority 0)
processid1 priority 0
processid1 priority 0
processid1 priority 0
processid3 priority 0
processid3 priority 0
processid3 priority 0
would become
process id1 priority 0
processid3 priority 0
process id1 priority 0
processid3 priority 0
process id1 priority 0
processid3 priority 0
Here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct assign //array struct for assigning ints from txt
{
int ProcessID;
int Quantum;
int Priority;
};
struct average // struct for trt and wt
{
int b; // for wt
int g; // for trt
};
int compare(const void *a, const void *b) // for qsort
{
const struct assign *x = a, *y = b;
if ( x->Priority < y->Priority )
{
return -1;
}
if ( x->Priority > y->Priority )
{
return 1;
}
if ( x->Quantum < y->Quantum )
{
return -1;
}
if ( x->Quantum > y->Quantum )
{
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
char filename[256]; //array to store filename in
strcpy(filename, argv[1]); //copies the filename the user entered to filename
FILE *file = fopen(filename, "r"); // opens and reads file
struct assign array[50]; // sets struct assign to a array named array
struct average Average[50]; // sets struct average to a array named Average
int count = 0; // used moving through struct array when assigning
int i; // moving through struct array when printfing
int x; // current quantum
int z; // amount quantum left
int c; // total amount of time taken
int q; // total amount of processes
int wt; // wait time
int trt; // turn around time
float avwt; // average wait time
float avtrt; // average turn around time
int w; // for moving through arrays b and g
if ( file )
{
char line[20];
while ( fgets(line, sizeof line, file) &&
count < sizeof array / sizeof *array ) // reads file line time
{
if ( sscanf(line, "Process%d %d %d",
&array[count].ProcessID,
&array[count].Quantum,
&array[count].Priority) == 3 ) // assigns value to array struct assign
{
++count;
}
}
}
else
{
perror(filename); // error message
}
printf("Input file name: %s \nInput text:\n",filename); // for user, makes easier to read
for ( i = 0; i < count; ++i )
{
printf("Process id %d, Quantum No %d, Priority %d\n",
array[i].ProcessID, array[i].Quantum, array[i].Priority); // prints out all the values in the text
}
printf("\nStart of processing --->\n"); // for user, makes easier to read
for ( i = 0; i < count; ++i )
{
qsort(array, count, sizeof *array, compare); //sorts out the priority in order
{
for (x = 0; x <= array[i].Quantum; ++x)
{
z = (array[i].Quantum - x); // works out amount of quantum left
printf("Process id %d -> Quantum No: %d Quantum left: %d -> Quantum Total:%d -> Priority:%d -> Total time:%d\n",array[i].ProcessID,x,z,array[i].Quantum,array[i].Priority,c); // prints out the number of times the quatunum is
c++; // counts amount times run through loop for total time taken
while( x == array[i].Quantum) // while quantum total is = total quantum number
{
printf("Process id %d--> trt is %d\n",array[i].ProcessID,c-1);
Average[w].g = c-1;
trt = trt + Average[w].g ; // + whatever in struct array to trt
break;
}
while ( x == 0) // while x is = to 0 do i.e the end of that process id
{
printf("Process id %d--> wt is %d\n",array[i].ProcessID,c-1);
Average[w].b = c-1;
wt = wt + Average[w].b; // + whatever in struct array to wt
break;
}
}
q = i+1; // total number of processes
}
printf("\n"); // prints new line, makes easier to read
}
printf("\nTotal time taken: %d\n\n",c); // the amount of total time taken to process all
printf("\nwt total =%d\n",wt); // prints total of array for wt
avwt = (wt/q); // divides wt by total number of processes
avtrt = (trt/q); // divides trt by total number of processes
printf("trt total =%d\n\n",trt); // prints total of array for trt
printf("Average Wait time = %-8.4f\n",avwt); // prints out the average waiting time (Not working)
printf("Average Turn around time = %-8.4f\n\n",avtrt); // prints out the average turn around time (Not working)
fclose(file); // closes file
return 0; // returns 0 to end program
}
I am thinking maybe adding another for loop beneath the others or something like that
anyone have ideas????