10
0

Added all codes.

This commit is contained in:
K
2025-01-07 22:00:12 +05:30
parent 5197bcdb76
commit c45d47e193
31 changed files with 1732 additions and 0 deletions
@@ -0,0 +1,80 @@
#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;
}
@@ -0,0 +1,100 @@
#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;
}
@@ -0,0 +1,110 @@
#include<iostream>
#include<queue>
using namespace std;
struct Process{
int id, burst, arrival, remaining, completion, waiting, turnaround, response;
};
Process processes[30];
class RoundRobin{
public:
int n, timeQuantum;
void input(){
cout<<"\nEnter number of processes: ";
cin>>n;
for(int i = 0; i < n; i++){
cout<<"\nEnter arrival time of P"<<i<<": ";
cin>>processes[i].arrival;
cout<<"Enter burst time of P"<<i<<": ";
cin>>processes[i].burst;
processes[i].id = i;
processes[i].remaining = processes[i].burst;
processes[i].response = -1;
}
cout<<"\nEnter time quantum: ";
cin>>timeQuantum;
}
void process(){
int currentTime = 0;
queue<int> q;
bool inQueue[30] = {false};
int completedProcesses = 0;
for(int i = 0; i < n; i++){
if(processes[i].arrival <= currentTime){
q.push(i);
inQueue[i] = true;
}
}
while(completedProcesses < n){
if(q.empty()){
currentTime++;
for(int i = 0; i < n; i++){
if(!inQueue[i] && processes[i].arrival <= currentTime){
q.push(i);
inQueue[i] = true;
}
}
continue;
}
int idx = q.front();
q.pop();
if(processes[idx].response == -1){
processes[idx].response = currentTime - processes[idx].arrival;
}
if(processes[idx].remaining <= timeQuantum){
currentTime += processes[idx].remaining;
processes[idx].remaining = 0;
processes[idx].completion = currentTime;
processes[idx].turnaround = processes[idx].completion - processes[idx].arrival;
processes[idx].waiting = processes[idx].turnaround - processes[idx].burst;
completedProcesses++;
}else{
currentTime += timeQuantum;
processes[idx].remaining -= timeQuantum;
}
for(int i = 0; i < n; i++){
if(!inQueue[i] && processes[i].arrival <= currentTime && processes[i].remaining > 0){
q.push(i);
inQueue[i] = true;
}
}
if(processes[idx].remaining > 0){
q.push(idx);
}
}
}
void displayMetrics(){
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for(int i = 0; i < n; i++){
totalWaiting += processes[i].waiting;
totalTurnaround += processes[i].turnaround;
totalCompletion += processes[i].completion;
cout<<"P"<< processes[i].id<<" | "<<processes[i].completion<<" | "<< processes[i].waiting<<" | "<<processes[i].turnaround<<" | "<<processes[i].response<<"\n";
}
cout<<"\nAverage completion time: "<<totalCompletion/n;
cout<<"\nAverage waiting time: "<<totalWaiting/n;
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
}
};
int main(){
RoundRobin rr;
rr.input();
rr.process();
rr.displayMetrics();
return 0;
}
@@ -0,0 +1,82 @@
#include <iostream>
using namespace std;
struct sjf{
int id, arr, bur, comp, turn, wait, orgBur;
};
sjf meh[30];
class SJF{
public:
int n;
void getIn()
{
cout<<"Enter number of processes: ";
cin>>n;
for(int i = 0; i < n; i++){
cout<<"\nEnter arrival time for process "<<i<<": ";
cin>>meh[i].arr;
cout<<"Enter burst time for process "<<i<<": ";
cin>>meh[i].bur;
meh[i].orgBur = meh[i].bur;
meh[i].id = i;
}
}
void process()
{
int k = 0;
int completed = 0;
cout<<"\nSequence of processes is: ";
while(completed < n){
int burst = 999;
int idd = -1;
for(int i = 0; i < n; i++){
if(meh[i].arr <= k && meh[i].bur > 0){
if(meh[i].bur < burst){
burst = meh[i].bur;
idd = i;
}
}
}
if(idd != -1){
cout<<"P"<<idd<<" ";
k++;
meh[idd].bur--;
if(meh[idd].bur == 0){
meh[idd].comp = k;
meh[idd].turn = meh[idd].comp - meh[idd].arr;
meh[idd].wait = meh[idd].turn - meh[idd].orgBur;
completed++;
}
} else{
k++;
}
}
}
void display()
{
double turn = 0, comp = 0, wait = 0;
cout<<"\n Completed | Waiting | Turnaround |";
for(int i = 0; i < n; i++){
turn += meh[i].turn;
wait += meh[i].wait;
comp += meh[i].comp;
cout<<"\nP"<<i<<"| "<<meh[i].comp<<" | "<<meh[i].wait<<" | "<<meh[i].turn;
}
cout<<"\n\nAvg completion time: "<<comp/n;
cout<<"\nAvg turnaround time: "<<turn/n;
cout<<"\nAvg waiting time: "<<wait/n;
}
};
int main()
{
SJF ob;
ob.getIn();
ob.process();
ob.display();
return 0;
}