Program 8: Q-Implement Memory Management Schemes Like Paging and Segmentation. 8 A) Paging Code
Program 8: Q-Implement Memory Management Schemes Like Paging and Segmentation. 8 A) Paging Code
8 A) PAGING
CODE:
#include<stdio.h>
int main()
{
int memsize=15;
int pagesize, nofpage;
int p[100];
int frameno, offset;
int logadd, phyadd;
int i, choice=0;
printf("\nYour memsize is %d ", memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
return 0;
}
OUTPUT:
8 B) SEGMENTATION
CODE:
#include<stdio.h>
#include<stdlib.h>
struct list
{
int seg;
int base;
int limit;
struct list *next;
} *p;
void main()
{
p=NULL;
int seg,offset,limit,base,c,s,physical;
printf("\nEnter segment table/n");
printf("Enter -1 as segment value for termination\n");
do
{
printf("Enter segment number");
scanf("%d",&seg);
if(seg!=-1)
{
printf("Enter base value:");
scanf("%d",&base);
printf("Enter value for limit:");
scanf("%d",&limit);
insert(p,base,limit,seg);
printf("\n");
}
}
while(seg!=-1);
printf("\nEnter offset:");
scanf("%d",&offset);
printf("Enter segmentation number:");
scanf("%d",&seg);
c=find(p,seg);
s=search(p,seg);
if(offset<c)
{
physical=s+offset;
printf("Address in physical memory %d\n",physical);
}
else
{
printf("error");
}
}
OUTPUT:
PROGRAM 9
Q- Implement Memory Management schemes like First Fit, Best Fit, Worst
Fit.
9 A) First Fit
CODE:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int blockSize[] = {100, 200, 500, 300, 600};
int processSize[] = {212, 430, 150, 417};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
return 0 ;
}
OUTPUT:
9 B) BEST FIT
CODE:
#include<bits/stdc++.h>
using namespace std;
if (bestIdx != -1)
{
allocation[i] = bestIdx;.
blockSize[bestIdx] -= processSize[i];
}
}
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;
}
OUTPUT:
9 C) WORST FIT
CODE:
#include<bits/stdc++.h>
using namespace std;
if (wstIdx != -1)
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;
}
OUTPUT:
PROGRAM 10
Q- Implement any file allocation techniques (Contiguous, linked or Indexed)
CODE:
#include<stdio.h>
#define TB 100
void allocate();
void deallocate();
void display();
int BFull();
struct Link
{
char n[30];
int len;
int st;
struct node
{
int index;
struct node *next;
}*Start,*current,*newnode,*temp;
}F[30];
int Table[TB+1],pos=0,r,i,j,ch,B=0;
char fn[30];
main()
{
printf("\n Linked File Allocation \n\n");
do{
printf("\n\n1.Allocate\n2.Deallocate\n3.Display\n4.Exit");
printf("\n\nEnter Your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
pos++;
allocate();
break;
case 2:
deallocate();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nInvalid Choice ");
}
}while(ch!=4);
}
void allocate()
{
printf("\nEnter File Name : ");
scanf("%s",&(F[pos].n));
printf("\nEnter File Length : ");
scanf("%d",&(F[pos].len));
F[pos].Start=NULL;
if(BFull())
{
pos--;
printf("\n\nNo Enough Free Space Available \n");
return;
}
for(i=1;i<=F[pos].len;i++)
{
F[pos].newnode=(struct node *)malloc(sizeof(struct node));
while(1)
{
r=rand()%TB+1;
if(Table[r]==0)
{
F[pos].newnode->index =r;
F[pos].newnode->next=NULL;
if(F[pos].Start==NULL)
{
F[pos].Start=F[pos].newnode;
F[pos].current=F[pos].newnode;
}
else
{
F[pos].current->next=F[pos].newnode;
F[pos].current=F[pos].newnode;
}
Table[r]=1;
break;
}
}
}
F[pos].st=F[pos].Start->index;
for(i=r;i<r+F[pos].len;i++)
Table[i]=1;
printf("\n\tFile Allocation Table\n");
printf("\nFileName\tStart\tEnd\tLength\n");
for(i=1;i<=pos;i++)
{
printf("\n%s\t\t%d\t%d\t%d",F[i].n,F[i].st,F[pos].current->index,F[i].len);
printf("\n");
}
}
void deallocate()
{
printf("\nEnter The File Name : ");
scanf("%s",&fn);
for(i=1;i<=pos;i++)
{
if(strcmp(F[i].n,fn)==0)
{
F[i].current=F[i].Start;
while(F[i].current)
{
Table[F[i].current->index]=0;
F[i].temp=F[i].current;
F[i].current=F[i].current->next;
free(F[i].temp);
}
strcpy(F[i].n,"NULL");
F[i].st=0;
F[i].len=0;
printf("\nFile (%s) Deleted Successfully \n",fn);
break;
}
else
printf("\nDeletion Unsuccessful\n");
}
printf("\n\t\tFile Allocation Table\n");
printf("\nFileName\tStart\tLength\n");
for(i=1;i<=pos;i++)
{
printf("\n%s\t\t%d\t%d",F[i].n,F[i].st,F[i].len);
printf("\n");
}
}
void display()
{
printf("\nEnter The File Name : ");
scanf("%s",fn);
printf("\nBlocks Allocated Are : ");
for(i=1;i<=pos;i++)
{
if(strcmp(F[i].n,fn)==0)
{
F[i].current=F[i].Start;
while(F[i].current)
{
printf(">--%d-->",F[i].current->index);
F[i].current=F[i].current->next;
}
break;
}
}
if(i==pos+1)
{
printf("\n\nNo File Found\n");
}
}
int BFull()
{
for(i=1,B=0;i<=pos;i++)
B=B+F[i].len;
if(B>TB)
return 1;
else
return 0;
}
OUTPUT: