3
1

Created new testing branch

This commit is contained in:
K 2024-08-21 01:15:20 +05:30
parent 79b9602edc
commit b5f96daec6
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
5 changed files with 0 additions and 393 deletions

View File

@ -1,80 +0,0 @@
#include<iostream>
using namespace std;
struct fcfs
{
int burst, arrival, id, completion, waiting, turnaround, response;
};
fcfs meh[30];
class FCFS
{
public:
int n;
void fcfsIn(){
cout<<"\nEnter number of processes: ";
cin>>n;
for(int i = 0; i < n; i++){
cout<<"\nEnter arrival time of P"<<i<<": ";
cin>>meh[i].arrival;
cout<<"\nEnter burst time of P"<<i<<": ";
cin>>meh[i].burst;
meh[i].id = i;
}
cout<<"\n | Arrival | Burst\n";
for(int j = 0; j < n; j++) {
cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n";
}
}
void process() {
cout<<"\nSequence of processes is: ";
int currentTime = 0;
for(int i = 0; i < n; i++){
if(currentTime < meh[i].arrival){
while(currentTime < meh[i].arrival){
cout<<" NULL ";
currentTime++;
}
}
meh[i].response = currentTime - meh[i].arrival;
cout<<"P"<<meh[i].id<<" ";
currentTime += meh[i].burst;
meh[i].completion = currentTime;
meh[i].turnaround = meh[i].completion - meh[i].arrival;
meh[i].waiting = meh[i].turnaround - meh[i].burst;
}
}
void displayMetrics()
{
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for(int j = 0; j < n; j++) {
totalWaiting += meh[j].waiting;
totalTurnaround += meh[j].turnaround;
totalCompletion += meh[j].completion;
cout<<"P"<<j<<"| "<<meh[j].completion
<<" | "<<meh[j].waiting
<<" | "<<meh[j].turnaround
<<" | "<<meh[j].response<<"\n";
}
cout<<"\nAverage completion time: "<<totalCompletion/n;
cout<<"\nAverage waiting time: "<<totalWaiting/n;
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
}
};
int main()
{
FCFS obj;
obj.fcfsIn();
obj.process();
obj.displayMetrics();
return 0;
}

View File

@ -1,100 +0,0 @@
#include<iostream>
#include<limits.h> // for INT_MAX
using namespace std;
struct sjf {
int burst, arrival, id, completion, waiting, turnaround, response, priority;
bool active;
};
sjf meh[30];
class lesgo {
public:
int n;
void priorityIn() {
cout << "\nEnter number of processes: ";
cin >> n;
for (int i = 1; i <= n; i++) {
cout << "\nEnter arrival time of P" << i << ": ";
cin >> meh[i].arrival;
cout << "\nEnter burst time of P" << i << ": ";
cin >> meh[i].burst;
cout << "\nEnter priority of P" << i << ": ";
cin >> meh[i].priority;
meh[i].id = i;
meh[i].active = false;
}
cout << "\n | Arrival | Burst | Priority\n";
for (int j = 1; j <= n; j++) {
cout << "P" << j << " | " << meh[j].arrival << " | " << meh[j].burst << " | " << meh[j].priority << "\n";
}
}
void priorityProcess() {
int k = 0; // Current time
int completed = 0; // Number of completed processes
while (completed < n) {
int highestPriority = INT_MAX;
int selectedProcess = -1;
// Find the process with the highest priority (smallest priority number) that has arrived and is not completed
for (int i = 1; i <= n; i++) {
if (meh[i].arrival <= k && !meh[i].active && meh[i].priority < highestPriority) {
highestPriority = meh[i].priority;
selectedProcess = i;
}
}
if (selectedProcess != -1) {
// Mark the process as active
meh[selectedProcess].active = true;
// If the process is starting now, calculate response time
if (meh[selectedProcess].response == 0) {
meh[selectedProcess].response = k - meh[selectedProcess].arrival;
}
// Execute the process
k += meh[selectedProcess].burst;
meh[selectedProcess].completion = k;
meh[selectedProcess].turnaround = meh[selectedProcess].completion - meh[selectedProcess].arrival;
meh[selectedProcess].waiting = meh[selectedProcess].turnaround - meh[selectedProcess].burst;
completed++;
} else {
// If no process is ready to run, just increment time
k++;
}
}
}
void displayMetrics() {
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout << "\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for (int j = 1; j <= n; j++) {
totalWaiting += meh[j].waiting;
totalTurnaround += meh[j].turnaround;
totalCompletion += meh[j].completion;
cout << "P" << j << " | " << meh[j].completion
<< " | " << meh[j].waiting
<< " | " << meh[j].turnaround
<< " | " << meh[j].response << "\n";
}
cout << "\nAverage completion time: " << totalCompletion / n;
cout << "\nAverage waiting time: " << totalWaiting / n;
cout << "\nAverage turnaround time: " << totalTurnaround / n;
}
};
int main() {
lesgo obj;
obj.priorityIn();
obj.priorityProcess();
obj.displayMetrics();
return 0;
}

View File

@ -1,116 +0,0 @@
#include <iostream>
#include <queue>
#include <vector>
#include <iomanip> // For formatting output
using namespace std;
struct process {
int burst, arrival, id, completion, priority, waiting, turnaround, response, remainingBurst;
bool active;
};
process meh[30];
class RoundRobin {
public:
int n;
int timeQuantum;
void inputProcesses() {
cout << "\nEnter number of processes: ";
cin >> n;
for (int i = 1; i <= n; i++) {
cout << "\nEnter arrival time of P" << i << ": ";
cin >> meh[i].arrival;
cout << "\nEnter burst time of P" << i << ": ";
cin >> meh[i].burst;
cout << "\nEnter priority of P" << i << ": ";
cin >> meh[i].priority; // Priority is not used in RR, but kept here for completeness.
meh[i].id = i;
meh[i].remainingBurst = meh[i].burst;
meh[i].active = false;
}
cout << "\nEnter time quantum: ";
cin >> timeQuantum;
cout << "\n | Arrival | Burst | Priority\n";
for (int j = 1; j <= n; j++) {
cout << "P" << j << " | " << meh[j].arrival << " | " << meh[j].burst << " | " << meh[j].priority << "\n";
}
}
void roundRobinProcess() {
int k = 0; // Current time
int completed = 0; // Number of completed processes
queue<int> readyQueue;
vector<bool> isProcessed(n + 1, false); // Track whether a process has been added to the ready queue
while (completed < n) {
// Add processes that have arrived to the ready queue
for (int i = 1; i <= n; i++) {
if (meh[i].arrival <= k && !isProcessed[i]) {
readyQueue.push(i);
isProcessed[i] = true;
}
}
if (readyQueue.empty()) {
// If no process is in the queue, increment time
k++;
continue;
}
int currentProcess = readyQueue.front();
readyQueue.pop();
// Calculate response time for the process if it starts now
if (!meh[currentProcess].active) {
meh[currentProcess].response = k - meh[currentProcess].arrival;
meh[currentProcess].active = true;
}
int timeSlice = min(timeQuantum, meh[currentProcess].remainingBurst);
// Process the current process
meh[currentProcess].remainingBurst -= timeSlice;
k += timeSlice;
if (meh[currentProcess].remainingBurst == 0) {
meh[currentProcess].completion = k;
meh[currentProcess].turnaround = meh[currentProcess].completion - meh[currentProcess].arrival;
meh[currentProcess].waiting = meh[currentProcess].turnaround - meh[currentProcess].burst;
completed++;
} else {
// If the process is not finished, re-add it to the queue
readyQueue.push(currentProcess);
}
}
}
void displayMetrics() {
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout << "\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for (int j = 1; j <= n; j++) {
totalWaiting += meh[j].waiting;
totalTurnaround += meh[j].turnaround;
totalCompletion += meh[j].completion;
cout << "P" << j << " | " << setw(15) << meh[j].completion
<< " | " << setw(12) << meh[j].waiting
<< " | " << setw(15) << meh[j].turnaround
<< " | " << setw(12) << meh[j].response << "\n";
}
cout << "\nAverage completion time: " << totalCompletion / n;
cout << "\nAverage waiting time: " << totalWaiting / n;
cout << "\nAverage turnaround time: " << totalTurnaround / n;
}
};
int main() {
RoundRobin obj;
obj.inputProcesses();
obj.roundRobinProcess();
obj.displayMetrics();
return 0;
}

View File

@ -1,97 +0,0 @@
#include<iostream>
#include<limits.h> // for INT_MAX
using namespace std;
struct sjf{
int burst, arrival, id, completion, waiting, turnaround, response;
bool active;
};
sjf meh[30];
class lesgo{
public:
int n;
void sjfIn(){
cout<<"\nEnter number of processes: ";
cin>>n;
for(int i = 1; i <= n; i++){
cout<<"\nEnter arrival time of P"<<i<<": ";
cin>>meh[i].arrival;
cout<<"\nEnter burst time of P"<<i<<": ";
cin>>meh[i].burst;
meh[i].id = i;
meh[i].active = false;
}
cout<<"\n | Arrival | Burst\n";
for(int j = 1; j <= n; j++) {
cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n";
}
}
void sjfProcess(){
int k = 0;
int completed = 0;
cout<<"\nSequence of processes is: ";
while(completed < n){
int burst1 = INT_MAX; // Initialize to the maximum possible value
int iddd = -1;
for(int i = 1; i <= n; i++){
if(meh[i].arrival <= k && meh[i].burst > 0){
if(meh[i].burst < burst1){
burst1 = meh[i].burst;
iddd = i;
}
}
}
if(iddd != -1){
// Mark the process as active
if(!meh[iddd].active) {
meh[iddd].response = k - meh[iddd].arrival;
meh[iddd].active = true;
}
cout<<"P"<<iddd<<" ";
meh[iddd].burst--;
k++;
if(meh[iddd].burst == 0){
meh[iddd].completion = k;
meh[iddd].turnaround = meh[iddd].completion - meh[iddd].arrival;
meh[iddd].waiting = meh[iddd].turnaround - (meh[iddd].response + 1); // +1 for the final unit burst time
completed++;
}
} else{
k++;
}
}
}
void displayMetrics(){
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for(int j = 1; j <= n; j++) {
totalWaiting += meh[j].waiting;
totalTurnaround += meh[j].turnaround;
totalCompletion += meh[j].completion;
cout<<"P"<<j<<"| "<<meh[j].completion
<<" | "<<meh[j].waiting
<<" | "<<meh[j].turnaround
<<" | "<<meh[j].response<<"\n";
}
cout<<"\nAverage completion time: "<<totalCompletion/n;
cout<<"\nAverage waiting time: "<<totalWaiting/n;
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
}
};
int main(){
lesgo obj;
obj.sjfIn();
obj.sjfProcess();
obj.displayMetrics();
return 0;
}