Priority scheduling program
#include <stdio.h>
struct Process {
int id;
int burstTime;
int priority;
int waitingTime;
int turnAroundTime;
};
void calculateWaitingTime(struct Process proc[], int n) {
proc[0].waitingTime = 0;
for (int i = 1; i < n; i++) {
proc[i].waitingTime = proc[i - 1].waitingTime + proc[i - 1].burstTime;
}
}
void calculateTurnAroundTime(struct Process proc[], int n) {
for (int i = 0; i < n; i++) {
proc[i].turnAroundTime = proc[i].waitingTime + proc[i].burstTime;
}
}
void sortProcessesByPriority(struct Process proc[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[i].priority > proc[j].priority) {
struct Process temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}
void printProcesses(struct Process proc[], int n) {
printf("\nProcess ID\tBurst Time\tPriority\tWaiting Time\tTurnaround
Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
proc[i].id, proc[i].burstTime, proc[i].priority, proc[i].waitingTime,
proc[i].turnAroundTime);
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process proc[n];
for (int i = 0; i < n; i++) {
printf("\nEnter details for Process %d:\n", i + 1);
proc[i].id = i + 1;
printf("Burst Time: ");
scanf("%d", &proc[i].burstTime);
printf("Priority: ");
scanf("%d", &proc[i].priority);
}
sortProcessesByPriority(proc, n);
calculateWaitingTime(proc, n);
calculateTurnAroundTime(proc, n);
printProcesses(proc, n);
return 0;
}
Output : Enter the number of processes: 3
Enter details for Process 1:
Burst Time: 10
Priority: 2
Enter details for Process 2:
Burst Time: 5
Priority: 1
Enter details for Process 3:
Burst Time: 8
Priority: 3
Process ID Burst Time Priority Waiting Time Turnaround Time
2 5 1 0 5
1 10 2 5 15
3 8 3 15 23
Round Robin scheduling algorithm
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {
int rem_bt[n];
for (int i = 0; i < n; i++) {
rem_bt[i] = bt[i];
}
int t = 0; // Current time
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0; // At least one process is remaining
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;
}
}
}
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 findAvgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n];
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes\tBurst Time\tWaiting Time\tTurnaround Time\n");
int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf("P%d\t\t%d\t\t%d\t\t%d\n", processes[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage waiting time = %.2f", (float)total_wt / n);
printf("\nAverage turnaround time = %.2f\n", (float)total_tat / n);
}
int main() {
int processes[] = {1, 2, 3};
int n = sizeof(processes) / sizeof(processes[0]);
int burst_time[] = {10, 5, 8};
int quantum = 2;
findAvgTime(processes, n, burst_time, quantum);
return 0;
}
Output:
Processes Burst Time Waiting Time Turnaround Time
P1 10 13 23
P2 5 10 15
P3 8 13 21
Average waiting time = 12.00
Average turnaround time = 19.67