0% found this document useful (0 votes)
10 views2 pages

SRTF

srtf

Uploaded by

fa22-bce-018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

SRTF

srtf

Uploaded by

fa22-bce-018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <limits.h>

int main() {
int numProcesses;

printf("Enter number of processes: ");


scanf("%d", &numProcesses);

int arrivalTime[numProcesses], burstTime[numProcesses],


remainingTime[numProcesses];
int waitingTime[numProcesses], turnaroundTime[numProcesses],
completionTime[numProcesses];
int completed[numProcesses];

// Input arrival and burst times


for (int i = 0; i < numProcesses; i++) {
printf("Enter arrival time of PID %d: ", i + 1);
scanf("%d", &arrivalTime[i]);
printf("Enter burst time of PID %d: ", i + 1);
scanf("%d", &burstTime[i]);
remainingTime[i] = burstTime[i]; // Initialize remaining time
completed[i] = 0; // Initialize completed status
}

int time = 0, completedProcesses = 0;

while (completedProcesses < numProcesses) {


int minIndex = -1;
int minRemainingTime = INT_MAX;

// Find the process with the smallest remaining time that has arrived
for (int i = 0; i < numProcesses; i++) {
if (arrivalTime[i] <= time && !completed[i] && remainingTime[i] <
minRemainingTime) {
minRemainingTime = remainingTime[i];
minIndex = i;
}
}

if (minIndex != -1) {
// Execute the selected process for one unit of time
time++;
remainingTime[minIndex]--;

// If the process is completed


if (remainingTime[minIndex] == 0) {
completed[minIndex] = 1; // Mark process as completed
completedProcesses++; // Increment completed process count
completionTime[minIndex] = time; // Set completion time

// Calculate turnaround and waiting times


turnaroundTime[minIndex] = completionTime[minIndex] -
arrivalTime[minIndex];
waitingTime[minIndex] = turnaroundTime[minIndex] -
burstTime[minIndex];
}
} else {
// If no process is ready, increment time
time++;
}
}

// Calculate averages
float totalWaitingTime = 0, totalTurnaroundTime = 0;
printf("PID\tArrival\tBurst\tCompletion\tTAT\tWait\n");
for (int i = 0; i < numProcesses; i++) {
printf("%d\t%d\t%d\t%d\t\t%d\t%d\n",
i + 1,
arrivalTime[i],
burstTime[i],
completionTime[i],
turnaroundTime[i],
waitingTime[i]);

totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}

printf("\nAverage Waiting Time: %.2f\n", totalWaitingTime / numProcesses);


printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / numProcesses);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy