Friday, 24 April 2015

Program in C to show LRU page replacement algorithm

#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 in C to show first ii firt out page replacement algorithm

#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 in C to show multiprogramming fixed task

#include<stdio.h>
#include<conio.h>
main()
{
            int ms,i,ps[20],n,size,p[20],s,intr=0;
            clrscr();
            printf("Enter size of memory:");
            scanf("%d",&ms);
            printf("Enter memory for OS:");
            scanf("%d",&s);
            ms-=s;
            printf("Enter no.of partitions to be divided:");
            scanf("%d",&n);
            size=ms/n;
            for(i=0;i<n;i++)
            {
                        printf("Enter process and process size");
                        scanf("%d%d",&p[i],&ps[i]);
                        if(ps[i]<=size)
                        {
                                    intr=intr+size-ps[i];
                                    printf("process%d is allocated\n",p[i]);
                        }
                        else
                                    printf("process%d is blocked",p[i]);
            }
            printf("total fragmentation is %d",intr);
            getch();
}


OUTPUT


Program in C to show multiprogramming variable task

#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 in C to show linked file allocation

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

}


Output


Monday, 23 February 2015

//C Programe for  INDEXED  FILE ALLOCATION  //


#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 in C to show sequential file allocation

#include<stdio.h>
#include<conio.h>
main()
{
 int n,i,j,b[20],sb[20],t[20],x,c[20][20];
 clrscr();
 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]);
 printf("Enter file name:");
 scanf("%d",&x);
 printf("File name is:%d",x);
 printf("length is:%d",b[x-1]);
 printf("blocks occupied:");
 for(i=0;i<b[x-1];i++)
          printf("%4d",c[x-1][i]);
 getch();
}

OUTPUT