OS Assignment
OS Assignment
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;
};
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;
};
// 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;
}
}
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>
struct Process {
int id, arrivalTime, burstTime, priority, remainingTime;
int completion, turnaround, waiting, startTime;
bool completed;
};
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;
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 << "\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 :
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int currentTime = 0;
while (!q.empty()) {
int i = q.front();
q.pop();
if (remainingTime[i] > 0)
q.push(i);
else {
turnaroundTime[i] = currentTime;
waitingTime[i] = turnaroundTime[i] - burstTime[i];
}
}
}
int currentTime = 0;
for (int i : queue) {
waitingTime[i] = currentTime;
turnaroundTime[i] = waitingTime[i] + burstTime[i];
currentTime += burstTime[i];
}
}
waitingTime[i] = currentTime;
turnaroundTime[i] = waitingTime[i] + burstTime[i];
currentTime += burstTime[i];
}
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
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);
}
return 0;
}
Output :