0% found this document useful (0 votes)
12 views1 page

SRTF

The document contains a C program that implements a Preemptive Shortest Job First (SJF) scheduling algorithm for process management. It prompts the user to enter the number of processes and their respective arrival and burst times, then calculates completion time, waiting time, and turnaround time for each process. Finally, it outputs the process details along with the average waiting and turnaround times.

Uploaded by

aftabkapsi64
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)
12 views1 page

SRTF

The document contains a C program that implements a Preemptive Shortest Job First (SJF) scheduling algorithm for process management. It prompts the user to enter the number of processes and their respective arrival and burst times, then calculates completion time, waiting time, and turnaround time for each process. Finally, it outputs the process details along with the average waiting and turnaround times.

Uploaded by

aftabkapsi64
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/ 1

#include <stdio.

h>
int main() {
int n, i, j, time = 0, done = 0;

printf("Enter number of processes: ");


scanf("%d", &n);

int pid[n], at[n], bt[n], remaining[n], ct[n], wt[n], tat[n];

for (i = 0; i < n; i++) {


pid[i] = i + 1;
printf("Enter AT, BT for Process %d: ", i + 1);
scanf("%d %d", &at[i], &bt[i]);
remaining[i] = bt[i];
}

// Preemptive SJF Scheduling


while (done < n) {
int minBurst = 999, idx = -1;
for (i = 0; i < n; i++) {
if (at[i] <= time && remaining[i] > 0 && remaining[i] < minBurst) {
minBurst = remaining[i];
idx = i;
}
}

if (idx == -1) {
time++;
continue;
}
remaining[idx]--;
time++;

if (remaining[idx] == 0) {
ct[idx] = time;
done++;
}
}

int totalWT = 0, totalTAT = 0;


for (i = 0; i < n; i++) {
wt[i] = ct[i] - at[i] - bt[i];
tat[i] = wt[i] + bt[i];
totalWT += wt[i];
totalTAT += tat[i];
}

printf("\nPID\tAT\tBT\tCT\tWT\tTAT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",pid[i], at[i], bt[i], ct[i], wt[i],
tat[i]);
}

printf("\nAverage waiting time: %.2f\n", (float)totalWT / n);


printf("Average turnaround time: %.2f\n", (float)totalTAT / n);
return 0;
}

You might also like