Assessment: 5: Ite 2002-Operating Systems Lab Lab Assessment Questions
Assessment: 5: Ite 2002-Operating Systems Lab Lab Assessment Questions
cout<<"\t\t-----Welcome---"<<endl;
cout<<"Enter memory total partitions:\t";
cin>>n;
cout<<"\nEnter memory size for\n";
for(i=1;i<=n;i++)
{
cout<<"\npartition "<<i<<" :\t";
cin>>m[i];
po[i]=i;
}
cout<<"\nEnter total number of process:\t";
cin>>j;
cout<<"\nEnter memory size for\n";
for(i=1;i<=j;i++)
{
cout<<"\nprocess "<<i<<" :\t";
cin>>p[i];
}
cout<<"\n**Menu**\n1.first fit\n2.best fit\n3.worst fit\nenter choice:\t";
cin>>c;
switch(c)
{
case 1:
for(i=1;i<=j;i++)
{
flag=1;
for(k=1;k<=n;k++)
{
if(p[i]<=m[k])
{
cout<<"\nProcess "<<i<<" whose memory size is "<<p[i]<<"KB allocated at
memory partition:\t"<<po[k];
m[k]=m[k]-p[i];
break;
}
else
{
flag++;
}
}
if(flag>n)
{
cout<<"\nProcess "<<i<<" whose memory size is "<<p[i]<<"KB can't be allocated";
}
}
break;
case 2:
for(y=1;y<=n;y++)
{
for(z=y;z<=n;z++)
{
if(m[y]>m[z])
{
temp=m[y];
m[y]=m[z];
m[z]=temp;
temp1=po[y];
po[y]=po[z];
po[z]=temp1;
}
}
}
for(i=1;i<=j;i++)
{
flag=1;
for(k=1;k<=n;k++)
{
if(p[i]<=m[k])
{
cout<<"\nProcess "<<i<<" whose memory size is "<<p[i]<<"KB allocated at
memory partition:\t"<<po[k];
m[k]=m[k]-p[i];
break;
}
else
{
flag++;
}
}
if(flag>n)
{
cout<<"\nProcess "<<i<<" whose memory size is "<<p[i]<<"KB can't be allocated";
}
}
break;
case 3:
for(y=1;y<=n;y++)
{
for(z=y;z<=n;z++)
{
if(m[y]<m[z])
{
temp=m[y];
m[y]=m[z];
m[z]=temp;
temp1=po[y];
po[y]=po[z];
po[z]=temp1;
}
}
}
for(i=1;i<=j;i++)
{
flag=1;
for(k=1;k<=n;k++)
{
if(p[i]<=m[k])
{
cout<<"\nProcess "<<i<<" whose memory size is "<<p[i]<<"KB allocated at
memory partition:\t"<<po[k];
m[k]=m[k]-p[i];
break;
}
else
{
flag++;
}
}
if(flag>n)
{
cout<<"\nProcess "<<i<<" whose memory size is "<<p[i]<<"KB can't be allocated";
}
}
break;
}
return 0;
}
Output
i)
ii)
iii)
b) i) FIFO
Code:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
Output:
No. of Frames=3
No. of Frames=5
ii) LRU
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
Output:
No. of Frames=3
No. of Frames=5
iii) OPT
Code:
#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);
if(flag2 == 0){
flag3 =0;
if(flag3 ==0){
max = temp[0];
pos = 0;
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
return 0;
}
Output:
No. of Frames=3
No. of Frames=5
c)
#include<stdio.h>
#include<unistd.h>
#onclude<stdlib.h>
int main()
{
int f[50],i,st,j,len,c,k;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit(0);
return 0;
}
Output:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n",
ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
return 0;
}
Output: