0% found this document useful (0 votes)
41 views

Os Ex4

The document discusses and provides code examples for different memory allocation strategies including first fit, best fit, and worst fit. It also discusses page replacement algorithms like first in first out, least recently used, and optimal page replacement.

Uploaded by

sakthiek3
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)
41 views

Os Ex4

The document discusses and provides code examples for different memory allocation strategies including first fit, best fit, and worst fit. It also discusses page replacement algorithms like first in first out, least recently used, and optimal page replacement.

Uploaded by

sakthiek3
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/ 15

4.

Implement Memory Allocation Strategies


(1)First Fit
Program:
#include<stdio.h>
main()
{
int p[]={100,10,35,15,23,6,25,55,88,100},np=10;
int m[]={50,200,70,115,15},nm=5;
int i,j,k,totp=0,totm=0,totr,temp,totpgm=0;
int done[10];
for(i=0;i<np;i++)
{
done[i]=0;
totpgm+=p[i];
}
for(i=0;i<nm;i++)
totm+=m[i];
totr=totm;
printf("\nProgram \t MemoryBlock\t Remaining");
for(i=0;i<np;i++)
{
for(j=0;j<nm;j++)
{
if( m[j] >= p[i] )
{
temp=m[j];
m[j]-=p[i];
done[i]=1;
printf("\n%4d \t%4d \t%4d",p[i],temp,m[j]);
totp+=p[i];
totr-=p[i];
break;
}
}
}
for(i=0;i<np;i++)
if( !done[i] )
printf("\n%d is NOT accommodated",p[i]);
printf("\n Total Program \t=%4d",totpgm);
printf("\n Total Memory \t=%4d",totm);
printf("\n Memory remaining\t= %4d",totr);
printf("\n Memory utilization\t =%.2f",(float)totp/totm*100);
}
OUTPUT:

Program Memory Block Remaining


100 200 100
10 50 40
35 40 5
15 100 85
23 85 62
6 62 56
25 56 31
55 70 15
88 115 27
100 is NOT accommodated
Total Program = 457
Total Memory = 450
Memory remaining = 93
Memory utilization =79.33

(2) Best Fit


Program:
#include<stdio.h>
int p[]={100,10,35,15,23,6,25,55,88,100},np=10;
int m[]={50,200,70,115,15},nm=5;
int i,j,k,totp=0,totm=0,totr,temp,totpgm=0;
int done[10];
void sortincreasing()
{
int x,y,temp;
for(x=0;x<nm;x++)
{
for(y=x+1;y<nm;y++)
if( m[x] > m[y] )
{
temp=m[x];
m[x]=m[y];
m[y]=temp;
}
}
}
main()
{
for(i=0;i<np;i++)
{
done[i]=0;
totpgm+=p[i];
}
for(i=0;i<nm;i++)
totm+=m[i];
totr=totm;
printf("\nProgram \t MemoryBlock\t Remaining");
for(i=0;i<np;i++)
{
sortincreasing();
for(j=0;j<nm;j++)
{
if( m[j] >= p[i] )
{
temp=m[j];
m[j]-=p[i];
done[i]=1;
printf("\n%4d \t%4d \t%4d",p[i],temp,m[j]);
totp+=p[i];
totr-=p[i];
break;
}
}
}
for(i=0;i<np;i++)
if(!done[i])
printf("\n%d is not accommodated",p[i]);
printf("\n Total Program \t=%4d",totpgm);
printf("\n Total Memory \t=%4d",totm);
printf("\n Memory remaining\t= %4d",totr);
printf("\n Memory utilization\t =%.2f",(float)totp/totm*100);
}
OUTPUT:
Program Memory Block Remaining
100 115 15
10 15 5
35 50 15
15 15 0
23 70 47
6 15 9
25 47 22
55 200 145
88 145 57
100 is not accommodated
Total Program = 457
Total Memory = 450
Memory remaining = 93
Memory utilization =79.33

(3) Worst Fit:


Program:
#include<stdio.h>
int p[]={100,10,35,15,23,6,25,55,88,100},np=10;
int m[]={50,200,70,115,15},nm=5;
int i,j,k,totp=0,totm=0,totr,temp,totpgm=0;
int done[10];
void sortdecreasing()
{
int x,y,temp;
for(x=0;x<nm;x++)
{
for(y=x+1;y<nm;y++)
if( m[x] < m[y] )
{
temp=m[x];
m[x]=m[y];
m[y]=temp;
}
}
}
main()
{
for(i=0;i<np;i++)
{
done[i]=0;
totpgm+=p[i];
}
for(i=0;i<nm;i++)
totm+=m[i];
totr=totm;
printf("\nProgram \t MemoryBlock\t Remaining");
for(i=0;i<np;i++)
{
sortdecreasing();
for(j=0;j<nm;j++)
{
if( m[j] >= p[i] )
{
temp=m[j];
m[j]-=p[i];
done[i]=1;
printf("\n%4d \t%4d \t%4d",p[i],temp,m[j]);
totp+=p[i];
totr-=p[i];
break;
}
}
}
for(i=0;i<np;i++)
if(!done[i])
printf("\n%d is not accommodated",p[i]);
printf("\n Total Program \t=%4d",totpgm);
printf("\n Total Memory \t=%4d",totm);
printf("\n Memory remaining\t= %4d",totr);
printf("\n Memory utilization\t =%.2f",(float)totp/totm*100);
}
OUTPUT:
Program Memory Block Remaining
100 200 100
10 115 105
35 105 70
15 100 85
23 85 62
6 70 64
25 70 45
55 64 9
88 is not accommodated
100 is not accommodated
Total Program = 457
Total Memory = 450
Memory remaining = 181
Memory utilization =59.78

5. Implementation of Page Replacement Algorithms


(1) First In First Out Page Replacement Algorithm
Program:
#include<stdio.h>
main()
{
int p[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1},f[5],nf=3,n=20;
int pf=0,i,j,k=0,found;/* p[] – pages, f[] – frames ,
nf – number of frames, pf – page fault */
for(i=0;i<nf;i++)
f[i]=-1;
printf("\nPage\tMemory Frames\tStatus");
printf("\n======================");
for(i=0;i<n;i++)
{
found=0;
for(j=0;j<nf;j++)
if(f[j]==p[i]) found=1;
if(found==0)
{
f[k]=p[i];
k++;
k=k%nf;
pf++;
}
printf("\n\n\t%d",p[i]);
for(j=0;j<nf;j++)
printf("\t%d",f[j]);
printf(found?"\tHit":"\tFault");
}
printf("\nTotal Number of Page Faults=%d",pf);
}

Output:

Page Memory Frames Status


7 7 -1 -1 Fault
0 7 0 -1 Fault
1 7 0 1 Fault
2 2 0 1 Fault
0 2 0 1 Hit
3 2 3 1 Fault
0 2 3 0 Fault
4 4 3 0 Fault
2 4 2 0 Fault
3 4 2 3 Fault
0 0 2 3 Fault
3 0 2 3 Hit
2 0 2 3 Hit
1 0 1 3 Fault
2 0 1 2 Fault
0 0 1 2 Hit
1 0 1 2 Hit
7 7 1 2 Fault
0 7 0 2 Fault
1 7 0 1 Fault
Total Number of Page Faults=15
(2) Least Recently Used Page Replacement Algorithm
Program:
#include<stdio.h>
int p[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1},f[5],count[5],nf=3,n=20;
int pf=0,i,j,k,found;
main()
{
for(i=0;i<nf;i++)
{
f[i]=-1;
count[i]=-1;
}
for(i=0;i<n;i++)
{
found=0;
for(j=0;j<nf;j++)
if(f[j]==p[i])
{
found=1;
count[j]=i;
}
if(found==0)
{
k=minimum(count);
f[k]=p[i];
count[k]=i;
pf++;
}
printf("\n%d",p[i]);
for(j=0;j<nf;j++)
printf("\t%d",f[j]);
printf(found?"\tHit":"\tFault");
}
printf("\nTotal number of page fault =%d",pf);
}
int minimum(int count[])
{
int small=count[0],x,index=0;
for(x=1;x<nf;x++)
if(count[x] < small)
{
small=count[x];
index=x;
}
return index; }
Output:
7 7 -1 -1 Fault
0 7 0 -1 Fault
1 7 0 1 Fault
2 2 0 1 Fault
0 2 0 1 Hit
3 2 0 3 Fault
0 2 0 3 Hit
4 4 0 3 Fault
2 4 0 2 Fault
3 4 3 2 Fault
0 0 3 2 Fault
3 0 3 2 Hit
2 0 3 2 Hit
1 1 3 2 Fault
2 1 3 2 Hit
0 1 0 2 Fault
1 1 0 2 Hit
7 1 0 7 Fault
0 1 0 7 Hit
1 1 0 7 Hit
Total number of page fault =12

(3) Optimal Page Replacement Algorithm


Program:
#include<stdio.h>
int p[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1},n=20;
int f[5],nf=3,count[3],pf=0,i,j,k,found,s;
main()
{
for(i=0;i<nf;i++)
{
f[i]=-1;
count[i]=-1;
}
for(i=0;i<n;i++)
{ found=0;
for(j=0;j<nf;j++)
if( f[j]==p[i])
found=1;
if(found==0)
{
if(i<nf)
{
f[i]=p[i];
pf++;
}
else
{
for(j=0;j<nf;j++)
{
count[j]=0;
for(s=i;s<n;s++)
if( f[j] != p[s] )
count[j]++;
else
break;
}
k=maximum(count);
f[k]=p[i];
pf++;
}
}
printf("\n%d",p[i]);
for(j=0;j<nf;j++)
printf("\t%d",f[j]);
printf(found?"\tHit":"\tFault");
}
printf("\nTotal number of page faults=%d",pf);
}
int maximum(int count[])
{
int large=count[0],x,index=0;
for(x=1;x<nf;x++)
if( count[x] > large )
{
large=count[x];
index=x;
}
return index;
}
Output:
7 7 -1 -1 Fault
0 7 0 -1 Fault
1 7 0 1 Fault
2 2 0 1 Fault
0 2 0 1 Hit
3 2 0 3 Fault
0 2 0 3 Hit
4 2 4 3 Fault
2 2 4 3 Hit
3 2 4 3 Hit
0 2 0 3 Fault
3 2 0 3 Hit
2 2 0 3 Hit
1 2 0 1 Fault
2 2 0 1 Hit
0 2 0 1 Hit
1 2 0 1 Hit
7 7 0 1 Fault
0 7 0 1 Hit
1 7 0 1 Hit
Total number of page faults=9
6. Implementation of Disk Scheduling Algorithms
(1) First Come First Serve Disks Scheduling Algorithm
Program:
#include <stdio.h>
void main()
{
int q[10]={98,183,37,122,14,124,65,67},n=8;
int start=53,seek=0,i,j,d;
for(i=0;i<n;i++)
{
d=abs(start-q[i]);
printf("\nHead moves from %4d to %4d = %4d",start,q[i],d );
seek+=d;
start=q[i];
}
printf("\nTotal number of head movements = %4d",seek);
}

Output:
Head moves from 53 to 98 = 45
Head moves from 98 to 183 = 85
Head moves from 183 to 37 = 146
Head moves from 37 to 122 = 85
Head moves from 122 to 14 = 108
Head moves from 14 to 124 = 110
Head moves from 124 to 65 = 59
Head moves from 65 to 67 = 2
Total number of head movements = 640
(2) Shortest Seek Time First - Disk Scheduling Algorithm
Program:
#include<stdio.h>
main()
{
int q[10]={98,183,37,122,14,124,65,67},n=8;
int start=53,seek=0,i,j,d,index,min,done[10];
for(i=0;i<n;i++)
done[i]=0;
for(i=0;i<n;i++)
{
min=9999;
for(j=0;j<n;j++)
if(abs(start-q[j])<min && !done[j] )
{
min=abs(start-q[j]);
index=j;
}
done[index]=1;
d=abs(start-q[index]);
printf("\nHead moves from %4d to %4d = %4d",start,q[index],d);
start=q[index];
seek+=d;
}
printf("\nTotal number of head movements =%4d",seek);
}

Output:

Head moves from 53 to 65 = 12


Head moves from 65 to 67 = 2
Head moves from 67 to 37 = 30
Head moves from 37 to 14 = 23
Head moves from 14 to 98 = 84
Head moves from 98 to 122 = 24
Head moves from 122 to 124 = 2
Head moves from 124 to 183 = 59
Total number of head movements = 236
(3) SCAN Disk Scheduling Algorithm
Program:
#include<stdio.h>
main()
{
int q[10]={98,183,37,122,14,124,65,67},n=8;
int start=53,seek=0,i,j,d,temp,s=0,begin;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(q[i] > q[j] )
{
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
while(q[s]<start) s++;
begin=s;
for(i=s-1;i>=0;i--)
{
d=abs(start-q[i]);
printf("\nHead moves from %4d to %4d = %4d",start,q[i],d);
start=q[i];
seek+=d;
}
printf("\nHead moves from %4d to %4d = %4d",start,0,start);
seek+=start;start=0;
for(i=begin;i<n;i++)
{
d=abs(start-q[i]);
printf("\nHead moves from %4d to %4d = %4d",start,q[i],d);
start=q[i];
seek+=d;
}
printf("\nTotal number of head movements =%4d",seek);
}
Output:
Head moves from 53 to 37 = 16
Head moves from 37 to 14 = 23
Head moves from 14 to 0 = 14
Head moves from 0 to 65 = 65
Head moves from 65 to 67 = 2
Head moves from 67 to 98 = 31
Head moves from 98 to 122 = 24
Head moves from 122 to 124 = 2
Head moves from 124 to 183 = 59
Total number of head movements = 236

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