OPERATING SYSTEM
ASSIGNMENT
NIT KURUKSHETRA
NAME : NASIB
ROLL NO : 123103058
BRANCH : IT – A
SECTION : 04
Question 1: FCFS
#include<iostream>
using namespace std;
struct Process {
int id, arrivalTime, burstTime, waitingTime, turnaroundTime,
completionTime;
};
void findCompletionTime(Process p[], int n) {
p[0].completionTime = p[0].arrivalTime + p[0].burstTime;
for(int i = 1; i < n; i++) {
if (p[i].arrivalTime > p[i-1].completionTime) {
p[i].completionTime = p[i].arrivalTime + p[i].burstTime;
} else {
p[i].completionTime = p[i-1].completionTime + p[i].burstTime;
}
}
}
void findWaitingTime(Process p[], int n) {
for(int i = 0; i < n; i++) {
p[i].waitingTime = p[i].completionTime - p[i].arrivalTime -
p[i].burstTime;
if(p[i].waitingTime < 0) p[i].waitingTime = 0;
}
}
void findTurnaroundTime(Process p[], int n) {
for(int i = 0; i < n; i++) {
p[i].turnaroundTime = p[i].completionTime - p[i].arrivalTime;
}
}
void findAverageTime(Process p[], int n) {
findCompletionTime(p, n);
findWaitingTime(p, n);
findTurnaroundTime(p, n);
float totalWaitingTime = 0, totalTurnaroundTime = 0;
cout << "\nProcess\tAT\tBT\tCT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
totalWaitingTime += p[i].waitingTime;
totalTurnaroundTime += p[i].turnaroundTime;
cout << p[i].id << "\t"
<< p[i].arrivalTime << "\t"
<< p[i].burstTime << "\t"
<< p[i].completionTime << "\t"
<< p[i].waitingTime << "\t"
<< p[i].turnaroundTime << "\n";
}
cout << "\nAverage Waiting Time: " << (totalWaitingTime / n);
cout << "\nAverage Turnaround Time: " << (totalTurnaroundTime / n) <<
endl;
}
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
cout << "Enter arrival time and burst time for each process:\n";
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Process " << i + 1 << ": ";
cin >> p[i].arrivalTime >> p[i].burstTime;
}
findAverageTime(p, n);
return 0;
}
Output:
Question 2: SJF
#include <iostream>
#include <algorithm>
using namespace std;
struct Process {
int id, arrivalTime, burstTime, waitingTime, turnaroundTime,
completionTime;
};
// Function to sort processes by Arrival Time (Tie-breaker: Burst Time)
bool compare(Process a, Process b) {
if (a.arrivalTime == b.arrivalTime)
return a.burstTime < b.burstTime;
return a.arrivalTime < b.arrivalTime;
}
void findCompletionTime(Process p[], int n) {
sort(p, p + n, compare); // Sort by Arrival Time (Primary), Burst Time
(Secondary)
bool completed[n] = {false};
int completedCount = 0, currentTime = 0;
while (completedCount < n) {
int shortestJob = -1;
int minBurstTime = 1e9;
// Find the process with the shortest burst time that has arrived
for (int i = 0; i < n; i++) {
if (!completed[i] && p[i].arrivalTime <= currentTime &&
p[i].burstTime < minBurstTime) {
shortestJob = i;
minBurstTime = p[i].burstTime;
}
}
if (shortestJob == -1) {
currentTime++; // If no process is available, move time forward
} else {
currentTime += p[shortestJob].burstTime;
p[shortestJob].completionTime = currentTime;
completed[shortestJob] = true;
completedCount++;
}
}
}
void findWaitingTime(Process p[], int n) {
for (int i = 0; i < n; i++) {
p[i].waitingTime = p[i].completionTime - p[i].arrivalTime -
p[i].burstTime;
if (p[i].waitingTime < 0) p[i].waitingTime = 0;
}
}
void findTurnaroundTime(Process p[], int n) {
for (int i = 0; i < n; i++) {
p[i].turnaroundTime = p[i].completionTime - p[i].arrivalTime;
}
}
void findAverageTime(Process p[], int n) {
findCompletionTime(p, n);
findWaitingTime(p, n);
findTurnaroundTime(p, n);
float totalWaitingTime = 0, totalTurnaroundTime = 0;
cout << "\nProcess\tAT\tBT\tCT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
totalWaitingTime += p[i].waitingTime;
totalTurnaroundTime += p[i].turnaroundTime;
cout << p[i].id << "\t"
<< p[i].arrivalTime << "\t"
<< p[i].burstTime << "\t"
<< p[i].completionTime << "\t"
<< p[i].waitingTime << "\t"
<< p[i].turnaroundTime << "\n";
}
cout << "\nAverage Waiting Time: " << (totalWaitingTime / n);
cout << "\nAverage Turnaround Time: " << (totalTurnaroundTime / n) <<
endl;
}
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
cout << "Enter arrival time and burst time for each process:\n";
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Process " << i + 1 << ": ";
cin >> p[i].arrivalTime >> p[i].burstTime;
}
findAverageTime(p, n);
return 0;
}
Output:
Question 3: Priority Scheduling
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
struct Process {
int id, arrivalTime, burstTime, priority, remainingTime;
int completion, turnaround, waiting, startTime;
bool completed;
};
bool compareArrival(const Process& a, const Process& b) {
return a.arrivalTime < b.arrivalTime;
}
void preemptivePriorityScheduling(vector<Process>& processes, double* avgTAT,
double* avgWT) {
int n = processes.size();
int completed_process = 0, maxPriority, time = 0;
int totalTAT = 0, totalWT = 0;
sort(processes.begin(), processes.end(), compareArrival);
while (completed_process < n) {
int startIndex = -1;
maxPriority = INT_MIN;
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= time &&
processes[i].remainingTime > 0) {
if (processes[i].priority > maxPriority) {
maxPriority = processes[i].priority;
startIndex = i;
} else if (processes[i].priority == maxPriority &&
processes[i].arrivalTime < processes[startIndex].arrivalTime) {
startIndex = i;
}
}
}
if (startIndex == -1) {
time++;
} else {
processes[startIndex].remainingTime--;
time++;
if (processes[startIndex].remainingTime == 0) {
processes[startIndex].completion = time;
processes[startIndex].turnaround =
processes[startIndex].completion - processes[startIndex].arrivalTime;
processes[startIndex].waiting =
processes[startIndex].turnaround - processes[startIndex].burstTime;
totalTAT += processes[startIndex].turnaround;
totalWT += processes[startIndex].waiting;
completed_process++;
}
}
}
*avgTAT = (double)totalTAT / n;
*avgWT = (double)totalWT / n;
}
void nonPreemptivePriorityScheduling(vector<Process>& processes, double*
avgTAT, double* avgWT) {
int n = processes.size();
int completed_process = 0, time = 0, maxPriority;
int totalTAT = 0, totalWT = 0;
sort(processes.begin(), processes.end(), compareArrival);
while (completed_process < n) {
int startIndex = -1;
maxPriority = INT_MIN;
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= time && !processes[i].completed) {
if (processes[i].priority > maxPriority) {
maxPriority = processes[i].priority;
startIndex = i;
} else if (processes[i].priority == maxPriority &&
processes[i].arrivalTime < processes[startIndex].arrivalTime) {
startIndex = i;
}
}
}
if (startIndex == -1) {
time++;
} else {
time += processes[startIndex].burstTime;
processes[startIndex].completion = time;
processes[startIndex].turnaround =
processes[startIndex].completion - processes[startIndex].arrivalTime;
processes[startIndex].waiting = processes[startIndex].turnaround -
processes[startIndex].burstTime;
processes[startIndex].completed = true;
totalTAT += processes[startIndex].turnaround;
totalWT += processes[startIndex].waiting;
completed_process++;
}
}
*avgTAT = (double)totalTAT / n;
*avgWT = (double)totalWT / n;
}
int main() {
int n, choice;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
cout << "Enter Process ID, Arrival Time, Burst Time, and Priority: \n";
for (int i = 0; i < n; i++) {
cin >> processes[i].id >> processes[i].arrivalTime >>
processes[i].burstTime >> processes[i].priority;
processes[i].remainingTime = processes[i].burstTime;
processes[i].completed = false;
}
cout << "Choose Scheduling Type:\n1. Preemptive Priority Scheduling\n2.
Non-Preemptive Priority Scheduling\nEnter choice: ";
cin >> choice;
double avgTAT, avgWT;
if (choice == 1) {
preemptivePriorityScheduling(processes, &avgTAT, &avgWT);
} else if (choice == 2) {
nonPreemptivePriorityScheduling(processes, &avgTAT, &avgWT);
} else {
cout << "Invalid choice!";
return 0;
}
cout << "\nP \tAT \tBT \tPriority \tCT \tTAT \tWT \n";
for (auto& p : processes) {
cout << p.id << "\t" << p.arrivalTime << "\t" << p.burstTime << "\t"
<< p.priority << "\t\t" << p.completion << "\t" << p.turnaround << "\t" <<
p.waiting << "\n";
}
cout << "\nAverage Turnaround Time: " << avgTAT << "\n";
cout << "Average Waiting Time: " << avgWT << "\n";
return 0;
}
Output :
Question 4: Multi-level queue scheduling
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
void roundRobin(queue<int> &q, int n, int burstTime[], int waitingTime[], int
turnaroundTime[]) {
int remainingTime[n], startTime[n];
for (int i = 0; i < n; i++) {
remainingTime[i] = burstTime[i];
startTime[i] = -1;
}
int currentTime = 0;
while (!q.empty()) {
int i = q.front();
q.pop();
if (startTime[i] == -1) startTime[i] = currentTime;
int executionTime = min(remainingTime[i], 1);
remainingTime[i] -= executionTime;
currentTime += executionTime;
if (remainingTime[i] > 0)
q.push(i);
else {
turnaroundTime[i] = currentTime;
waitingTime[i] = turnaroundTime[i] - burstTime[i];
}
}
}
void sjf(vector<int> &queue, int burstTime[], int waitingTime[], int
turnaroundTime[]) {
sort(queue.begin(), queue.end(), [&](int a, int b) { return burstTime[a] <
burstTime[b]; });
int currentTime = 0;
for (int i : queue) {
waitingTime[i] = currentTime;
turnaroundTime[i] = waitingTime[i] + burstTime[i];
currentTime += burstTime[i];
}
}
void fcfs(queue<int> &q, int n, int burstTime[], int waitingTime[], int
turnaroundTime[]) {
int currentTime = 0;
while (!q.empty()) {
int i = q.front();
q.pop();
waitingTime[i] = currentTime;
turnaroundTime[i] = waitingTime[i] + burstTime[i];
currentTime += burstTime[i];
}
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
int queueType[n], burstTime[n], waitingTime[n] = {0}, turnaroundTime[n] =
{0};
queue<int> highPriority;
vector<int> midPriority;
queue<int> lowPriority;
cout << "Enter Burst Time and Queue Type (1=RR, 2=SJF, 3=FCFS) for each
process:\n";
for (int i = 0; i < n; i++) {
cin >> burstTime[i] >> queueType[i];
if (queueType[i] == 1)
highPriority.push(i);
else if (queueType[i] == 2)
midPriority.push_back(i);
else
lowPriority.push(i);
}
roundRobin(highPriority, n, burstTime, waitingTime, turnaroundTime);
sjf(midPriority, burstTime, waitingTime, turnaroundTime);
fcfs(lowPriority, n, burstTime, waitingTime, turnaroundTime);
float totalWaitingTime = 0, totalTurnaroundTime = 0;
cout << "\nPId\tQueue\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
cout << i + 1 << "\t" << queueType[i] << "\t" << burstTime[i] << "\t"
<< waitingTime[i] << "\t" << turnaroundTime[i] << "\n";
}
cout << "\nAverage Waiting Time: " << totalWaitingTime / n;
cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n;
cout << "\nThroughput: " << (float)n / totalTurnaroundTime << " processes
per unit time\n";
return 0;
}
Output :