Last active
April 27, 2022 03:22
-
-
Save hatim-the-dark-knight/1e4b21dbe28b29f45b4c35035e514469 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
// FCFS SCHEDULING | |
#include<stdio.h> | |
struct process { | |
int pid, atime, btime, ctime, tatime, wtime; | |
}; | |
int main () { | |
printf ("FCFS SCHEDULING\n"); | |
int n, ttatime = 0, twtime = 0; | |
printf ("Enter the no. of processes -> "); | |
scanf ("%d", &n); | |
struct process p[n], temp; | |
printf ("\nP\tAT\tBT\n"); | |
for (int i = 0; i < n; i++) { | |
p[i].pid = i+1; | |
printf ("P%d\t", p[i].pid); | |
scanf ("%d\t%d", &p[i].atime, &p[i].btime); | |
p[i].ctime = 0; | |
p[i].tatime = 0; | |
p[i].wtime = 0; | |
} | |
for (int i = 0; i < n-1; i++) { | |
for (int j = 0; j < n-i-1; j++) { | |
if (p[j].atime > p[j+1].atime) { | |
temp = p[j]; | |
p[j] = p[j+1]; | |
p[j+1] = temp; | |
} | |
} | |
} | |
for (int i = 0; i < n; i++) { | |
if (i == 0) | |
p[0].ctime = p[0].atime + p[0].btime; | |
else if (p[i].atime < p[i-1].ctime) | |
p[i].ctime = p[i-1].ctime + p[i].btime; | |
else | |
p[i].ctime = p[i].atime + p[i].btime; | |
p[i].tatime = p[i].ctime - p[i].atime; | |
p[i].wtime = p[i].tatime -p[i].btime; | |
ttatime += p[i].tatime; | |
twtime += p[i].wtime; | |
} | |
printf ("\nP\tAT\tBT\tCT\tTAT\tWT\n"); | |
for (int i = 0; i < n; i++) { | |
printf ("\nP%d\t%d\t%d\t%d\t%d\t%d", p[i].pid, p[i].atime, p[i].btime, p[i].ctime, p[i].tatime, p[i].wtime); | |
} | |
printf ("\n\nAverage TAT -> %f", (float) ttatime/n); | |
printf ("\nAverage WT -> %f\n\n", (float) twtime/n); | |
} |
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"); | |
} |
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
//Round Robin CPU SCHEDULING | |
#include<stdio.h> | |
#include<stdlib.h> | |
struct rrs { | |
int pid, atime, btime, tempb, ctime, tatime, wtime; | |
short isTraversed; | |
}; | |
void main () { | |
int i, j, k, n, count = 0, qt, curr_time = 0, arriv = 0, leucpid; | |
float ttatime = 0, twtime = 0; | |
printf ("\nRRS CPU SCHEDULING\n\nEnter the no. of processes -> "); | |
scanf ("%d", &n); | |
struct rrs p[n]; | |
// dynamic allocation for ready queue | |
int *q; | |
q = (int *)malloc (sizeof (int)); | |
printf ("\nEnter the values -> \n"); | |
printf ("P[pid]\tAT\tBT\n"); | |
for (i = 0; i < n; i++) { | |
p[i].isTraversed = 0; | |
p[i].pid = i; | |
printf ("P[%d]\t", p[i].pid); | |
scanf ("%d %d", &p[i].atime, &p[i].btime); | |
p[i].tempb = p[i].btime; // burstTime left after preemption stored in p[i].tempb | |
} | |
printf ("\nEnter the time quantum -> "); | |
scanf ("%d", &qt); | |
j = 0; // ready queue index | |
k = 0; // completed process index in ready queue | |
leucpid = -1; // stores id of last executed uncomplete process | |
printf ("\nP\tAT\tBT\tCT\tTAT\tWT"); | |
while (count != n) { | |
if (arriv < n) | |
for (i = 0; i < n; i++) | |
if (p[i].isTraversed == 0) // checks whether a process is traversed once to avoid repetition of processes in the ready queue | |
if (curr_time >= p[i].atime) { | |
q[j] = i; | |
p[q[j]].isTraversed = 1; | |
arriv++; | |
j++; | |
} | |
if (leucpid != -1) { // for storing id of the preempted process | |
q[j] = leucpid; | |
j++; | |
} | |
if (p[q[k]].tempb <= qt) { | |
curr_time += p[q[k]].tempb; | |
p[q[k]].tempb = 0; | |
p[q[k]].ctime = curr_time; | |
p[q[k]].tatime = p[q[k]].ctime - p[q[k]].atime; | |
p[q[k]].wtime = p[q[k]].tatime - p[q[k]].btime; | |
ttatime += p[q[k]].tatime; | |
twtime += p[q[k]].wtime; | |
printf ("\nP%d\t%d\t%d\t%d\t%d\t%d", p[q[k]].pid, p[q[k]].atime, p[q[k]].btime, p[q[k]].ctime, p[q[k]].tatime, p[q[k]].wtime); | |
leucpid = -1; | |
k++; | |
count++; | |
} | |
else if (p[q[k]].tempb > qt) { | |
curr_time += qt; | |
p[q[k]].tempb -= qt; | |
leucpid = p[q[k]].pid; | |
k++; | |
} | |
} | |
printf ("\n\nAverage Turnaround Time: %f", ttatime/n); | |
printf ("\nAverage Waiting Time: %f\n\n", twtime/n); | |
} |
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
// SJF CPU SCHEDULING | |
#include<stdio.h> | |
struct pris { | |
int pid, atime, btime, ctime, wtime, tatime; | |
short check; | |
}; | |
void main () { | |
int i, j, k, n, t, twtime = 0, ttatime; | |
printf ("SJF SCHEDULING\n\nEnter the no. of processes -> "); | |
scanf ("%d", &n); | |
struct pris p[n]; | |
int q[n]; | |
printf ("\nInput AT and BT ->\n\n\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", &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]].btime > p[q[l]].btime) { | |
temp = q[i]; | |
q[i] = q[l]; | |
q[l] = temp; | |
} | |
else if (p[q[i]].btime == p[q[l]].btime) { | |
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\tA.T\tB.T\tC.T\tT.A.T\tW.T\n"); | |
for (i = 0; i < n; i++) | |
printf ("P%d\t%d\t%d\t%d\t%d\t%d\n", p[q[i]].pid, p[q[i]].atime, p[q[i]].btime, p[q[i]].ctime, p[q[i]].tatime, p[q[i]].wtime); | |
printf("\nAverage turn around time: %f\n", (float)ttatime/n); | |
printf("Average waiting time: %f\n\n", (float)twtime/n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment