Skip to content

Instantly share code, notes, and snippets.

@AakashCode12
Last active March 12, 2021 13:32
Show Gist options
  • Save AakashCode12/483c93dc3d42d6dd373822249c439bdb to your computer and use it in GitHub Desktop.
Save AakashCode12/483c93dc3d42d6dd373822249c439bdb to your computer and use it in GitHub Desktop.
#include <stdio.h>
struct Process
{
int arrivalTime;
int burstTime;
int remainingTime;
int priority;
};
int completionTime[500];
int pos = 0;
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 | %-15s", "PROCESS ", "ARRIVAL.T", "BURST.T", "PRIORITY", "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 | %-15d", i + 1, p[i].arrivalTime, p[i].burstTime, p[i].priority, 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 calculatelowestPriority(struct Process p[], int n, int readyQueue[], int readyQueueCounter)
{
int min = 0;
int ProcessNo;
min = 99999;
for (int i = 0; i < readyQueueCounter; i++)
{
ProcessNo = readyQueue[i];
if (p[ProcessNo].priority < min && p[ProcessNo].remainingTime != 0)
{
min = p[ProcessNo].priority;
pos = ProcessNo;
if (p[ProcessNo].remainingTime == 0)
{
p[ProcessNo].priority = 9999;
}
}
}
return pos;
}
void printREADYQUEUE(int arr[], int n)
{
printf("\n");
for (int i = 0; i < n; i++)
{
printf("%d\t", arr[i]);
}
}
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;
printf("Priority : ");
scanf("%d", &p[i].priority);
}
//*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++;
}
}
// printREADYQUEUE(readyQueue, n);
int processNoExecuted = calculatelowestPriority(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