Last active
March 8, 2021 09:34
-
-
Save AakashCode12/5c5a8835b91c0f4f2ad241b1b7fab0bf to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
struct Process | |
{ | |
int arrivalTime; | |
int burstTime; | |
int remainingTime; | |
}; | |
int completionTime[100]; | |
void finalPrint(struct Process p[], int n) | |
{ | |
int TAT[n]; | |
int WT[n]; | |
printf("\n--------------- FINAL RESULT ----------------"); | |
printf("\n%-15s | %-15s | %-15s | %-15s | %-15s | %-15s", "PROCESS ", "ARRIVAL.T", "BURST.T", "COMPLETION.T", "TurnAround.T", "Waiting.T"); | |
for (int i = 0; i < n; i++) | |
{ | |
TAT[i] = completionTime[i] - p[i].arrivalTime; | |
WT[i] = TAT[i] - p[i].burstTime; | |
printf("\nP-%-15d | %-15d | %-15d | %-15d | %-15d | %-15d", i + 1, p[i].arrivalTime, p[i].burstTime, completionTime[i], TAT[i], WT[i]); | |
} | |
int sumTAT = 0; | |
int sumWT = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
sumTAT += TAT[i]; | |
sumWT += WT[i]; | |
} | |
float avgTAT = sumTAT / n; | |
float avgWT = sumWT / n; | |
printf("\n-------------------------"); | |
printf("\nAVG TurnAround Time :%-25f \nAVG Waiting Time :%-25f", avgTAT, avgWT); | |
} | |
int calculateLowestBurstTime(struct Process p[], int n, int readyQueue[], int readyQueueCounter) | |
{ | |
int min = 0; | |
int element; | |
min = 99999; | |
int pos = 0; | |
for (int i = 0; i < readyQueueCounter; i++) | |
{ | |
element = readyQueue[i]; | |
if (p[element].remainingTime < min && p[element].remainingTime != 0) | |
{ | |
min = p[element].remainingTime; | |
pos = element; | |
} | |
} | |
return pos; | |
} | |
void printGANTTChart(int start, int last, int Pno) | |
{ | |
printf("\n %-5d P%-5d %d ", start, Pno + 1, last); | |
} | |
void main() | |
{ | |
int n; | |
printf("\nEnter the no of processes : "); | |
scanf("%d", &n); | |
struct Process p[n]; | |
int readyQueue[n]; | |
for (int i = 0; i < n; i++) | |
{ | |
readyQueue[i] = -1; | |
} | |
int readyQueueCounter = 0; | |
int sumBurstTime = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
printf("\n-->PROCESS %d", i + 1); | |
printf("\nArrival Time : "); | |
scanf("%d", &p[i].arrivalTime); | |
printf("Burst Time : "); | |
scanf("%d", &p[i].burstTime); | |
p[i].remainingTime = p[i].burstTime; | |
sumBurstTime += p[i].burstTime; | |
} | |
//*Execution Starts here | |
printf("\n--- GANTT CHART FOR PREEMPTIVE SJF---"); | |
for (int exectime = 0; exectime < sumBurstTime; exectime++) | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
if (p[i].arrivalTime == exectime) | |
{ | |
readyQueue[readyQueueCounter] = i; | |
readyQueueCounter++; | |
} | |
} | |
int processNoExecuted = calculateLowestBurstTime(p, n, readyQueue, readyQueueCounter); | |
printGANTTChart(exectime, exectime + 1, processNoExecuted); | |
completionTime[processNoExecuted] = exectime + 1; | |
p[processNoExecuted].remainingTime -= 1; | |
} | |
finalPrint(p, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment