First Come First Serve (FCFS)
#include <stdio.h>
void findWaitingTime(int processes[ ], int n, int bt[ ], int wt[ ]) {
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
}
void findTurnAroundTime(int processes[ ], int n, int bt[ ], int wt[ ], int
tat[ ]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
void findAverageTime(int processes[ ], int n, int bt[ ]) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst Time Waiting Time Turnaround Time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d %d %d %d\n",
processes[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);
printf("Average Turnaround Time: %.2f\n", (float)total_tat / n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n], burst_time[n];
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
processes[i] = i + 1; // Process ID
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}
findAverageTime(processes, n, burst_time);
return 0;
}
Output
Enter the number of processes: 5
Enter burst time for each process:
Process 1: 4
Process 2: 3
Process 3: 1
Process 4: 2
Process 5: 5
Processes Burst Time Waiting Time Turnaround Time
1 4 0 4
2 3 4 7
3 1 7 8
4 2 8 10
5 5 10 15
Average Waiting Time: 5.80
Average Turnaround Time: 8.80
● Shortest Job First (SJF)
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
wt[0] = 0; // Waiting time for the first process is 0
for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
}
void findTurnAroundTime(int processes[ ], int n, int bt[ ], int wt[ ], int
tat[ ]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
void sortProcessesByBurstTime(int processes[], int n, int bt[]) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (bt[j] > bt[j + 1]) {
int temp = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = temp;
int tempP = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = tempP;
}
}
}
}
void findAverageTime(int processes[ ], int n, int bt[ ]) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
sortProcessesByBurstTime(processes, n, bt);
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst Time Waiting Time Turnaround Time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d %d %d %d\n",
processes[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);
printf("Average Turnaround Time: %.2f\n", (float)total_tat / n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n], burst_time[n];
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
processes[i] = i + 1; // Process ID
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}
findAverageTime(processes, n, burst_time);
return 0;
}
Output
Enter the number of processes: 5
Enter burst time for each process:
Process 1: 4
Process 2: 3
Process 3: 1
Process 4: 2
Process 5: 5
Processes Burst Time Waiting Time Turnaround Time
3 1 0 1
4 2 1 3
2 3 3 6
1 4 6 10
5 5 10 15
Average Waiting Time: 4.00
Average Turnaround Time: 7.00
● Round Robin
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int wt[], int
quantum) {
int remaining_bt[n];
for (int i = 0; i < n; i++) {
remaining_bt[i] = bt[i];
}
int time = 0;
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (remaining_bt[i] > 0) {
done = 0;
if (remaining_bt[i] > quantum) {
time += quantum; // Increase the time
remaining_bt[i] -= quantum;
} else {
time += remaining_bt[i];
wt[i] = time - bt[i];
remaining_bt[i] = 0;
}
}
}
if (done == 1) {
break; }
}
}
void findTurnAroundTime(int processes[ ], int n, int bt[ ], int wt[ ], int
tat[ ]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
void findAverageTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n];
int total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst Time Waiting Time Turnaround Time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d %d %d %d\n",
processes[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);
printf("Average Turnaround Time: %.2f\n", (float)total_tat / n);
}
int main() {
int n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n], burst_time[n];
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
processes[i] = i + 1; // Process ID
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
findAverageTime(processes, n, burst_time, quantum);
return 0;
}
Output
Enter the number of processes: 3
Enter burst time for each process:
Process 1: 4
Process 2: 3
Process 3: 3
Enter time quantum: 4
Processes Burst Time Waiting Time Turnaround Time
1 4 0 4
2 3 4 7
3 3 7 10
Average Waiting Time: 3.67
Average Turnaround Time: 7.00
● priority scheduling - non preemptive
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int wt[], int
priority[]) {
int completed[n];
for (int i = 0; i < n; i++) {
completed[i] = 0; // Mark all processes as not completed
}
int time = 0;
for (int count = 0; count < n; count++) {
int min_priority_index = -1;
for (int i = 0; i < n; i++) {
if (!completed[i] && (min_priority_index == -1 || priority[i] <
priority[min_priority_index])) {
min_priority_index = i;
}
}
if (min_priority_index != -1) {
wt[min_priority_index] = time;
time += bt[min_priority_index];
completed[min_priority_index] = 1;
}
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int
tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
void findAverageTime(int processes[], int n, int bt[], int priority[]) {
int wt[n], tat[n];
int total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, priority);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst Time Priority Waiting Time Turnaround
Time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d %d %d %d %d\n",
processes[i], bt[i], priority[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);
printf("Average Turnaround Time: %.2f\n", (float)total_tat / n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n], burst_time[n], priority[n];
printf("Enter burst time and priority for each process:\n");
for (int i = 0; i < n; i++) {
processes[i] = i + 1; // Process ID
printf("Process %d:\n", i + 1);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
printf("Priority: ");
scanf("%d", &priority[i]);
}
findAverageTime(processes, n, burst_time, priority);
return 0;
}
Output
Enter the number of processes: 5
Enter burst time and priority for each process:
Process 1:
Burst Time: 10
Priority: 3
Process 2:
Burst Time: 1
Priority: 1
Process 3:
Burst Time: 2
Priority: 4
Process 4:
Burst Time: 1
Priority: 5
Process 5:
Burst Time: 5
Priority: 2
Processes Burst Time Priority Waiting Time Turnaround Time
1 10 3 6 16
2 1 1 0 1
3 2 4 16 18
4 1 5 18 19
5 5 2 1 6
Average Waiting Time: 8.20
Average Turnaround Time: 12.00
● Priority Scheduling with Round Robin
#include <stdio.h>
#include <stdbool.h>
scheduling
void findWaitingTime(int processes[], int n, int bt[], int wt[], int
priority[], int quantum) {
int remaining_bt[n];
bool completed[n];
for (int i = 0; i < n; i++) {
remaining_bt[i] = bt[i];
completed[i] = false;
wt[i] = 0;
}
int time = 0;
for (int pr = 1; ; pr++) {
bool process_found = false;
for (int i = 0; i < n; i++) {
if (!completed[i] && priority[i] == pr) {
process_found = true;
if (remaining_bt[i] > quantum) {
time += quantum;
remaining_bt[i] -= quantum;
} else {
time += remaining_bt[i];
wt[i] = time - bt[i];
remaining_bt[i] = 0;
completed[i] = true;
}
}
}
if (!process_found) {
bool all_completed = true;
for (int i = 0; i < n; i++) {
if (!completed[i]) {
all_completed = false;
break;
}
}
if (all_completed) break;
}
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int
tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
void findAverageTime(int processes[ ], int n, int bt[ ], int priority[ ], int
quantum) {
int wt[n], tat[n];
int total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, priority, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst Time Priority Waiting Time Turnaround
Time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d %d %d %d %d\n",
processes[i], bt[i], priority[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);
printf("Average Turnaround Time: %.2f\n", (float)total_tat / n);
}
int main() {
int n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n], burst_time[n], priority[n];
printf("Enter burst time and priority for each process:\n");
for (int i = 0; i < n; i++) {
processes[i] = i + 1; // Process ID
printf("Process %d:\n", i + 1);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
printf("Priority: ");
scanf("%d", &priority[i]);
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
findAverageTime(processes, n, burst_time, priority, quantum);
return 0;
}
Output
Enter the number of processes: 4
Enter burst time and priority for each process:
Process 1:
Burst Time: 8
Priority: 2
Process 2:
Burst Time: 4
Priority: 1
Process 3:
Burst Time: 9
Priority: 2
Process 4:
Burst Time: 5
Priority: 3
Enter time quantum: 3
Processes Burst Time Priority Waiting Time Turnaround Time
1 8 2 6 14
2 4 1 0 4
3 9 2 14 23
4 5 3 14 19
Average Waiting Time: 8.50
Average Turnaround Time: 15.00