First come First serve:
Code:
#include <stdio.h>
int pid[20], waiting_time[20], turnaround_time[20], burst_time[20], n;
void calculate_waiting_time() {
waiting_time[0] = 0;
for (int i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}
}
void calculate_turnaround_time() {
for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
}
}
int main() {
printf("Enter the total number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
pid[i] = i;
printf("Enter burst time for process %d: ", i);
scanf("%d", &burst_time[i]);
}
calculate_waiting_time();
calculate_turnaround_time();
printf("\nProcess ID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", pid[i], burst_time[i], waiting_time[i], turnaround_time[i]);
}
return 0;
}
Shortest job :
#include <stdio.h>
int pid[20], waiting_time[20], turnaround_time[20], burst_time[20], n;
void sort_by_burst_time() {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (burst_time[j] > burst_time[j + 1]) {
int temp = burst_time[j];
burst_time[j] = burst_time[j + 1];
burst_time[j + 1] = temp;
temp = pid[j];
pid[j] = pid[j + 1];
pid[j + 1] = temp;
}
}
}
}
void calculate_waiting_time() {
waiting_time[0] = 0;
for (int i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}
}
void calculate_turnaround_time() {
for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
}
}
int main() {
printf("Enter the total number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
pid[i] = i;
printf("Enter burst time for process %d: ", i);
scanf("%d", &burst_time[i]);
}
sort_by_burst_time();
calculate_waiting_time();
calculate_turnaround_time();
printf("\nProcess ID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", pid[i], burst_time[i], waiting_time[i], turnaround_time[i]);
}
return 0;
}
Round Robin:
#include <stdio.h>
int pid[20], waiting_time[20], turnaround_time[20], burst_time[20], n;
void round_robin(int quantum) {
int remaining_time[20], time = 0, done;
for (int i = 0; i < n; i++) {
remaining_time[i] = burst_time[i];
waiting_time[i] = 0;
}
do {
done = 1;
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0) {
done = 0;
if (remaining_time[i] > quantum) {
time += quantum;
remaining_time[i] -= quantum;
} else {
time += remaining_time[i];
waiting_time[i] = time - burst_time[i];
remaining_time[i] = 0;
}
}
}
} while (!done);
for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
}
}
int main() {
int quantum;
printf("Enter the total number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
pid[i] = i;
printf("Enter burst time for process %d: ", i);
scanf("%d", &burst_time[i]);
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
round_robin(quantum);
printf("\nProcess ID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", pid[i], burst_time[i], waiting_time[i], turnaround_time[i]);
}
return 0;
}