[go: up one dir, main page]

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

SRTF Io

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 2

#include <stdio.

h>
#include <limits.h>

int main() {
int numProcesses;

// Input number of processes


printf("Enter number of processes: ");
scanf("%d", &numProcesses);

int arrivalTime[numProcesses], burstTime[numProcesses],


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

// Input arrival, burst, and I/O wait times for each process
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]);
printf("Enter I/O wait time of PID %d: ", i + 1);
scanf("%d", &ioWaitTime[i]);
remainingTime[i] = burstTime[i]; // Initialize remaining time
completed[i] = 0; // Initialize completed status
}

int currentTime = 0, completedProcesses = 0;

// SRTF Scheduling
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] <= currentTime && !completed[i] && remainingTime[i]
< minRemainingTime) {
minRemainingTime = remainingTime[i];
minIndex = i;
}
}

if (minIndex != -1) {
// Execute the selected process for one unit of time
currentTime++;
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] = currentTime; // Set completion time

// Calculate turnaround and waiting times


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

// Print the results


printf("\nPID\tArrival\tBurst\tI/O Wait\tCompletion\tTAT\tWait\n");
for (int i = 0; i < numProcesses; i++) {
printf("%d\t%d\t%d\t%d\t\t%d\t\t%d\t%d\n",
i + 1,
arrivalTime[i],
burstTime[i],
ioWaitTime[i],
completionTime[i],
turnaroundTime[i],
waitingTime[i]);
}

return 0;
}

You might also like