Created
December 10, 2021 19:30
-
-
Save hatim-the-dark-knight/cbd1461d6fc42cbdbeeeb45c1915c18d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PRIORITY CPU SCHEDULING | |
#include<stdio.h> | |
struct pris { | |
int pid, pri, atime, btime, ctime, wtime, tatime; | |
short check; | |
}; | |
void main () { | |
int i, j, k, n, t, twtime = 0, ttatime; | |
printf ("PRIORITY SCHEDULING\n\nEnter the no. of processes -> "); | |
scanf ("%d", &n); | |
struct pris p[n]; | |
int q[n]; | |
printf ("\nInput Pri, AT and BT ->\n\n\tPri\tA.T\tB.T\n"); | |
for (i = 0; i < n; i++) { | |
p[i].pid = i; p[i].check = 0; p[i].ctime = 0; p[i].wtime = 0; p[i].tatime = 0; q[i] = -1; | |
printf ("P%d\t", p[i].pid); | |
scanf ("%d %d %d", &p[i].pri, &p[i].atime, &p[i].btime); | |
} | |
t = 0; j = 0; k = 0; | |
while (j != n) { | |
printf ("\nt updated to %d ", t); | |
for (i = 0; i < n; i++) { | |
if (t >= p[i].atime && p[i].check != 1) { | |
q[j] = i; | |
p[i].check = 1; | |
printf (" P%d ", q[j]); | |
j++; | |
} | |
} | |
printf ("arrived! "); | |
int temp; | |
for (i = k; i < j; i++) { | |
for (int l = i+1; l < j; l++) { | |
if (p[q[i]].pri > p[q[l]].pri) { | |
temp = q[i]; | |
q[i] = q[l]; | |
q[l] = temp; | |
} | |
else if (p[q[i]].pri == p[q[l]].pri) { | |
if (p[q[i]].atime > p[q[l]].atime) { | |
temp = q[i]; | |
q[i] = q[l]; | |
q[l] = temp; | |
} | |
} | |
} | |
} | |
t += p[q[k]].btime; | |
p[q[k]].ctime = t; | |
p[q[k]].tatime = p[q[k]].ctime - p[q[k]].atime; | |
p[q[k]].wtime = p[q[k]].tatime - p[q[k]].btime; | |
twtime += p[q[k]].wtime; | |
ttatime += p[q[k]].tatime; | |
printf ("\nP%d executed!", p[q[k]].pid); | |
k++; | |
} | |
for (i = k; i < n; i++) { | |
printf ("\nt updated to %d ", t); | |
t += p[q[i]].btime; | |
p[q[i]].ctime = t; | |
p[q[i]].tatime = p[q[i]].ctime - p[q[i]].atime; | |
p[q[i]].wtime = p[q[i]].tatime - p[q[i]].btime; | |
twtime += p[q[i]].wtime; | |
ttatime += p[q[i]].tatime; | |
printf ("\nP%d executed!", p[q[i]].pid); | |
} | |
printf ("\n\n\tPri\tA.T\tB.T\tC.T\tT.A.T\tW.T"); | |
for (i = 0; i < n; i++) | |
printf ("\nP%d\t%d\t%d\t%d\t%d\t%d\t%d", p[q[i]].pid, p[q[i]].pri, p[q[i]].atime, p[q[i]].btime, p[q[i]].ctime, p[q[i]].tatime, p[q[i]].wtime); | |
printf ("\n\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment