Objective
The objective of this lab is to understand and implement various CPU scheduling algorithms including FCFS (First Come First Serve), SJF (Shortest Job First), Round Robin, and Priority Scheduling in C programming language.
Overview
In this lab, we'll create a C program that simulates four different CPU scheduling algorithms. The program will calculate and display the turnaround time and waiting time for a set of predefined processes using these algorithms.
#include<stdio.h>
// Structure to represent a process
struct Process {
int pid; // Process ID
int burst_time; // Burst time
int arrival_time; // Arrival time
int priority; // Priority
};
// Function to calculate FCFS (First Come First Serve) scheduling
void FCFS(struct Process processes[], int n) {
int waiting_time = 0, turnaround_time = 0;
printf("\nFCFS Scheduling:\n");
printf("Process\t Waiting Time\t Turnaround Time\n");
for (int i = 0; i < n; i++) {
turnaround_time += processes[i].burst_time;
printf("%d\t %d\t\t %d\n", processes[i].pid, waiting_time, turnaround_time);
waiting_time += processes[i].burst_time;
}
printf("Average Waiting Time: %.2f\n", (float)waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (float)turnaround_time / n);
}
// Function to calculate SJF (Shortest Job First) scheduling
void SJF(struct Process processes[], int n) {
// Sort processes based on burst time (shortest burst time first)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].burst_time > processes[j + 1].burst_time) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
int waiting_time = 0, turnaround_time = processes[0].burst_time;
printf("\nSJF Scheduling:\n");
printf("Process\t Waiting Time\t Turnaround Time\n");
printf("%d\t %d\t\t %d\n", processes[0].pid, waiting_time, turnaround_time);
for (int i = 1; i < n; i++) {
waiting_time += processes[i - 1].burst_time;
turnaround_time += processes[i].burst_time;
printf("%d\t %d\t\t %d\n", processes[i].pid, waiting_time, turnaround_time);
}
printf("Average Waiting Time: %.2f\n", (float)waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (float)turnaround_time / n);
}
// Function to calculate Round Robin scheduling
void RoundRobin(struct Process processes[], int n, int quantum) {
int remaining_burst_time[n];
int total_waiting_time = 0, total_turnaround_time = 0;
// Initialize remaining burst times
for (int i = 0; i < n; i++)
remaining_burst_time[i] = processes[i].burst_time;
int time = 0;
printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum);
printf("Process\t Waiting Time\t Turnaround Time\n");
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (remaining_burst_time[i] > 0) {
done = 0;
if (remaining_burst_time[i] > quantum) {
time += quantum;
remaining_burst_time[i] -= quantum;
} else {
time += remaining_burst_time[i];
total_turnaround_time += time - processes[i].arrival_time;
total_waiting_time += time - processes[i].arrival_time - processes[i].burst_time;
remaining_burst_time[i] = 0;
printf("%d\t %d\t\t %d\n", processes[i].pid, time - processes[i].arrival_time - processes[i].burst_time, time - processes[i].arrival_time);
}
}
}
if (done == 1)
break;
}
printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}
// Function to calculate Priority scheduling
void Priority(struct Process processes[], int n) {
// Sort processes based on priority (higher priority first)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].priority > processes[j + 1].priority) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
int waiting_time = 0, turnaround_time = processes[0].burst_time;
printf("\nPriority Scheduling:\n");
printf("Process\t Waiting Time\t Turnaround Time\n");
printf("%d\t %d\t\t %d\n", processes[0].pid, waiting_time, turnaround_time);
for (int i = 1; i < n; i++) {
waiting_time += processes[i - 1].burst_time;
turnaround_time += processes[i].burst_time;
printf("%d\t %d\t\t %d\n", processes[i].pid, waiting_time, turnaround_time);
}
printf("Average Waiting Time: %.2f\n", (float)waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (float)turnaround_time / n);
}
int main() {
int n = 4; // Number of processes
int quantum = 2; // Quantum for Round Robin
struct Process processes[n];
// Initialize processes (PID, Burst Time, Arrival Time, Priority)
processes[0] = (struct Process){1, 6, 1, 3};
processes[1] = (struct Process){2, 8, 1, 1};
processes[2] = (struct Process){3, 7, 2, 4};
processes[3] = (struct Process){4, 3, 3, 2};
FCFS(processes, n);
SJF(processes, n);
RoundRobin(processes, n, quantum);
Priority(processes, n);
return 0;
}
Process | Waiting Time | Turnaround Time | Burst Time |
---|---|---|---|
1 | 0 | 6 | 6 |
2 | 6 | 14 | 8 |
3 | 14 | 21 | 7 |
4 | 21 | 24 | 3 |
For SJF:
Process | Waiting Time | Turnaround Time | Burst Time |
---|---|---|---|
4 | 0 | 3 | 3 |
1 | 3 | 9 | 6 |
3 | 9 | 16 | 7 |
2 | 16 | 24 | 8 |
For Round Robin (Quantum = 2):
Process | Waiting Time | Turnaround Time | Burst Time |
---|---|---|---|
4 | 3 | 6 | 3 |
1 | 10 | 16 | 6 |
3 | 13 | 20 | 7 |
2 | 15 | 23 | 8 |
For Priority:
Process | Waiting Time | Turnaround Time | Burst Time |
---|---|---|---|
2 | 0 | 8 | 8 |
4 | 8 | 11 | 3 |
1 | 11 | 17 | 6 |
3 | 17 | 24 | 7 |
FCFS Scheduling
Grant Chart:
|---------------------------------------------|
| P1 | P2 | P3 | P4 | |---------|-----------------------|-------|------| | 0 | 6
| 14 | 21 | 24 |
SJF Scheduling
Grant Chart:
|---------------------------------------------|
| P1 | P4 | P3 | P2 | |---------|-----------------------|-------|------| | 0 | 6
| 9 | 16 | 24 |
Round Robin
Scheduling (Quantum = 2) Grant Chart:
|-------------------------------------------------------|
| P1 | P2 | P3 | P4 | P1 | P2 | P3 | P2 | |------|------|------|------|------|------|------|-----|
| 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 |
Priority Scheduling
Grant Chart:
Comments
Post a Comment