Thursday, July 18, 2024

OS-II Assignment No.2: File Allocation Methods

 Operating Systems -II 

Assignment No.2: File Allocation Methods


Slot-1

i. Write a program to simulate Sequential (Contiguous) file allocation method. Assume disk

with n number of blocks. Give value of n as input. Write menu driver program with menu

options as mentioned above and implement each option.

Solution :- 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int bv[50],i,st,j,len,c,k;

char name[10][30];

int start[10],length[10],num=0;


void dislayBitVector()

{

  printf("\n Bit Vector contents \n");

  for(i=0;i<50;i++)

     printf(" %d ",bv[i]);

}//dislayBitVector


void createFile()

{

    char temp[30];

    printf("Enter the name of the file :");

    scanf("%s",&temp);

    for(int i=0;i<num;i++)

            if(strcmp(name[i],temp)==0) 

            {

            printf("\n Filename already exists");

            return;

            }//if

    strcpy(name[num],temp);

    printf("Enter the start block of the file : ");

    scanf("%d",&start[num]);

    printf("Enter the length of the file : ");

    scanf("%d",&length[num]);

    

    for(j=start[num];j<(start[num]+length[num]);j++)

      if(bv[j]==1)

        {  

                printf("File can not be allocated...Block already allocated");

                strcpy(name[j],"-");   

                start[j]=0;

                length[j]=0; 

                return;

        }//if

    

     for(j=start[num];j<(start[num]+length[num]);j++)

       { 

        bv[j]=1;

        printf("\n%d->%d",j,bv[j]);

        }//for

    num++;

}//creteFile


void showDirectory()

{

  printf("Directory contents\n");

    printf("%s%40s%40s\n","File Name","Start Block","Length");

   

    for(int i=0;i<num;i++)

    {

        if(strcmp(name[i],"-")!=0)

            {

                printf("%s%40d%40d\n",name[i],start[i],length[i]);

            }//if

    }//for

}//showDirectory


void deleteFile()

{  

    char temp[10];

   

    printf("\nEnter the name of the file to delete  :");

    scanf("%s",&temp);

    for(int i=0;i<num;i++)

    {

     if(strcmp(name[i],temp)==0)

       {  

          for(j=start[i];j<(start[i]+length[i]);j++)

            {

            bv[j]=0;

            printf("\n%d->%d",j,bv[j]);

            }//for

          printf("\n File successfully deleted..");

          strcpy(name[i],"-");   

          start[i]=0;

          length[i]=0; 

          return;

       }//if  

    }//for

       printf("\n File not found..");

}//deleteFile


int main()

{

  int ch=0;

  for(i=0;i<50;i++)

     bv[i]=0;

   do

   {

        printf("\n *** Menu ****");

        printf("\n 1. Show Bit Vector");

        printf("\n 2. Create New File");

        printf("\n 3. Show Directory");

        printf("\n 4. Delete File");

        printf("\n 5. Exit");

        printf("\n Enter your choice : ");

        scanf("%d",&ch);

        switch(ch)

        {

        case 1: dislayBitVector();

                break;

        case 2: createFile();

                break;

        case 3: showDirectory();

                break;

        case 4: deleteFile();

                break;

        } //switch      

   }while(ch!=5);

      

}//main

Slot- II

i. Write a program to simulate Linked file allocation method. Assume disk with n number

of blocks. Give value of n as input. Write menu driver program with menu options as

mentioned above and implement each option.

Solution :- 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>


#define MAX 100

typedef struct dir

{

 char fname[20];

 int start;

 struct dir *next;

}NODE;

NODE *head,*last,*temp,*prev,*dell,*tmp;

int n,bit[MAX],fb=0;


void init()

{

 int i;

 printf("Enter total no.of disk blocks:");

 scanf("%d",&n);

 for(i=0;i<n;i++)

 {

bit[i]=rand()%2;

 }

}


void show_bitvector()

{

 int i;

 for(i=0;i<n;i++)

  printf("%d ",bit[i]);

 printf("\n");

}


void show_dir()

{

 NODE *temp;

 int i;

 printf("\n Directory "); 

 printf("\n File Name     Allocated Block Number "); 

 for(temp=head;temp!=NULL;temp=temp->next)

  {

  printf("\n   %s",temp->fname);  

  printf("           %d",temp->start);

  }

 


printf("\n\n Allocated Blocks :  "); 

for(temp=head;temp!=NULL;temp=temp->next)

  {

   printf("%d->",temp->start);

  }

 printf("NULL\n\n");

}


void create()

{

 NODE *p;

 char fname[20];

 int i,j,nob;

 int fb=0;

 printf("Enter file name:");

 scanf("%s",&fname);

 printf("Enter no.of blocks:");

 scanf("%d",&nob);

 for(i=0;i<n;i++)

 {

  if(bit[i]==0) 

    fb++; 

 }

if(nob>fb)

 {

  printf("Failed to create file %s\n",fname);

  return;

 }

 else

 {

    for(i=0;i<n;i++)  

{

if(bit[i]==0 && nob!=0) 

         {

p = (NODE*)malloc(sizeof(NODE));   

strcpy(p->fname,fname);                     

nob--;                            

bit[i]=1;                           

p->start=i;                         

p->next=NULL;                       

if(head==NULL)

                   head=p;                        

else

          last->next=p;

               last=p;

}

       }

    printf("File %s created successully.\n",fname);

 } 

}

void delete()

{

   int i=0; 

   NODE *p,*q;

   char fname[20];

  temp=head;

  printf("Enter file to be deleted:");

  scanf("%s",fname);


  while(temp!=NULL)

    {

p = q = head;

while(p!=NULL)

{

           if(strcmp(p->fname,fname)==0)

break;

           q = p;

           p = p->next;

}


if(p==NULL)

{

   return;

}



if(p==head)

head = head->next;

else if(p==last)

      {

last=q;

last->next=NULL;

      }

    else

             {

q->next = p->next;

             }

        bit[p->start]=0;

free(p);

   temp=temp->next;

  }


printf("File %s deleted successfully.\n",fname);


}


  

int main()

{

 int ch;

 init();

 while(1)

 {

  printf("1.Show bit vector\n");

  printf("2.Create new file\n");

  printf("3.Show directory\n");

  printf("4.Delete file\n");

  printf("5.Exit\n");

  printf("Enter your choice (1-5):");

  scanf("%d",&ch);

  switch(ch)

  {

  case 1:show_bitvector(); break;

  case 2: create(); break;

  case 3:show_dir(); break;

  case 4: delete();break;

  case 5: exit(0);

  }

}

return 0;

}

Slot- III

i. Write a program to simulate Indexed file allocation method. Assume disk with n number

of blocks. Give value of n as input. Write menu driver program with menu options as

mentioned above and implement each option.

Solution :- 

#include<stdio.h>

#include<stdlib.h>

int f[50],index1[50],i, n, st, len, j, c, k, ind,count=0,cnt=0,key[50],blocks[50];


void show_bitvector()

{

printf("\n vector contents....\n");

for(i=0;i<50;i++)

    printf(" %d ",f[i]);

}


void show_dir()

{

 printf("\nIndex Block Number\n");

 for(k=0;k<cnt;k++)

       printf("%d-------->%d\n",key[k],blocks[k]);

}


void create()

{

   x:printf("\nEnter the index block: ");

   scanf("%d",&ind);

   if(f[ind]!=1)

     {

       printf("\nEnter number of blocks needed 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;

      printf("\n Enter block numbers for the index %d on the disk : \n", ind);

      for(i=0;i<n;i++)

        {

        scanf("%d",&index1[i]);

        if(f[index1[i]]==0)

            count++;

        }                 

    if(count==n)            

       {                     

       key[cnt]=ind;         

       f[ind]=1;           

       for(j=0;j<n;j++)

        {

         f[index1[j]]=1;

         blocks[cnt]=index1[j];

         key[cnt]=ind; 

         cnt++;

        }

      printf("Allocated\n");

      printf("File Indexed\n");

      for(k=0;k<n;k++)

       printf("%d-------->%d : %d\n",ind,index1[k],f[index1[k]]); 

   }                                                              

  

else

   {

    printf("File in the index1 is already allocated \n");

    printf("Enter another file indexed");

    goto y;

   }

}

void deleteindex()

  int flag=0;

  printf("\nEnter the index block to delete: ");

  scanf("%d",&ind);

  for(i=0;i<cnt;i++)

     {

       if(key[i]==ind)

       {

         flag=1;

         f[ind]=0;

         printf("\n blocks = %d",blocks[i]);

         f[blocks[i]]=0;

         blocks[i]=0;

         key[i]=0;

        }

    }


  if(flag==1)

    printf("\n File deleted...\n\n");

  else

    printf("\n File not found.. \n\n");

}


int main()

{

 int ch;

 for(i=0;i<50;i++)

    f[i]=0;

 while(1)

 {

  printf("\n1.Show bit vector\n");

  printf("2.Create new file\n");

  printf("3.Show directory\n");

 printf("4.Delete file\n");

  printf("5.Exit\n");

  printf("Enter your choice (1-5):");

  scanf("%d",&ch);

  switch(ch)

  {

  case 1:show_bitvector(); break;

  case 2: create(); break;

  case 3:show_dir(); break;

  case 4: deleteindex(); break;

  case 5: exit(0);

  }

}

return 0;

}

No comments:

Post a Comment