Tuesday 12 May 2015

Program 11a):
AIM:  Write a program to implement Dynamic allocation of memories in Memory Variable Technique.


#include<stdio.h>
#include<conio.h>
main()
{
          int i,m,n,tot,s[20];
          clrscr();
          printf("Enter total memory size:");
          scanf("%d",&tot);
          printf("Enter no. of pages:");
          scanf("%d",&n);
          printf("Enter memory for OS:");
          scanf("%d",&m);
          for(i=0;i<n;i++)
          {
                   printf("Enter size of page%d:",i+1);
                   scanf("%d",&s[i]);
          }
          tot=tot-m;
          for(i=0;i<n;i++)
          {
                   if(tot>=s[i])
                   {
                             printf("Allocate page %d\n",i+1);
                             tot=tot-s[i];
                   }
                   else
                             printf("process p%d is blocked\n",i+1);
          }
          printf("External Fragmentation is=%d",tot);
          getch();
}





OUTPUT:

















Program 11b):
AIM:  Write a program to implement Dynamic allocation of memories in Memory Fixed Technique.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 10
int main()
{
int ma,bs,ps,tmp,sbn[MAX]={0},ebn[MAX]={0},count=0,i=0,k,ifrag[MAX]={0};
char ch;
clrscr();
printf("\nEnter total memory available (in MB)");
scanf("%d",&ma);
printf("\nEnter size of each block(in MB)");
scanf("%d",&bs);
while(ma)
{
printf("\nDo u have a program(y/n)");
fflush(stdin);
scanf("%c",&ch);
if((ch!='y') && (ch!='Y'))
{
printf("\nMemory available %d MB",ma);
break;
}
printf("\nEnter the size of program(inMB)");
scanf("%d",&ps);
if(ps>ma)
{
printf("\nMemory required %d\nBUt\nMemory available:%d",ps,ma);
break;
}
count++;
if(!i)
{
sbn[i]=0;
ebn[i]=(ceil((float)ps/bs))-1;
}
else
{
sbn[i]=ebn[i-1]+1;
ebn[i]=sbn[i]+(ceil((float)ps/bs))-1;
}
tmp=((ceil((float)ps/bs)*bs));
ifrag[i]=tmp-ps;
i++;
ma-=tmp;
printf("\nMemory allocated%dMB\nMemory available%dMB",tmp,ma);
printf("\nBlocls\tnum of blocks\tInternal fragmentation(inMB)");
for(k=0;k<count;k++)
printf("\n\n%d-%d\t\t%d\t\t%d",sbn[k],ebn[k],ebn[k]-sbn[k]+1,ifrag[k]);
}
getch();
return 1;
}























OUTPUT:















Program 11c):
AIM: To implement the Memory management policy- Paging.


#include<stdio.h>
#define MAX 50
int main()
{
int page[MAX],i,n,f,ps,off,pno;
printf("\nEnter the no of pages in memory");
scanf("%d",&n);
printf("\nEnter page size");
scanf("%d",&ps);
printf("\nEnter no of frames");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
scanf("%d",&page[i]);
}
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
return 0;
}









OUTPUT:











Program 11d):
AIM: To implement the memory management policy-segmentation.


#include <stdio.h>
#include <conio.h>
#include <math.h>
int sost;
void gstinfo();
void ptladdr();

struct segtab
{
          int sno;
          int baddr;
          int limit;
          int val[10];
}st[10];

void gstinfo()
{
          int i,j;
          printf("\n\tEnter the size of the segment table: ");
          scanf("%d",&sost);

          for(i=1;i<=sost;i++)
                   {
                   printf("\n\tEnter the information about segment: %d",i);
                   st[i].sno = i;
                   printf("\n\tEnter the base Address: ");
                   scanf("%d",&st[i].baddr);
                   printf("\n\tEnter the Limit: ");
                   scanf("%d",&st[i].limit);
                   for(j=0;j<st[i].limit;j++)
                             {
                             printf("Enter the %d address Value: ",(st[i].baddr + j));
                             scanf("%d",&st[i].val[j]);
                             }
                   }
}

void ptladdr()
{
          int i,swd,d=0,n,s,disp,paddr;
          clrscr();
          printf("\n\n\t\t\t SEGMENT TABLE \n\n");
          printf("\n\t      SEG.NO\tBASE ADDRESS\t      LIMIT \n\n");
          for(i=1;i<=sost;i++)
                   printf("\t\t%d    \t\t%d\t\t%d\n\n",st[i].sno,st[i].baddr,st[i].limit);
          printf("\n\nEnter the logical Address: ");
          scanf("%d",&swd);
          n=swd;
          while (n != 0)
          {
                   n=n/10;
                   d++;
          }

          s = swd/pow(10,d-1);
          disp = swd%(int)pow(10,d-1);

          if(s<=sost)
          {
                   if(disp < st[s].limit)
                   {
                             paddr = st[s].baddr + disp;
                             printf("\n\t\tLogical Address is: %d",swd);
                             printf("\n\t\tMapped Physical address is: %d",paddr);
                             printf("\n\tThe value is: %d",( st[s].val[disp] )  );
                   }
                   else
                             printf("\n\t\tLimit of segment %d is high\n\n",s);
          }

          else
                   printf("\n\t\tInvalid Segment Address \n");
          }

          void main()
          {
          char ch;
          clrscr();
          gstinfo();
          do
          {
          ptladdr();
          printf("\n\t Do U want to Continue(Y/N)");
          flushall();
          scanf("%c",&ch);
          }while (ch == 'Y' || ch == 'y' );

          getch();
          }


No comments:

Post a Comment