0% found this document useful (0 votes)
58 views42 pages

Os 2020UIT3063

The document provides an index of 10 operating system concepts and algorithms that will be covered in the OS practical file, including first come first serve scheduling, shortest job scheduling, priority scheduling, preemptive priority scheduling, round robin scheduling, readers-writers problem, dining philosophers problem, bounded buffer problem, memory management algorithms, and page replacement algorithms. Code examples and explanations are provided for the first 4 algorithms: first come first serve scheduling, shortest job scheduling, priority scheduling, and preemptive priority scheduling.

Uploaded by

brainx Magic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views42 pages

Os 2020UIT3063

The document provides an index of 10 operating system concepts and algorithms that will be covered in the OS practical file, including first come first serve scheduling, shortest job scheduling, priority scheduling, preemptive priority scheduling, round robin scheduling, readers-writers problem, dining philosophers problem, bounded buffer problem, memory management algorithms, and page replacement algorithms. Code examples and explanations are provided for the first 4 algorithms: first come first serve scheduling, shortest job scheduling, priority scheduling, and preemptive priority scheduling.

Uploaded by

brainx Magic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

AARUSHI DAHIYA

2020UIT3063
OS PRACTICAL FILE

Index
1. First Come First Serve Scheduling
2. Shortest Job Scheduling Algorithm
3. Priority Scheduling Algorithm
4. Preemptive Priority Scheduling
5. Round Robin Scheduling
6. Reader – Writer’s Problem
7. Dining Philosophers’ Problem
8. Bounded Buffer Problem
9. Memory Management Algorithm
10. Page Replacement Algorithms
11. Bankers Algorithm
1. First Come First Serve Scheduling

#include<bits/stdc++.h>
#include<graphics.h>
using namespace std; int
main()
{
initwindow(800,800);
int n;
cout<<"Enter the no of processes\n";
cin>>n;
vector<pair<string,int> >v;
cout<<"Enter the process name and its burst time\n";
while(n--)
{
string str;
cin>>str;
int val;
cin>>val;
v.push_back(make_pair(str,val));
}
cout<<"Order of execution and waiting time for each process is\n";
int sum=0;
float average=0;
int x= 150;
for(int i=0;i<v.size();i++)
{

cout<<v[i].first<<" "<<sum<<endl;
average+=sum;

sum+=v[i].second;

}
sum = 0;
for(int i=0;i < v.size();i++)
{
rectangle(x , 300, x + 10*v[i].second, 400);
char intAsString [10];
outtextxy(x , 410, itoa(sum,intAsString,10));
x = x + 10*v[i].second;
sum+=v[i].second;
}
cout<<endl<<"Average waiting time is "<<average/v.size();
getch();
closegraph();
return 0;
}
2. Shortest Job Scheduling Algorithm
#include <graphics.h>

#include <bits/stdc++.h>

#define ll long long

#define f(i, n) for (int i = 0; i < n; i++)

using namespace std;

void DDA(float x1, float y1, float x2, float y2, int i, int buffer=0)
{
float dx = x2 - x1;
float dy = y2 - y1;
// Finding slope
float m=dy/dx;
float steps;
if(abs(m)<=1)
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}

float Delx=dx/steps;
float Dely=dy/steps;
float x=x1, y=y1;
putpixel(round(x),round(y),WHITE);
for(int k=1;k<=steps;k++)
{
x=x+ Delx;
y=y+ Dely;
putpixel(round(x),round(y),WHITE);
if(buffer)
{
putpixel(round(x),round(y),WHITE);
delay(40);
}
}
}

void makeRect( int x, int y,int i, int t)//t is thickness i,e, time
{
{
DDA(x,y,t*10+x,y,i);
DDA(x+t*10,y,t*10+x,y+50,i);
DDA(x+t*10,y+50,x,50+y,i);
DDA(x,y+50,x,y,i);
}
}
void intToString(int num, char str[100]){
if(num == 0){
str[0] = '0';
str[1] = '\0';
return;
}
int num_of_digits = 0, temp = num;
while(temp){
++num_of_digits;
temp /= 10;
}
str[num_of_digits] = '\0';
temp = num;
int i=num_of_digits-1;
while(temp){
str[i] = '0' + (temp%10);
temp /= 10;
--i;
}
}
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
initwindow(1300,800);
setcolor(WHITE);
cout<<"enter number of processes\n";
int n;
cin>>n;
int burstTimes[n];
cout<<"Enter the burst times \n";
f(i, n){
cout<<"Process "<<(i+1)<<"th :";
cin>>burstTimes[i];
}

sort(burstTimes,burstTimes+n);

int x=100, y=100;


f(i, n){
//gantt chart
makeRect(x,y,(i%8)+1,burstTimes[i]);
char c[3];
c[0]='P';
c[1]='1'+i;
outtextxy(x+5, y+10, c);
x+=burstTimes[i]*10;
}
int waitingTimes[n];
int turnaround=0;
float avgWait=0;

outtextxy(200,200,"Process waiting time");


x=100;
f(i, n){
cout<<"For process "<<(i+1)<<"\n";
// cout<<"Turnaround time = ";
turnaround += burstTimes[i];
// cout<<turnaround<<endl;

cout<<"waiting time : ";


waitingTimes[i] = (turnaround - burstTimes[i]);
cout<<waitingTimes[i];
avgWait += waitingTimes[i];
cout<<endl<<endl;

//gantt chart
makeRect(x,y,(i%8)+3,burstTimes[i]);
delay(500);
char c[100];
intToString(waitingTimes[i], c);
outtextxy(x+5, y+65, c);
x+=burstTimes[i]*10;

avgWait = avgWait/n;
cout << "Avg wait time: "<<avgWait<<endl;

/* initwindow(1000,500)
makeDiagram(waitingTimes,n); */
//cout << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
getch();
return 0;
}
3. Priority Scheduling Algorithm

#include<graphics.h>
#include<bits/stdc++.h>
using namespace std;

int main() {
priority_queue < int > pqueue;
pair<string, int> process[10];
vector<pair<string, int>> v;
int n;
cout << "enter the number of processes: "<< " ";
cin >> n;
for(int i =0; i < n; i++){
string temp;
int time, pri;
pair<string, int> temp2;
cout<<"enter the process name, burst time and priority of the program: ";
cin>> temp >> time >> pri;
temp2.first = temp;
temp2.second = time;
process[pri] = temp2;
pqueue.push(pri);
}
while (!pqueue.empty()) {
pair<string, int> temp3;
int lol;
lol = pqueue.top();
temp3 = process[lol];
v.push_back(temp3);
pqueue.pop();
}
cout<<"order- "<<endl;
int x= 150, sum =0;
initwindow(1000,1000);
for(int i = v.size() -1; i> -1; i--){
cout<<v[i].first<<" "<<v[i].second<<endl;
rectangle(x , 300, x + 10*v[i].second, 400);
char intAsString [10],intAsString2 [3];
for(int j =0;j<v[i].first.size(); j++){
intAsString2[j] = v[i].first[j];
}
intAsString2[3] ='\0';
outtextxy(x +20 , 350, intAsString2);
outtextxy(x , 410, itoa(sum,intAsString,10));
x = x + 10*v[i].second;
sum+=v[i].second;
}
getch();
closegraph();

return 0;
}
4. Preemptive Priority Scheduling
#include<iostream>
using namespace std;
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;

void drawLineDDA(float x1, float y1, float x2, float y2, int i, int buffer=0)
{
float dx = x2 - x1;
float dy = y2 - y1;
// Finding slope
float m=dy/dx;
float steps;
if(abs(m)<=1)
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}

float Delx=dx/steps;
float Dely=dy/steps;
float x=x1, y=y1;
putpixel(round(x),round(y),i);
for(int k=1;k<=steps;k++)
{
x=x+ Delx;
y=y+ Dely;
putpixel(round(x),round(y),i);
if(buffer)
{
putpixel(round(x),round(y),(k)%6+1);
delay(40);
}
}
}

void drawRectangle( int x, int y,int i, int t)//t is thickness i,e, time
{

{
drawLineDDA(x,y,t*10+x,y,i);
drawLineDDA(x+t*10,y,t*10+x,y+50,i);
drawLineDDA(x+t*10,y+50,x,50+y,i);
drawLineDDA(x,y+50,x,y,i);
}
}

void int_to_string(int num, char str[100]){


if(num == 0){
str[0] = '0';
str[1] = '\0';
return;
}
int num_of_digits = 0, temp = num;
while(temp){
++num_of_digits;
temp /= 10;
}
str[num_of_digits] = '\0';
temp = num;
int i=num_of_digits-1;
while(temp){
str[i] = '0' + (temp%10);
temp /= 10;
--i;
}
}
class Process
{
public:
int burst_Time;
int process_num;
int arrival_time=0;
int org_arrival_time;
int org_burst_time;
int priority;
};

bool ValueCmp1(Process const & a, Process const & b)


{
if(a.arrival_time<b.arrival_time)
return 1;
else if(a.arrival_time>b.arrival_time)
return 0;
else
return a.priority<b.priority;
}
bool ValueCmp(Process const & a, Process const & b)
{
return a.priority < b.priority;
}
int main()
{
initwindow(1100,800);

cout<<"\n Input the number of processes :\t";


int num_task;
cin>>num_task;

cout<<"\n Enter 1 for non-preemptive and 2 for preemptive scheduling\n";


int choice;
cin>>choice;

if(choice==1)
{
Process tasks[num_task];
cout<<"\n Enter the CPU bursts of the processes and their priority:\t";

int x=100, y=100;


for(int i=0; i< num_task;i++ )
{
cout<<"\n Enter the CPU burst of process and it's priority "<<i+1<<" :\t";
cin>>tasks[i].burst_Time;
cin>>tasks[i].priority;
tasks[i].process_num=i+1;

sort(tasks,tasks+num_task,ValueCmp);
for(int i=0; i< num_task;i++ )
{
drawRectangle(x,y,(i%8)+1,tasks[i].burst_Time);
char str[3];
str[0]='P';
str[1]='0'+tasks[i].process_num;
outtextxy(x+5, y+10, str);
x+=tasks[i].burst_Time*10;
}

int waiting_time=0;
int sum_waiting_time=0;
// int turnaround_time=0;
// int sum_turnaround_time=0;
outtextxy(200,200,"Process waiting time");
x=100;
for(int i=0; i<num_task; i++)
{
sum_waiting_time+=waiting_time;
//turnaround_time=waiting_time+tasks[i];
//sum_turnaround_time+=turnaround_time;
cout<<"\n Waiting time for "<<tasks[i].process_num<<" task is:\t"<<waiting_time<<" secs";
// cout<<"\n Turnaround time for "<<i+1<<" task is:\t"<<turnaround_time<<" secs \n";
drawRectangle(x,y,(i%8)+3,tasks[i].burst_Time);
delay(1000);
char str[100];
int_to_string(waiting_time, str);
outtextxy(x+5, y+65, str);
x+=(tasks[i].burst_Time)*10;

waiting_time+=tasks[i].burst_Time;
}
cout<<"\n\n Average waiting time : \t"<<float(sum_waiting_time)/num_task<<" secs.";
getch();
}

else{
Process tasks[num_task];
cout<<"\n Enter the CPU bursts of the processes:\t";
int waiting_time=0;

int x=100, y=100;


for(int i=0; i< num_task;i++ )
{
cout<<"\n Enter the CPU burst of process and arrival time and priority "<<i+1<<" :\t";
cin>>tasks[i].burst_Time;
cin>>tasks[i].arrival_time;
cin>>tasks[i].priority;
tasks[i].process_num=i+1;
tasks[i].org_arrival_time=tasks[i].arrival_time;
tasks[i].org_burst_time=tasks[i].burst_Time;

}
sort(tasks,tasks+num_task,ValueCmp1);
char arr[100];
int_to_string(tasks[0].arrival_time, arr);
outtextxy(x+5, y+65, arr);

for(int i=0; 1;i++ )


{
if(tasks[0].burst_Time>=190000)
break;

drawRectangle(x,y,tasks[0].process_num,5);

char str[3];
str[0]='P';
str[1]='0'+tasks[0].process_num;
str[2]='\0';
outtextxy(x+1, y+10, str);
x+=50;
tasks[0].burst_Time-=1;
tasks[0].arrival_time+=1;
for(int j=0; j<num_task;j++)
if(tasks[j].arrival_time<tasks[0].arrival_time)
tasks[j].arrival_time=tasks[0].arrival_time;
char arr[100];
int_to_string(tasks[0].arrival_time, arr);
outtextxy(x+5, y+65, arr);

if(tasks[0].burst_Time==0)
{
tasks[0].burst_Time=190000;//INT_MAX ki jagh
char arr[2];
arr[0]=i+'0'+1;
arr[1]='\0';
outtextxy(x+1, y+10, arr);
// cout<<tasks[0].arrival_time<<" "<<tasks[0].org_arrival_time<<"
"<<tasks[0].org_burst_time<<endl;
cout<<"Waiting time for process "<<tasks[0].process_num <<" is
:\t"<<(tasks[0].arrival_time- tasks[0].org_arrival_time)-tasks[0].org_burst_time<<"secs"<<endl;
waiting_time+=(tasks[0].arrival_time- tasks[0].org_arrival_time)-tasks[0].org_burst_time;

tasks[0].arrival_time=190000;
tasks[0].priority=1900000;

sort(tasks,tasks+num_task,ValueCmp1);

outtextxy(100,200,"Grantt Chart (with each box denoting a second) each process denoted by
a single color");
cout<<"\n Average waiting time :\t"<<float(waiting_time)/num_task<<" secs";

}
getch();
return 0;

}
5. Round Robin Scheduling
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the number of processes\n";
cin>>n;
int a[n];
int temp[n];
cout<<"Enter the burst time of the processes\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
temp[i]=a[i];
}
int quantum;
cout<<"Enter the quantum time\n";
cin>>quantum;
int wt[n];
int t=0;
while(1)
{
bool comp = true;
for (int i = 0 ; i < n; i++)
{
if (temp[i]>0)
{
comp = false;
if (temp[i]>quantum)
{
t += quantum;
temp[i] -= quantum;
}
else
{
t = t + temp[i];
wt[i] = t - a[i];
temp[i] = 0;
}
}
}
if(comp)
{
break;
}
}
float turn=0;
cout<<"Waiting times are\n";
float wait=0;
for(int i=0;i<n;i++)
{
cout<<wt[i]<<" ";
wait+=float(wt[i]);
turn+=(float(wt[i])+float(a[i]));
}
cout<<endl;
cout<<"Turnaround times are\n";
for(int i=0;i<n;i++)
{
cout<<wt[i]+a[i]<<" ";
}
cout<<endl;
cout<<"Average waiting time is "<<wait/n<<" s"<<endl;
cout<<"Average turnaround time is "<<turn/n<<" s"<<endl;
return 0;
}
6. Reader – Writer’s Problem
#include<bits/stdc++.h>
using namespace std;
int main(){
int choice;
set<string> reader;
set<string> writer;
char ch='y';
while(ch=='y')
{
cout<<"Enter "<<endl<<" Add reader-1 \n"<<" Add writer-2 \n"<<" Remove reader-3\n";
cout<<"Remove writer-4\n";
cin>>choice;
if(choice==3)
{
if(reader.size()==0)
{
cout<<"\nThere are no readers\n";

}
else
{
cout<<"Enter reader: ";
string s;
cin>>s;
reader.erase(s);
cout<<"reader "<<s<<" was removed \n";
}
}

else if(choice==1)
{
if(writer.size()!=0){
cout<<"\nCan't add reader as the data is being written by a writer.\n";
}
else{
cout<<"Enter reader: ";
string s;
cin>>s;
reader.insert(s);
cout<<"The reader "<<s<<" was added \n";
}
}
else if(choice==4)
{
if(writer.size()==0)
{
cout<<"\nThere are no writers\n";
}
else
{
cout<<"Enter writer: ";
string s;
cin>>s;
writer.erase(s);
cout<<"The writer "<<s<<" was removed \n";
}
}
else
{
if(writer.size()!=0){
cout<<"\nCan't add writer as the data is being written by a writer.\n";
}
else if(reader.size()!=0){

cout<<"\nThe data is currently being reader by "<<reader.size()<<" readers.\n";


cout<<"remove reader to add writer.\n";
}
else{
cout<<"Enter name of writer: ";
string s;
cin>>s;
writer.insert(s);
cout<<"The writer "<<s<<" was added \n";
}
}
cout<<"press y to do again, n to exit\n";
cin>>ch;
}
return 0;
}
7. Dining Philosophers’ Problem
#include<stdio.h>

#define n 5

int compltedPhilo = 0,i;

struct fork{
int taken;
}ForkAvil[n];

struct philosp{
int left;
int right;
}Philostatus[n];

void goForDinner(int philID){


if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner\n",philID+1);

else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){

printf("Philosopher %d completed his dinner\n",philID+1);

Philostatus[philID].left = Philostatus[philID].right = 10;


int otherFork = philID-1;

if(otherFork== -1)
otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0;
printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1);
compltedPhilo++;
}
else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){
if(philID==(n-1)){
if(ForkAvil[philID].taken==0){
ForkAvil[philID].taken = Philostatus[philID].right = 1;
printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
}
}else{
int dupphilID = philID;
philID-=1;

if(philID== -1)
philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d\n",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
}
}else{
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
}
}
}else{}
}

int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

while(compltedPhilo<n){
for(i=0;i<n;i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are
%d\n\n",compltedPhilo);
}

return 0;
}
8. Bounded Buffer Problem
#include<stdio.h>
#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main()
{
int n;

void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;

case 2:if((mutex==1)&&(full!=0))
consumer();

else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}

return 0;
}

int wait(int s)
{
return (--s);
}

int signal(int s)
{
return(++s);
}

void producer()
{
mutex=wait(mutex);
full=signal(full);

empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);

}
9. Memory Management Algorithms

First Fit
#include<stdio.h>

int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");
return 0;
}

Best Fit

#include<iostream>
using namespace std;

int main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
cout<<"\n\t\t\tMemory Management Scheme - Best Fit";
cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of processes:";
cin>>np;

cout<<"\nEnter the size of the blocks:-\n";


for(i=1;i<=nb;i++)
{
cout<<"Block no."<<i<<":";
cin>>b[i];
}

cout<<"\nEnter the size of the processes :-\n";


for(i=1;i<=np;i++)
{
cout<<"Process no. "<<i<<":";
cin>>p[i];
}

for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}

fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}

cout<<"\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment";
for(i=1;i<=np && parray[i]!=0;i++)

cout<<"\n"<<i<<"\t\t"<<p[i]<<"\t\t"<<parray[i]<<"\t\t"<<b[parray[i]]<<"\t\t"<<fragment[i];

return 0;
}

Worst Fit

#include<stdio.h>

int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");
return 0;
}
Least Recently used
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k,hit=0;
cout<<"Enter number of frames\n";
cin>>n;
cout<<"Enter number of processes\n";
cin>>m;
vector<int> p(m);
vector<int> hi(m);
cout<<"Enter processes\n";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n);
for(i=0;i<n;i++){
a[i]=vector<int>(m,-1);
}
map <int, int> mp;
for(i=0;i<m;i++){
vector<pair<int,int>> c;
for(auto q: mp){
c.push_back({q.second,q.first});
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
hit++;
hi[i]=1;
mp[p[i]]=1;
hasrun=true;
break;
}
if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
hasrun=true;
break;
}
}

if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
cout<<"Process ";

for(i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<'\n';
for(i=0;i<n;i++){
cout<<"Frame "<<i<<" ";
for(j=0;j<m;j++){
if(a[i][j]==-1)
cout<<"E ";
else
cout<<a[i][j]<<" ";
}
cout<<'\n';
}

for(i=0;i<m;i++){
if(hi[i]==0)
cout<<" ";
else
cout<<hi[i]<<" ";
}
cout<<"\n";
cout<<"Hit "<<hit<<'\n'<<"Page Fault "<<m-hit<<'\n';
return 0;
}

Optimal Page Replacement


#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k;
cout<<"Enter number of frames\n";
cin>>n;
cout<<"Enter number of processes\n";
cin>>m;
vector<int> p(m);
cout<<"Enter processes\n";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n,vector<int>(m,-1));
map <int, int> mp;
for(i=0;i<m;i++){
vector<int> op;
vector<pair<int,int>> c;
for(auto q: mp){

c.push_back({q.second,q.first});
}
for(int q=i+1;q<m;q++){
for(j=0;j<n;j++){
if(a[j][i]==p[q]){
op.push_back(p[q]);
}
}
}
sort(op.begin(),op.end());
op.erase(unique(op.begin(),op.end()),op.end());
bool dontCall=true;
if(op.size()==n){
dontCall=false;
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
mp[p[i]]++;
hasrun=true;
break;
}

if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
hasrun=true;
break;
}
}
if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(dontCall==true){
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
else if(dontCall==false){
if(a[j][i]==op[op.size()-1]){

mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
int hit=0;
vector<int> hitv(m);

for(i=1;i<m;i++){
for(j=0;j<n;j++){
if(p[i]==a[j][i-1]){
hit++;
hitv[i]=1;
break;
}
}
}
cout<<"Process ";
for(i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<'\n';
for(i=0;i<n;i++){
cout<<"Frame "<<i<<" ";
for(j=0;j<m;j++){
if(a[i][j]==-1)
cout<<"E ";
else
cout<<a[i][j]<<" ";
}
cout<<'\n';
}
cout<<"HIT ";

for(i=0;i<hitv.size();i++){
if(hitv[i]==0)
cout<<" ";
else
cout<<hitv[i]<<" ";
}
cout<<"\n";
cout<<"Hit "<<hit<<'\n'<<"Page Fault "<<m-hit<<'\n';
return 0;
}
11. Bankers Algorithm

#include<iostream>
using namespace std;

const int P = 5;

const int R = 3;

void calculateNeed(int need[P][R], int maxm[P][R],


int allot[P][R])
{

for (int i = 0 ; i < P ; i++)


for (int j = 0 ; j < R ; j++)

need[i][j] = maxm[i][j] - allot[i][j];


}
bool isSafe(int processes[], int avail[], int maxm[][R],
int allot[][R])
{
int need[P][R];

calculateNeed(need, maxm, allot);

bool finish[P] = {0};

int safeSeq[P];

int work[R];
for (int i = 0; i < R ; i++)
work[i] = avail[i];

int count = 0;
while (count < P)
{
bool found = false;
for (int p = 0; p < P; p++)
{

if (finish[p] == 0)
{

int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;

if (j == R)
{
for (int k = 0 ; k < R ; k++)
work[k] += allot[p][k];

safeSeq[count++] = p;

finish[p] = 1;

found = true;
}
}
}

if (found == false)
{
cout << "System is not in safe state";
return false;
}
}

cout << "System is in safe state.\nSafe"


" sequence is: ";
for (int i = 0; i < P ; i++)
cout << safeSeq[i] << " ";

return true;
}
int main()
{
int processes[] = {0, 1, 2, 3, 4};

int avail[] = {3, 3, 2};

int maxm[][R] = {{7, 5, 3},


{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};

int allot[][R] = {{0, 1, 0},


{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}};
isSafe(processes, avail, maxm, allot);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy