Tuesday 12 May 2015

Program 12a):
AIM: To implement page replacement algorithms FIFO (First In First Out)

 
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
 clrscr();
 printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
 printf("\n Enter no.of frames....");
 scanf("%d",&nof);
 printf("Enter number of reference string..\n");
 scanf("%d",&nor);
 printf("\n Enter the reference string..");
 for(i=0;i<nor;i++)
 scanf("%d",&ref[i]);
 printf("\nThe given reference string:");
 for(i=0;i<nor;i++)
 printf("%4d",ref[i]);
 for(i=1;i<=nof;i++)
 frm[i]=-1;
 printf("\n");
 for(i=0;i<nor;i++)
 {
   flag=0;
   printf("\n\t Reference np%d->\t",ref[i]);
   for(j=0;j<nof;j++)
   {
     if(frm[j]==ref[i])
     {
       flag=1;
       break;
     }}
      if(flag==0)
   {
     pf++;
     victim++;
     victim=victim%nof;
     frm[victim]=ref[i];
     for(j=0;j<nof;j++)
     printf("%4d",frm[j]);
   }
 }
 printf("\n\n\t\t No.of pages faults...%d",pf);
 getch();
}


OUTPUT:


Program 12b):
AIM: To implement page replacement algorithm LRU (Least Recently Used)

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();

void main()
{
  clrscr();
  printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
  printf("\n Enter no.of Frames....");
  scanf("%d",&nof);

  printf(" Enter no.of reference string..");
  scanf("%d",&nor);

  printf("\n Enter reference string..");
  for(i=0;i<nor;i++)
  scanf("%d",&ref[i]);

  printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
  printf("\n\t The given reference string:");
  printf("\n………………………………..");
  for(i=0;i<nor;i++)
  printf("%4d",ref[i]);
  for(i=1;i<=nof;i++)
  {
    frm[i]=-1;
    lrucal[i]=0;
  }

  for(i=0;i<10;i++)
   recent[i]=0;
   printf("\n");
   for(i=0;i<nor;i++)
   {
     flag=0;
     printf("\n\t Reference NO %d->\t",ref[i]);
     for(j=0;j<nof;j++)
     {
      


        if(frm[j]==ref[i])
       {
           flag=1;
           break;
       }
     }

     if(flag==0)
     {
       count++;
       if(count<=nof)
       victim++;
       else
       victim=lruvictim();
       pf++;
       frm[victim]=ref[i];
       for(j=0;j<nof;j++)
       printf("%4d",frm[j]);
     }
     recent[ref[i]]=i;
   }
   printf("\n\n\t No.of page faults...%d",pf);
   getch();
}
int lruvictim()
{
  int i,j,temp1,temp2;
  for(i=0;i<nof;i++)
  {
    temp1=frm[i];
    lrucal[i]=recent[temp1];
  }
  temp2=lrucal[0];
  for(j=1;j<nof;j++)
  {
    if(temp2>lrucal[j])
    temp2=lrucal[j];
  }
  for(i=0;i<nof;i++)
  if(ref[temp2]==frm[i])
  return i;
  return 0;
}


OUTPUT:
                        




Program 12c):
AIM: To implement page replacement algorithms Optimal (The page which is not used for longest time)


#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
   clrscr();
   printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
   printf("\n.................................");
   printf("\nEnter the no.of frames");
   scanf("%d",&nof);
   printf("Enter the no.of reference string");
   scanf("%d",&nor);
   printf("Enter the reference string");
   for(i=0;i<nor;i++)
       scanf("%d",&ref[i]);
   clrscr();
   printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
   printf("\n................................");
   printf("\nThe given string");
   printf("\n....................\n");
   for(i=0;i<nor;i++)
       printf("%4d",ref[i]);
   for(i=0;i<nof;i++)
   {
       frm[i]=-1;
       optcal[i]=0;
   }
   for(i=0;i<10;i++)
       recent[i]=0;
   printf("\n");
   for(i=0;i<nor;i++)
   {
      flag=0;
      printf("\n\tref no %d ->\t",ref[i]);
      for(j=0;j<nof;j++)
      {
          if(frm[j]==ref[i])
          {
             flag=1;
             break;
          }
      }
      if(flag==0)
      {
          count++;
          if(count<=nof)
              victim++;
          else
              victim=optvictim(i);
          pf++;
          frm[victim]=ref[i];
          for(j=0;j<nof;j++)
             printf("%4d",frm[j]);
      }
   }
   printf("\n Number of page faults: %d",pf);
   getch();
}
int optvictim(int index)
{
   int i,j,temp,notfound;
   for(i=0;i<nof;i++)
   {
      notfound=1;
      for(j=index;j<nor;j++)
          if(frm[i]==ref[j])
          {
              notfound=0;
              optcal[i]=j;
              break;
          }
      if(notfound==1)
             return i;
   }
   temp=optcal[0];
   for(i=1;i<nof;i++)
       if(temp<optcal[i])
             temp=optcal[i];
   for(i=0;i<nof;i++)
       if(frm[temp]==frm[i])
             return i;
 return 0;
}
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();
          }


Program 10a):
AIM: Write a C program to simulate file organization technique Single Level

#include<stdio.h>
#include<conio.h>
main()
{
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
clrscr();
printf("enter number of directorios:");
scanf("%d",&master);
printf("enter names of directories:");
for(i=0;i<master;i++)
scanf("%s",&d[i]);
printf("enter size of directories:");
for(i=0;i<master;i++)
scanf("%d",&s[i]);
printf("enter the file names :");
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\n");
printf("*************************************************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
}
printf("\t\n");
getch();
}





OUTPUT:








Program 10b):
AIM: Write a C program to simulate file organization technique Two Level


#include<stdio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
main()
{
int i,j,k,n;
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");    }
  
  }


PROGRAM 9a):
AIM: Write a C Program to implement Sequential File Allocation method.

#include<stdio.h>
void main()
{
 int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
          printf("Enter no. of blocks occupied by file%d:",i+1);
          scanf("%d",&b[i]);
          printf("Enter the starting block of file%d:",i+1);
          scanf("%d",&sb[i]);
          t[i]=sb[i];
          for(j=0;j<b[i];j++)
                   c[i][j]=sb[i]++;
          }
 printf("Filename\tStart block\tlength\n");
 for(i=0;i<n;i++)
          printf("%d\t        %d \t           %d\n",i+1,t[i],b[i]);
}

OUTPUT:
PROGRAM 9b):
AIM: Write a C Program to implement Indexed File Allocation method.

#include<stdio.h>
#include<conio.h>
main()
{
 int n,m[20],i,j,sb[20],s[20],b[20][20],x;
 clrscr();
 printf("Enter no. of files:");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {       printf("Enter starting block and size of file%d:",i+1);
          scanf("%d%d",&sb[i],&s[i]);
          printf("Enter blocks occupied by file%d:",i+1);
          scanf("%d",&m[i]);
          printf("enter blocks of file%d:",i+1);
          for(j=0;j<m[i];j++)
                   scanf("%d",&b[i][j]);
 } printf("\nFile\t index\tlength\n");
 for(i=0;i<n;i++)
 {
          printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
 }printf("\nEnter file name:");
 scanf("%d",&x);
 printf("file name is:%d\n",x);
 i=x-1;
 printf("Index is:%d",sb[i]);
 printf("Block occupied are:");
 for(j=0;j<m[i];j++)
          printf("%3d",b[i][j]);
 getch();
}

OUTPUT:







                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      










Program 9c):
Aim: Write a C Program to implement Linked File Allocation method.

#include<stdio.h>
#include<conio.h>
struct file
{
 char fname[10];
 int start,size,block[10];
}f[10];
main()
{
 int i,j,n;
 clrscr();
 printf("Enter no. of files:");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
 printf("Enter file name:");
 scanf("%s",&f[i].fname);
 printf("Enter starting block:");
 scanf("%d",&f[i].start);
 f[i].block[0]=f[i].start;
 printf("Enter no.of blocks:");
 scanf("%d",&f[i].size);
 printf("Enter block numbers:");
 for(j=1;j<=f[i].size;j++)
 {
          scanf("%d",&f[i].block[j]);
 }
 }
 printf("File\tstart\tsize\tblock\n");
 for(i=0;i<n;i++)
 {
          printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
          for(j=1;j<=f[i].size-1;j++)
                   printf("%d--->",f[i].block[j]);
          printf("%d",f[i].block[j]);
          printf("\n");
 }
 getch();
}