From 30dc298c6fc08c712bb9b1cdfe56942d658d534d Mon Sep 17 00:00:00 2001 From: Utkarsh Brahma Date: Fri, 24 Oct 2025 20:36:51 +0530 Subject: [PATCH] scheduling algorithms added --- scheduling_algorithms/fcfs_scheduling.c | 42 +++++++++++ scheduling_algorithms/priority_scheduling.c | 69 +++++++++++++++++++ .../round_robin_scheduling.c | 68 ++++++++++++++++++ scheduling_algorithms/sjf_scheduling.c | 65 +++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 scheduling_algorithms/fcfs_scheduling.c create mode 100644 scheduling_algorithms/priority_scheduling.c create mode 100644 scheduling_algorithms/round_robin_scheduling.c create mode 100644 scheduling_algorithms/sjf_scheduling.c diff --git a/scheduling_algorithms/fcfs_scheduling.c b/scheduling_algorithms/fcfs_scheduling.c new file mode 100644 index 0000000000..a080f19477 --- /dev/null +++ b/scheduling_algorithms/fcfs_scheduling.c @@ -0,0 +1,42 @@ +#include + +int main() +{ + int n, bt[20], wt[20], tat[20]; + float avg_wt = 0, avg_tat = 0; + + printf("Enter number of processes: "); + scanf("%d", &n); + + printf("Enter Burst Times:\n"); + for (int i = 0; i < n; i++) + { + printf("P%d: ", i + 1); + scanf("%d", &bt[i]); + } + + wt[0] = 0; + for (int i = 1; i < n; i++) + wt[i] = wt[i - 1] + bt[i - 1]; + + for (int i = 0; i < n; i++) + tat[i] = bt[i] + wt[i]; + + for (int i = 0; i < n; i++) + { + avg_wt += wt[i]; + avg_tat += tat[i]; + } + + avg_wt /= n; + avg_tat /= n; + + printf("\nProcess\tBT\tWT\tTAT\n"); + for (int i = 0; i < n; i++) + printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]); + + printf("\nAverage Waiting Time = %.2f", avg_wt); + printf("\nAverage Turnaround Time = %.2f\n", avg_tat); + + return 0; +} diff --git a/scheduling_algorithms/priority_scheduling.c b/scheduling_algorithms/priority_scheduling.c new file mode 100644 index 0000000000..80d1ea89f6 --- /dev/null +++ b/scheduling_algorithms/priority_scheduling.c @@ -0,0 +1,69 @@ +#include + +int main() +{ + int n, bt[20], p[20], pr[20], wt[20], tat[20]; + float avg_wt = 0, avg_tat = 0; + int i, j, pos, temp; + + printf("Enter number of processes: "); + scanf("%d", &n); + + printf("Enter Burst Time and Priority:\n"); + for (i = 0; i < n; i++) + { + printf("P%d\n", i + 1); + printf("Burst Time: "); + scanf("%d", &bt[i]); + printf("Priority: "); + scanf("%d", &pr[i]); + p[i] = i + 1; + } + + // Sort by Priority (lower number = higher priority) + for (i = 0; i < n; i++) + { + pos = i; + for (j = i + 1; j < n; j++) + { + if (pr[j] < pr[pos]) + pos = j; + } + temp = pr[i]; + pr[i] = pr[pos]; + pr[pos] = temp; + temp = bt[i]; + bt[i] = bt[pos]; + bt[pos] = temp; + temp = p[i]; + p[i] = p[pos]; + p[pos] = temp; + } + + wt[0] = 0; + for (i = 1; i < n; i++) + { + wt[i] = 0; + for (j = 0; j < i; j++) + wt[i] += bt[j]; + avg_wt += wt[i]; + } + + for (i = 0; i < n; i++) + { + tat[i] = bt[i] + wt[i]; + avg_tat += tat[i]; + } + + avg_wt /= n; + avg_tat /= n; + + printf("\nProcess\tBT\tPriority\tWT\tTAT\n"); + for (i = 0; i < n; i++) + printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i], bt[i], pr[i], wt[i], tat[i]); + + printf("\nAverage Waiting Time = %.2f", avg_wt); + printf("\nAverage Turnaround Time = %.2f\n", avg_tat); + + return 0; +} diff --git a/scheduling_algorithms/round_robin_scheduling.c b/scheduling_algorithms/round_robin_scheduling.c new file mode 100644 index 0000000000..d4a9095020 --- /dev/null +++ b/scheduling_algorithms/round_robin_scheduling.c @@ -0,0 +1,68 @@ +#include + +int main() +{ + int n, bt[20], rem_bt[20], wt[20], tat[20], quantum; + int t = 0, i; + float avg_wt = 0, avg_tat = 0; + + printf("Enter number of processes: "); + scanf("%d", &n); + + printf("Enter Burst Times:\n"); + for (i = 0; i < n; i++) + { + printf("P%d: ", i + 1); + scanf("%d", &bt[i]); + rem_bt[i] = bt[i]; + wt[i] = 0; + } + + printf("Enter Time Quantum: "); + scanf("%d", &quantum); + + int done; + while (1) + { + done = 1; + for (i = 0; i < n; i++) + { + if (rem_bt[i] > 0) + { + done = 0; + if (rem_bt[i] > quantum) + { + t += quantum; + rem_bt[i] -= quantum; + } + else + { + t += rem_bt[i]; + wt[i] = t - bt[i]; + rem_bt[i] = 0; + } + } + } + if (done == 1) + break; + } + + for (i = 0; i < n; i++) + { + tat[i] = bt[i] + wt[i]; + avg_wt += wt[i]; + avg_tat += tat[i]; + } + + avg_wt /= n; + avg_tat /= n; + + printf("\nProcess\tBT\tWT\tTAT\n"); + for (i = 0; i < n; i++) + printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]); + + printf("\nAverage Waiting Time = %.2f", avg_wt); + printf("\nAverage Turnaround Time = %.2f\n", avg_tat); + + return 0; +} diff --git a/scheduling_algorithms/sjf_scheduling.c b/scheduling_algorithms/sjf_scheduling.c new file mode 100644 index 0000000000..55d5b56c43 --- /dev/null +++ b/scheduling_algorithms/sjf_scheduling.c @@ -0,0 +1,65 @@ +#include + +int main() +{ + int n, bt[20], p[20], wt[20], tat[20]; + float avg_wt = 0, avg_tat = 0; + int i, j, pos, temp; + + printf("Enter number of processes: "); + scanf("%d", &n); + + printf("Enter Burst Times:\n"); + for (i = 0; i < n; i++) + { + printf("P%d: ", i + 1); + scanf("%d", &bt[i]); + p[i] = i + 1; + } + + // Sort by Burst Time + for (i = 0; i < n; i++) + { + pos = i; + for (j = i + 1; j < n; j++) + { + if (bt[j] < bt[pos]) + pos = j; + } + temp = bt[i]; + bt[i] = bt[pos]; + bt[pos] = temp; + temp = p[i]; + p[i] = p[pos]; + p[pos] = temp; + } + + wt[0] = 0; + for (i = 1; i < n; i++) + wt[i] = 0; + + for (i = 1; i < n; i++) + { + for (j = 0; j < i; j++) + wt[i] += bt[j]; + avg_wt += wt[i]; + } + + for (i = 0; i < n; i++) + { + tat[i] = bt[i] + wt[i]; + avg_tat += tat[i]; + } + + avg_wt /= n; + avg_tat /= n; + + printf("\nProcess\tBT\tWT\tTAT\n"); + for (i = 0; i < n; i++) + printf("P%d\t%d\t%d\t%d\n", p[i], bt[i], wt[i], tat[i]); + + printf("\nAverage Waiting Time = %.2f", avg_wt); + printf("\nAverage Turnaround Time = %.2f\n", avg_tat); + + return 0; +}