Pruned old commits from testing branch. Created a new one for archiving untested codes.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
blocks = [100,500,200,400, 300]
|
||||
processes = [110,50,410]
|
||||
|
||||
def firstfit():
|
||||
block1= blocks.copy()
|
||||
for p in processes:
|
||||
for b in block1:
|
||||
if b>=p:
|
||||
print(f"P {p} allocated to B {b}")
|
||||
block1.pop(block1.index(b))
|
||||
break
|
||||
else: print(f"P {p} not allocated")
|
||||
print(f"Blocks empty: ", block1)
|
||||
|
||||
print(blocks,"\n",processes)
|
||||
print("\n\n FF:")
|
||||
firstfit()
|
||||
|
||||
|
||||
def bestfit():
|
||||
block1= sorted(blocks)
|
||||
for p in processes:
|
||||
for b in block1:
|
||||
if b>=p:
|
||||
print(f"P {p} allocated to B {b}")
|
||||
block1.pop(block1.index(b))
|
||||
break
|
||||
else: print(f"P {p} not allocated")
|
||||
print(f"Blocks empty: ", block1)
|
||||
|
||||
print("\n\n BF:")
|
||||
bestfit()
|
||||
|
||||
'''
|
||||
Common External Q: why tha fuck was worst fit even invented?
|
||||
->coz in bestfit, we have small spaces left in all the blocks.unuseable.
|
||||
But in worstfit, we have one large space in the biggest block. so we can put in one more process if extra.
|
||||
This is called External Fragmentation...
|
||||
so, Worstfit is better than Bestfit
|
||||
Answer by Afan Shaikh'''
|
||||
def worstfit():
|
||||
block1= sorted(blocks, reverse=True)
|
||||
for p in processes:
|
||||
for b in block1:
|
||||
if b>=p:
|
||||
print(f"P {p} allocated to B {b}")
|
||||
block1.pop(block1.index(b))
|
||||
break
|
||||
else: print(f"P {p} not allocated")
|
||||
print(f"Blocks empty: ", block1)
|
||||
|
||||
print("\n\n WF:")
|
||||
worstfit()
|
||||
|
||||
'''
|
||||
NF:
|
||||
You need to know working of NF, before understanding it's code.
|
||||
start is the starting index for current iteration. at end, we do start++
|
||||
the ind= i%n wraps the index around, like a circle.
|
||||
After allocation, I make block=0 to mark allocated. Who tf is gonna cross check?
|
||||
'''
|
||||
def nextfit():
|
||||
start = 0
|
||||
n = len(blocks)
|
||||
for p in processes:
|
||||
for i in range(start, start +n):
|
||||
ind = i % n
|
||||
if blocks[ind] >= p:
|
||||
print(f"P {p} allocated to B {blocks[ind]}")
|
||||
blocks[ind] = 0
|
||||
start = (ind +1 ) % n
|
||||
break
|
||||
else: print(f'P {p} not allocated')
|
||||
a=[x for x in blocks if x>0]
|
||||
print("Blocks empty: ", a)
|
||||
|
||||
|
||||
print("\n\n NF:")
|
||||
nextfit()
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
// Assignment A4 - (MUTEX AND SEMAPHORE)
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Synchronization {
|
||||
int a[10]; // Increased buffer size to 10
|
||||
int mutex;
|
||||
int empty;
|
||||
int full;
|
||||
int in;
|
||||
int out;
|
||||
|
||||
void wait(int &x) {
|
||||
if (x > 0) x--;
|
||||
}
|
||||
|
||||
void signal(int &x) {
|
||||
x++;
|
||||
}
|
||||
|
||||
public:
|
||||
Synchronization() : mutex(1), empty(10), full(0), in(0), out(0) {}
|
||||
|
||||
void producer() {
|
||||
if (empty > 0 && mutex == 1) {
|
||||
wait(empty);
|
||||
wait(mutex);
|
||||
cout << "Data to be produced: ";
|
||||
int data;
|
||||
cin >> data;
|
||||
a[in] = data;
|
||||
in = (in + 1) % 10; // Update for new buffer size
|
||||
signal(mutex);
|
||||
signal(full);
|
||||
} else {
|
||||
cout << "Buffer is full, cannot produce!" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void consumer() {
|
||||
if (full > 0 && mutex == 1) {
|
||||
wait(full);
|
||||
wait(mutex);
|
||||
cout << "Data consumed is: " << a[out] << endl; // Show consumed data
|
||||
out = (out + 1) % 10; // Update for new buffer size
|
||||
signal(mutex);
|
||||
signal(empty);
|
||||
} else {
|
||||
cout << "Buffer is empty, cannot consume!" << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
int fchoice;
|
||||
Synchronization s;
|
||||
do {
|
||||
cout << "1. Producer\n2. Consumer\n3. Exit" << endl;
|
||||
cout << "Enter your choice: ";
|
||||
cin >> fchoice;
|
||||
switch (fchoice) {
|
||||
case 1: s.producer(); break;
|
||||
case 2: s.consumer(); break;
|
||||
case 3: break;
|
||||
default: cout << "Invalid choice!" << endl; break;
|
||||
}
|
||||
} while (fchoice != 3);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int blocks[] = {100, 500, 200, 300, 600};
|
||||
int processes[] = {212, 417, 112, 426};
|
||||
int blkSize = sizeof(blocks) / sizeof(blocks[0]);
|
||||
int prSize = sizeof(processes) / sizeof(processes[0]);
|
||||
|
||||
void FirstFit() {
|
||||
cout<<endl<<"FIRST FIT:";
|
||||
for (int i=0; i<prSize; i++) {
|
||||
bool check = false;
|
||||
for(int j=0; j<blkSize; j++) {
|
||||
if ((blocks[j] - processes[i] >= 0)) {
|
||||
blocks[j] -= processes[i];
|
||||
check = true;
|
||||
cout<<endl<<"Process "<<processes[i]<<" has been allocated to block "<<j+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (check == false) {
|
||||
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NextFit() {
|
||||
int j = 0;
|
||||
cout<<endl<<"NEXT FIT:";
|
||||
for (int i=0; i<prSize; i++) {
|
||||
bool check = false;
|
||||
for (int k=0; k<blkSize; k++) {
|
||||
if ((blocks[j] - processes[i]) >= 0) {
|
||||
blocks[j] = blocks[j] - processes[i];
|
||||
cout<<endl<<"Process "<<processes[i]<<" has been allocated to block "<<j+1;
|
||||
check = true;
|
||||
break;
|
||||
}
|
||||
j = (j + 1) % blkSize;
|
||||
}
|
||||
if (check == false) {
|
||||
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BestFit() {
|
||||
cout<<endl<<"BEST FIT:";
|
||||
for (int i=0; i<prSize; i++) {
|
||||
bool check = false;
|
||||
int k, store = 999;
|
||||
for (int j=0; j<blkSize; j++) {
|
||||
if ((blocks[j] - processes[i]) >= 0 && (blocks[j] - processes[i]) < store) {
|
||||
k = j + 1;
|
||||
store = blocks[j] - processes[i];
|
||||
check = true;
|
||||
}
|
||||
}
|
||||
if (check == false) {
|
||||
cout<<endl<<"Process "<<processes[i]<<" was not allocated to any block.";
|
||||
}
|
||||
else {
|
||||
blocks[k - 1] -= processes[i];
|
||||
cout<<endl<<"Process "<<processes[i]<<" was allocated to block "<<k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorstFit() {
|
||||
cout<<endl<<"WORST FIT:";
|
||||
for (int i=0; i<prSize; i++) {
|
||||
bool check = false;
|
||||
int k, store = -1;
|
||||
for (int j=0; j<blkSize; j++) {
|
||||
if ((blocks[j] - processes[i] >= 0 && (blocks[j] - processes[i] > store))) {
|
||||
k = j + 1;
|
||||
store = blocks[j] - processes[i];
|
||||
check = true;
|
||||
}
|
||||
}
|
||||
if (check == false) {
|
||||
cout<<endl<<"Process "<<processes[i]<<" was not allocated to any block.";
|
||||
}
|
||||
else {
|
||||
blocks[k - 1] -= processes[i];
|
||||
cout<<endl<<"Process "<<processes[i]<<" was allocated to block "<<k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main () {
|
||||
// ONLY RUN ONE FUNCTION AT A TIME.
|
||||
// FirstFit();
|
||||
// NextFit();
|
||||
BestFit();
|
||||
// WorstFit();
|
||||
return 0;
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
// Assignment-B7 - Page Replacement in C++ (LRU + Optimal)
|
||||
// BEGINNING OF CODE
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int refString[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};
|
||||
int window = 0, hit = 0, miss = 0, currentPages[4], lastUsed[40];
|
||||
int len = sizeof(refString) / sizeof(refString[0]);
|
||||
|
||||
int findLRU() {
|
||||
int min_index = 0;
|
||||
for (int i=1; i<window; i++) {
|
||||
if (lastUsed[i] < lastUsed[min_index]) {
|
||||
min_index = i;
|
||||
}
|
||||
}
|
||||
return min_index;
|
||||
}
|
||||
|
||||
void LRU() {
|
||||
for (int i=0; i<window; i++) {
|
||||
currentPages[i] = -1;
|
||||
lastUsed[i] = -1;
|
||||
}
|
||||
|
||||
for (int i=0; i<len; i++) {
|
||||
bool hitFlag = false;
|
||||
for (int j=0; j<window; j++) {
|
||||
if (currentPages[j] == refString[i]) {
|
||||
hit++;
|
||||
hitFlag = true;
|
||||
lastUsed[j] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hitFlag == false) {
|
||||
miss++;
|
||||
bool emptyFound = false;
|
||||
for (int j=0; j<window; j++) {
|
||||
if (currentPages[j] == -1) {
|
||||
emptyFound = true;
|
||||
currentPages[j] = refString[i];
|
||||
lastUsed[j] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (emptyFound == false) {
|
||||
int lru_index = findLRU();
|
||||
currentPages[lru_index] = refString[i];
|
||||
lastUsed[lru_index] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout<<endl<<"----- LRU ------";
|
||||
cout<<endl<<"Number of hits: "<<hit;
|
||||
cout<<endl<<"Number of misses: "<<miss;
|
||||
}
|
||||
|
||||
int findOptimal(int current_index) {
|
||||
int max_index = -1, farthest = current_index;
|
||||
for (int i=0; i<window; i++) {
|
||||
int j;
|
||||
for (j=current_index+1; j<len; j++) {
|
||||
if (currentPages[i] == refString[j]) {
|
||||
if (j > farthest) {
|
||||
farthest = j;
|
||||
max_index = i;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == len) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return max_index;
|
||||
}
|
||||
|
||||
void Optimal() {
|
||||
hit = 0;
|
||||
miss = 0;
|
||||
|
||||
for (int i=0; i<window; i++) {
|
||||
currentPages[i] = -1;
|
||||
}
|
||||
|
||||
for (int i=0; i<len; i++) {
|
||||
bool hitFlag = false;
|
||||
for (int j=0; j<window; j++) {
|
||||
if (currentPages[j] == refString[i]) {
|
||||
hit++;
|
||||
hitFlag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hitFlag == false) {
|
||||
miss++;
|
||||
bool emptyFound = false;
|
||||
for (int j=0; j<window; j++) {
|
||||
if (currentPages[j] == -1) {
|
||||
emptyFound = true;
|
||||
currentPages[j] = refString[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (emptyFound == false) {
|
||||
int optimal_index = findOptimal(i);
|
||||
currentPages[optimal_index] = refString[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
cout<<endl<<"----- OPTIMAL -----";
|
||||
cout<<endl<<"Number of hits: "<<hit;
|
||||
cout<<endl<<"Number of misses: "<<miss;
|
||||
}
|
||||
|
||||
void display() {
|
||||
cout<<endl<<"Current pages are:\t";
|
||||
for (int i=0; i<window; i++) {
|
||||
cout<<currentPages[i]<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cout<<endl<<"Enter window size:\t";
|
||||
cin>>window;
|
||||
LRU();
|
||||
display();
|
||||
Optimal();
|
||||
display();
|
||||
}
|
||||
// END OF CODE
|
||||
Reference in New Issue
Block a user