0% found this document useful (0 votes)
92 views16 pages

Assessment: 5: Ite 2002-Operating Systems Lab Lab Assessment Questions

The document contains code and output for three memory allocation algorithms: first fit, best fit, and worst fit. It also contains code and output for three page replacement algorithms: FIFO, LRU, and OPT. Finally, it contains code for three file allocation methods: sequential, linked, and indexed allocation. The codes demonstrate implementing the memory and page allocation algorithms as well as displaying the output.

Uploaded by

ARIHANT SAXENA
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)
92 views16 pages

Assessment: 5: Ite 2002-Operating Systems Lab Lab Assessment Questions

The document contains code and output for three memory allocation algorithms: first fit, best fit, and worst fit. It also contains code and output for three page replacement algorithms: FIFO, LRU, and OPT. Finally, it contains code for three file allocation methods: sequential, linked, and indexed allocation. The codes demonstrate implementing the memory and page allocation algorithms as well as displaying the output.

Uploaded by

ARIHANT SAXENA
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/ 16

ITE 2002- OPERATING SYSTEMS LAB

Lab Assessment Questions


Assessment: 5

NAME: ARIHANT SAXENA


REG. NO.: 18BIT0133
SLOT: L25+L26
a) Code:
#include <iostream>
using namespace std;
int main()
{
int c,i,j,k,n,l,m[10],p[10],po[20],flag,z,y,temp,temp1;

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);

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:
No. of Frames=3

No. of Frames=5
c)

i)Sequential File Allocation Program:

#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:

ii)Linked File Allocation Program:


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
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:

iii)Indexed File Allocation Program:

#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:

You might also like