Shortest Remaining Time First


C Program For Shortest Remaining Time First(SRTF) Source Code: #include <stdio.h> #include <stdlib.h> struct Process { int id; int btime; int atime; int isdone; int wtime; int ttime; }; int cmp(const void *a, const void *b) { struct Process* a1 = (struct Process*)a; struct Process* b1 = (struct Process*)b; return (a1->atime - b1->atime); } int allComplete(struct Process *p, int n) { int i; for (i = 0; i < n; i++) { if (p[i].isdone != 1) { return 0; } } return 1; } int main() { struct Process* p; int i, n, g[50], j, currentTime = 0, min, k, total = 0, y = 0, m = 0; printf("Enter Number Of Process: "); scanf("%d", &n); p = (struct Process*)malloc(sizeof(struct Process) * n); for(i = 0; i < n; i++) { printf("Enter The Brust Time Of Process %d: ", i); scanf("%d", &p[i].btime); printf("Enter Arrival Time Of Process %d: ", i); scanf("%d", &p[i].atime); p[i].id = i; p[i].isdone = 0; total += p[i].btime; p[i].ttime = 0; p[i].wtime = 0; } qsort(p, n, sizeof(struct Process), cmp); printf("Process\tATime\tBTime\n"); for (i = 0; i < n; ++i) { printf("P%d\t%d\t%d\n",p[i].id, p[i].atime, p[i].btime); } p[n].atime = 999; int lstTime = p[n-1].atime; while(!allComplete(p, n)) { for(y = 0; y < n; y++) { if(p[y].isdone != 1 && p[y].btime != 999) min = y; break;} for (k = 1; k < n; k++) { printf("%d < %d && %d < %d && %d == %d \n",p[k].btime,p[min].btime,p[k].atime,currentTime, p[k].isdone, 0); if (p[k].btime < p[min].btime && p[k].atime <= currentTime && p[k].isdone == 0) { min = k; } } printf("min is: %d\n",min); for (j = p[min].atime; j < p[min+1].atime && p[min].btime != 999; j++) { g[m++] = min; //printf("-"); printf("\t%d: %d < %d\n", p[min].atime, j, p[min+1].atime); (p[min].btime)--;; currentTime++; if (p[min].btime == 0) {p[min].isdone = 1; p[min].atime = 999; p[min].btime = 999;} } } for(i = 0; i < total; i++) printf("P%d ",g[i]); return 0; }

Share:

0 comments