Anda di halaman 1dari 15

ASSIGNMENT

SUBMITTED TO: SIR ALI


PREPARED BY: FIZA HABIB
(BCS02143199) SEC-C
COURSE: OPERATING SYSTEM (LAB)
DATED: 29-DEC-2016

DEPARTMENT OF COMPUTER SCIENCE & IT

IMPLEMENTATION OF DINING PHILOSOPHERS


PROBLEM:
C++ CODE:
#include
#include
#include
#include
#include

<iostream>
<vector>
<thread>
<random>
<condition_variable>

namespace {
int sleep_for_random_time() {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(1000, 4000);
}

return dist(mt);

}
class DiningPhilosopher {
public:
DiningPhilosopher(int numOfPhilosophers_ = 5, int numOfForks_ = 5, int numOfEating_ =
3) : numOfPhilosophers(numOfPhilosophers_), numOfForks(numOfForks_),
numOfEating(numOfEating_) {
forks.push_back(new Fork);
forks.push_back(new Fork);
forks.push_back(new Fork);
forks.push_back(new Fork);
forks.push_back(new Fork);
numAllowed = numOfForks - 1;
}
~DiningPhilosopher() {
for(const auto& fork : forks) {
delete fork;
}
}
DiningPhilosopher(const DiningPhilosopher&) = delete;
DiningPhilosopher& operator = (const DiningPhilosopher&) = delete;
void StartDining() {
for(int i = 0; i < numOfPhilosophers; ++i) {
threads.push_back(std::thread(&DiningPhilosopher::Philosopher, this, i));
}
std::for_each(threads.begin(),threads.end(), std::mem_fn(&std::thread::join));
}
private:

void WaitForPermission() {
std::lock_guard<std::mutex> lock(permission);
cv.wait(permission, [this] { return numAllowed > 0; });
--numAllowed;
}
void GrantPermission() {
std::lock_guard<std::mutex> lock(permission);
++numAllowed;
if(numAllowed == 1) {
cv.notify_all();
}
}
void Think(int id) {
std::chrono::milliseconds duration(sleep_for_random_time());
std::this_thread::sleep_for(duration);
}
void Eat(int id) {
this->WaitForPermission();
forks[id]->mux.lock();
forks[(id + 1) % numOfForks]->mux.lock();
std::cout << "Philosopher "<< id << " is eating" << std::endl;
this->Think(id);
std::cout << "Philosopher "<< id << " has finished eating" << std::endl;
forks[id]->mux.unlock();
forks[(id + 1) % numOfForks]->mux.unlock();
this->GrantPermission();
}
void Philosopher(int id) {
for(int i = 0; i < numOfEating; ++i) {
this->Think(id);
this->Eat(id);
}
}

std::cout << "Philosopher " << id << " DONE" << std::endl;

struct Fork {
std::mutex mux;
};
int numOfEating;
int numOfPhilosophers;
int numAllowed;
std::vector<std::thread> threads;
std::vector<Fork*> forks;
std::mutex permission;

std::condition_variable_any cv;
};
int main()
{
DiningPhilosopher dp;
dp.StartDining();
}

return 0;

PAGE REPLACEMENT ALGORITHMS:


1. FIFO:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
int found(int x,int *l,int max)
{
for(int i=0;i<max;i++)
if(l[i]==x){return(i);}
return(-1);
}
int main()
{
cout<<"\n\nEnter the maximum number of frames in the main memory:\t";
int max;
cin>>max;
int *l=new int[max];
for(int i=0;i<max;i++)l[i]=-1;
int a,x;
int k=0,c=0,res;
cout<<"\n\nEnter the sequence of page requests(enter -1 to stop):\t";
while(1)
{
cin>>x;
if(x==-1) {cout<<"\n\n";break;}

else{
if(k<max)
{
if((res=found(x,l,max))!=-1) {cout<<"\n\npage "<<x<<" already exists in frame
"<<res<<" in MM";
cout<<"\n\nNext page:\t";}
else
{
cout<<"\n\npage "<<x<<" has been allocated a frame "<<k<<" in MM.";
l[k++]=x;
cout<<"\n\nNext page:\t";
}
}
else
{
if((res=found(x,l,max))!=-1) {cout<<"\n\npage "<<x<<" already exists in frame
"<<res<<" in MM";
cout<<"\n\nNext page:\t";}
else{
cout<<"\n\npage fault has occured";
cout<<"\n\npage "<<x<<" has been allocated frame "<<c<<" in MM by
replacing page "<<l[c];
l[c]=x;
c=(c+1)%max;
cout<<"\n\nNext page:\t";
}
}
}
}
delete[] l;
return(0);
}

OUTPUT:

2.

LEAST-FREQUENTLY USED:

#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
printf("Enter no of frames : ");

scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);
for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{

if(count[frame[j]]==count[least] &&
time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}

3. most frequently used algorithm:


#include<stdio.h>
#include<conio.h>
struct node
{
int pno,freq;
}frames[20];
int n;
int page_found(int pno)
{
int fno;
for(fno=0;fno<n;fno++)
if(frames[fno].pno==pno)
return fno;
return -1;
}
int get_free_frame()
{ int fno;
for (fno=0; fno<=n; fno++)
if (frames[fno].pno==-1)
return(fno);

return(-1);
}
int get_mfu_frame()
{
int fno;
int selfno=0;
for (fno=1; fno<n; fno++)
if(frames[fno].freq>frames[selfno].freq)
selfno=fno;
return selfno;
}
void main()
{
int p_request[]={5,8,10,14,10,9,5,10,8,5,1,10,9,12,10};
int size=15;
int page_falts=0,i,j,fno;
clrscr();
printf("\nHow many frames:"); scanf("%d",&n);
//initialize frames
for (i=0; i<n; i++)
{ frames[i].pno=-1;
frames[i].freq=0;
}
printf("\nPageNo Page Frames
Page Fault");
printf("\n---------------------------------------------------");
for(i=0;i<size;i++)
{
j=page_found(p_request[i]);
if(j==-1) //page fault occurs
{
j=get_free_frame();
if (j==-1) //no free frame - do page replacement
j=get_mfu_frame();
page_falts++;
frames[j].pno=p_request[i];
frames[j].freq=1;
printf("\n%4d\t ",p_request[i]);
for (fno=0; fno<n; fno++)
printf("%4d:%2d",frames[fno].pno,frames[fno].freq);
printf(" : YES");

}
else //page found in frame j
{
printf("\n%4d\t ",p_request[i]);
frames[j].freq++;
for (fno=0; fno<n; fno++)
printf("%4d:%2d",frames[fno].pno,frames[fno].freq);
printf(" : NO");
}
}
printf("\n-------------------------------------------------------");
printf("\n Number of Page_Falts=%d",page_falts);
getch();
}

4. the principle of optimality:


#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,l,m,n,p,c=0,s;
int a[20],b[20],q,max;
printf("enter no. of reference string: ");
scanf("%d",&n);
printf("enter size of frame: ");
scanf("%d",&m);
printf("enter the elements of ref. string: \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(j=0;j<m;j++)
b[j]=-1; //initialize all frame elements with -1
for(i=0;i<n;i++)
{
for(k=0;k<m;k++)

if(b[k]==a[i])
goto here;
for(j=0;j<m;j++)
{
if(b[j]==-1)//check if element already present in frame,if true then no page fault.
{
b[j]=a[i];
c++;
goto here;
}
}
if(j==m)
{
l=i+1,max=0;
for(j=0;j<m;j++)
{
for(s=l;s<n;s++)
{
if(a[s]==b[j])
{
if(s>max)
{
max=s;
p=j;
}
break;
}
}
if(s==n)
{
max=s;
p=j;
}
}
}

b[p]=a[i];
c++;
here: printf("\n\n");
for(k=0;k<m;k++)
printf(" %d",b[k]);
}
printf("\n No of page fault is:%d",c);
getch();
}

5. LRU
#include<iostream>
using namespace std;
class pra
{
int frame[50], frame1[50][2], pn[50], n, cnt, p, fs;
public:
pra();
void init();
void getdata();
void lru();
void optimal();
};
pra::pra()
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::init()
{
int i;
for (i = 0; i < fs; i++)
{

frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::getdata()
{
cout << "\nEnter frame size : ";
cin >> fs;
cout << "\nEnter the number of pages : ";
cin >> n;
cout << "\nEnter the page numbers : ";
for (int i = 0; i < n; i++)
{
cin >> pn[i];
}
}
void pra::lru()
{
init();
int ind = 0, fault = 0, pi = 0, i, j, fn, k;
p = 0;
cnt = 0;
int min;
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
pi = 0;
for (i = 0; i < n; i++)
{
j = 0;
if (ind > fs - 1)
ind = 0;
fault = 1;
min = 999;
while (j < fs)
{
if (frame1[j][0] == pn[pi])
{
fault = 0;
p++;

frame1[j][1] = p;
goto l2;
}
if (frame1[j][1] < min)
{
min = frame1[j][1];
fn = j;
}
j++;
}
j = 0;
while (j < fs)
{
if (frame1[j][0] == -1)
{
fault = 1;
fn = j;
goto l2;
}
j++;
}
ind++;
l2:
if (fault == 1)
{
p++;
frame1[fn][0] = pn[pi];
frame1[fn][1] = p;
cnt++;
}
cout << "\nElement is : " << pn[pi] << "-->";
pi++;
for (k = 0; k < fs; k++)
{
cout << "\t" << frame1[k][0];
}
if (fault == 1)
cout << "\t*****Page Fault******";
else
cout << "\t-----No Page Fault-----";
}
cout << "\nTotal number of page fault : " << cnt;
}
int main()
{
pra p;
int j, fault = 0, i, pi, k, fn, ind = 0, ans, ch;
p.getdata();
do

{
cout << "LRU;
Cout<< p.lru();
cout << "\nDo you want to cont. (1/0) : ";
cin >> ans;
}
while (ans == 1);
return 1;
}

NRU:

Anda mungkin juga menyukai