Os1 Practical Assignment4
Os1 Practical Assignment4
#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");
}
int main()
{
accept();
fifo();
disp();
return 0;
}
/* OUTPUT:-
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 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:-
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
/* 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
#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX];
void accept()
{
int i;
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");
}
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
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;
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
*/
/* OUTPUT:-
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.
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
*/