0% found this document useful (0 votes)
16 views21 pages

Os1 Practical Assignment4

Topics

Uploaded by

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

Os1 Practical Assignment4

Topics

Uploaded by

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

Set A

i. Write the simulation program to implement demand paging and


show the page
scheduling and total number of page faults for the following given
page reference string.
Give input n as the number of memory frames.
Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
1) Implement FIFO
2) Implement LRU

//program for FIFO……

#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n;
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:\n");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("\n\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("\n");
}

printf("Total Page Faults: %d\n",faults);


}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
void fifo()
{
int i,j;
for(i=0;i<m;i++)
{
if(search(ref[i])==-1)
{
frames[sp] = ref[i];
sp = (sp+1)%n;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
}
}

int main()
{
accept();
fifo();
disp();
return 0;
}

/* OUTPUT:-

Enter no.of frames: 3


Enter no.of references:16
Enter reference string:
[0]=12
[1]=15
[2]=12
[3]=18
[4]=6
[5]=8
[6]=11
[7]=12
[8]=19
[9]=12
[10]=6
[11]=8
[12]=12
[13]=15
[14]=19
[15]=8
12 15 12 18 6 8 11 12 19 12 6 8 12 15 19 8

12 12 12 6 6 6 12 12 12 8 8 8 19 19
15 15 15 8 8 8 19 19 19 12 12 12 8
18 18 18 11 11 11 6 6 6 15 15 15
Total Page Faults: 14
*/
//Program for LRU……

#include<stdio.h>

int findLRU(int time[], int n)


{
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0,
time[10], flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i)
{
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
{
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
/* OUTPUT:-

Enter number of frames: 3


Enter number of pages: 16
Enter reference string:
12 15 12 18 6 8 11 12 19 12 6 8 12 15 19 8

12 -1 -1
12 15 -1
12 15 -1
12 15 18
12 6 18
8 6 18
8 6 11
8 12 11
19 12 11
19 12 11
19 12 6
8 12 6
8 12 6
8 12 15
19 12 15
19 8 15

Total Page Faults = 13 */

/* Set B:
I. Write the simulation program to implement demand paging and
show the page
scheduling and total number of page faults for the following given
page reference string.
Give input n as the number of memory frames.
Reference String : 12,15,12,18,6,8,11,12,19,12,6,8,12,15,19,8
1) Implement OPT
2) Implement MFU

//program for MFU……..

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX];

void accept()
{
int i;

printf("Enter no.of frames:");


scanf("%d", &n);

printf("Enter no.of references:");


scanf("%d", &m);

printf("Enter reference string:\n");


for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("\n\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("\n");
}

printf("Total Page Faults: %d\n",faults);


}

int search(int pno)


{
int i;

for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}

return -1;
}

int get_mfu(int sp)


{
int i,max_i,max=-9999;

i=sp;
do
{
if(count[i]>max)
{
max = count[i];
max_i = i;
}
i=(i+1)%n;
}while(i!=sp);

return max_i;
}

void mfu()
{
int i,j,k;

for(i=0;i<m && sp<n;i++)


{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
count[sp]++;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
count[k]++;
}
sp=0;
for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_mfu(sp);
frames[sp] = ref[i];
count[sp]=1;
faults++;
sp = (sp+1)%n;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
count[k]++;
}
}
int main()
{
accept();
mfu();
disp();

return 0;
}
/*

OUTPUT:-
Enter number of frames: 3
Enter no.of references:16
Enter reference string:
[0]=12
[1]=15
[2]=12
[3]=18
[4]=6
[5]=8
[6]=11
[7]=12
[8]=19
[9]=12
[10]=6
[11]=8
[12]=12
[13]=15
[14]=19
[15]=8
12 15 12 18 6 8 11 12 19 12 6 8 12 15 19 8
12 12 12 6 6 6 12 12 6 6 6 15 15 15
15 15 15 8 8 8 19 19 8 8 8 19 19
18 18 18 11 11 11 11 11 12 12 12 8
Total Page Faults: 14
*/

//program for OTP……


#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10],
flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i)
{
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < no_of_frames; ++j)
{
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k)
{
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j)
{
if(temp[j] == -1)
{
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0)
{
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
{
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}

/* OUTPUT:-

Enter number of frames: 3


Enter number of pages: 16
Enter page reference string: 12 15 12 18 6 8 11 12 19 12 6 8 12 15 19 8
12 -1 -1
12 15 -1
12 15 -1
12 15 18
12 15 6
12 8 6
12 11 6
12 11 6
12 19 6
12 19 6
12 19 6
12 19 8
12 19 8
15 19 8
15 19 8
15 19 8

Total Page Faults = 9


*/

Set C:
I. Write the simulation program to implement demand paging and
show the page
scheduling and total number of page faults for the following given
page reference string.
Give input n as the number of memory frames.
Reference String: 2,5,2,8,5,4,1,2,3,2,6,1,2,5,9,8
1) Implement MRU
2) Implement Second Chance Page Replacement.
3) Least Frequently Used.

//program for LFU……


#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 Fault = %d",hit);
return 0;
}

/* OUTPUT:-

Enter no of frames : 3
Enter no of pages : 16
Enter page no :
2528541232612598

2 -1 -1
2 5 -1
2 5 -1
258
258
254
251
251
253
253
256
251
251
251
259
258
Page Fault = 6

*/

You might also like