JAI NARAIN COLLEGE OF TECHNOLOGY BHOPAL
Department of Computer Science and Engineering
Subject : Operating System
Subject Code: CS-405
Write the following six experiments in a sessional
paper and submit before 25-April-2024.
1. Write a program to implement FCFS CPU scheduling algorithm.
2. Write a program to implement the SJF CPU scheduling algorithm.
3. Write a program to implement Priority CPU Scheduling algorithm.
4. Write a program to implement the Round Robin CPU scheduling algorithm.
5. Write a program to compare various CPU Scheduling Algorithms over different Scheduling
Criteria.
6. Write a program to implement & Compare various Disk & Drum scheduling Algorithms
All experiments contain the following Section:
1.Theory:
2.Algorithm:
3.Program:
4.Input:
5.Output:
6. Time Complexity
Sample experiment
Experiment 1
Write a program to implement FCFS CPU scheduling algorithm.
Theory:
FCFS (First-Come, First-Served) CPU scheduling is one of the simplest scheduling
algorithms. In FCFS, the processes are executed in the order they arrive in the ready
queue. The process that arrives first gets executed first by the CPU. FCFS is a
non-preemptive algorithm, meaning once a process starts executing, it continues
until it finishes or gets blocked.
Algorithm:
1. Start with an empty ready queue.
2. As processes arrive, add them to the end of the ready queue.
3. Execute the processes in the order they arrived. That is, remove the process
from the front of the ready queue and execute it until it finishes.
4. Calculate waiting time and turnaround time for each process.
Program:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
bool compareArrivalTime(Process p1, Process p2) {
return p1.arrival_time < p2.arrival_time;
}
void fcfs_scheduling(vector<Process>& processes) {
int n = processes.size();
int current_time = 0;
int total_waiting_time = 0;
cout << "FCFS Scheduling:" << endl;
cout << "Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time"
<< endl;
for (int i = 0; i < n; i++) {
processes[i].waiting_time = max(0, current_time - processes[i].arrival_time);
processes[i].turnaround_time = processes[i].waiting_time +
processes[i].burst_time;
current_time += processes[i].burst_time;
total_waiting_time += processes[i].waiting_time;
cout << processes[i].pid << "\t\t" << processes[i].arrival_time << "\t\t"
<< processes[i].burst_time << "\t\t" << processes[i].waiting_time << "\t\t"
<< processes[i].turnaround_time << endl;
}
double average_waiting_time = (double)total_waiting_time / n;
cout << "\nAverage Waiting Time: " << average_waiting_time << endl;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
cout << "Enter arrival time and burst time for each process:" << endl;
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
cout << "Process " << i + 1 << ":" << endl;
cout << "Arrival Time: ";
cin >> processes[i].arrival_time;
cout << "Burst Time: ";
cin >> processes[i].burst_time;
}
// Sort processes based on arrival time
sort(processes.begin(), processes.end(), compareArrivalTime);
// Perform FCFS scheduling
fcfs_scheduling(processes);
return 0;
}
Input:
● Enter the number of processes: 5
● Enter arrival time and burst time for each process:
● Process 1:
● Arrival Time: 3
● Burst Time: 4
● Process 2:
● Arrival Time: 5
● Burst Time: 3
● Process 3:
● Arrival Time: 0
● Burst Time: 2
● Process 4:
● Arrival Time: 5
● Burst Time: 1
● Process 5:
● Arrival Time: 4
● Burst Time: 3
Output:
Process ID Arrival Time Burst Time Waiting Turnaround
Time Time
P1 3 4 0 4
P2 5 3 5 8
P3 0 2 0 2
P4 5 1 8 9
P5 4 3 3 6