Skip to main content

Assignment No: 2

 Assignment No: 2


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.

CODE:

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define SIZE 100
  5. #define NEWNODE (struct direntry*)malloc(sizeof(struct direntry))

  6. struct direntry
  7. {
  8. char fname[14];
  9. int start,count;

  10. struct direntry *next;
  11. }

  12.  *dirst=NULL,  *dirend=NULL;
  13.  int bitvector[SIZE];
  14.  
  15.  void main()
  16.  {
  17.  int ch1=0,i,j,k,n,flag;
  18.  char fname[14];
  19.  int rand();
  20.  struct direntry *t1, *t2;
  21.   for(i=0; i<SIZE;i++)
  22.    {
  23.     bitvector[i]=rand()%2;   
  24.     }
  25.     
  26.     
  27. while(1)
  28.  {
  29.      printf("\n\n1.Print  Bit Vector");
  30.      printf("\n2.Create File");
  31.      printf("\n3.Print Directory");
  32.      printf("\n4.Delete File");
  33.      printf("\n5.Exit");
  34.      printf("\nEnter your choice :");
  35.      scanf("%d",&ch1);
  36.      
  37.  switch(ch1)
  38.  {
  39.    
  40.    case 1:
  41.           for(i=0; i<SIZE; i++)
  42.           printf("%4d",bitvector[i]);
  43.           break;
  44.    
  45.   case 2:
  46.           if(dirst==NULL)
  47.           dirst=dirend=NEWNODE;
  48.              else
  49.               {
  50.                 dirend->next=NEWNODE;
  51.                 dirend=dirend->next;
  52.                }
  53.            dirend->next=NULL;
  54.            printf("\n Enter A Filename :");
  55.            scanf("%s",dirend->fname);
  56.            printf("\nEnter the blocks to allocate :");
  57.            scanf("%d",&n);
  58.            dirend->count=n;
  59.            for(i=0; i<100; i++)
  60.             {
  61.               if(bitvector[i]==1)//found free block
  62.                {
  63.                 for(j=i; j<i+n; j++)//find next n free block
  64.                  {
  65.                   if(bitvector[j]!=1)//not free
  66.                   break;
  67.                  }//for
  68.            if(j==i+n)//found n contiguous free block
  69.             {
  70.                   dirend->start=i;
  71.                   for(k=i; k<j; k++)//allocate block
  72.                   bitvector[k]=0;
  73.                   break;   
  74.             }
  75.       }      
  76.    }      
  77.    break;                    

  78.   case 3:
  79.         printf("\nDirectory :");
  80.         printf("\n---------------------------");
  81.         printf("\nFilename   Start      Count");
  82.         printf("\n-------------------------------");
  83.         for(t1=dirst; t1!=NULL; t1=t1->next)
  84.         printf("\n%-10s %5d %5d ",t1->fname,t1->start,t1->count);
  85.         printf("\n------------------------------------------------");
  86.         break;
  87.         
  88.         case 4:
  89.         printf("\nEnter a Filename :");
  90.         scanf("%s",fname);
  91.         t1=dirst;
  92.         while(t1!=NULL)
  93.         {
  94.         if(strcmp(t1->fname,fname)==0)
  95.    break;
  96.    t2=t1;
  97.    t1=t1->next;
  98.         }  
  99.      if(t1!=NULL)//file found
  100.      {
  101.       for(i=t1->start;i<t1->start+t1->count; i++)
  102.       bitvector[i]=1;//free blocks
  103.      
  104.      if(t1==dirst)
  105.      dirst=dirst->next;
  106.     else
  107.       t2->next=t1->next;
  108.           if(dirst==NULL)
  109.                dirend=NULL;
  110.                
  111.                free(t1);
  112.        }
  113.          else
  114.          printf("\nFile not found...\n");
  115.          break;
  116.          
  117.     case 5:
  118.               exit(0);
  119.                                      
  120.     }
  121.   }  
  122. }

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

CODE:

👇

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define SIZE 100
  5. #define NEWNODE (struct direntry*)malloc(sizeof(struct direntry))
  6. #define NEWBLK (struct blknode*)malloc(sizeof(struct blknode))

  7. struct blknode
  8. {
  9. int bno;
  10. struct blknode *next;
  11. }*curr,*prev;

  12. struct direntry
  13. {
  14. char fname[14];
  15. int start,end;
  16. struct blknode *blist;
  17. struct direntry *next;
  18. }

  19. *dirst=NULL,  *dirend=NULL;
  20.  int bitvector[SIZE];
  21.  
  22. void main()
  23. {
  24.  int ch1=0,i,j,n;
  25.  char fname[14];
  26.  int rand();
  27.  struct direntry *t1, *t2;
  28.  for(i=0; i<SIZE;i++)
  29.     bitvector[i]=rand()%2;   
  30.  
  31.  while(ch1!=5)
  32.  {
  33.    printf("\n\n1.Print Bit Vector");
  34.    printf("\n\n2.Create File");
  35.    printf("\n\n3.Print Directory");    
  36.    printf("\n\n4.Delete File");
  37.    printf("\n\n5.Exit");
  38.     printf("\nEnter Your Choice :");
  39.     scanf("%d",&ch1);
  40.     
  41. switch(ch1)

  42. case 1:
  43.   for(i=0; i<SIZE; i++)
  44.     printf("%4d",bitvector[i]);
  45.           break;
  46.    
  47.   case 2:
  48.           if(dirst==NULL)
  49.           dirst=dirend=NEWNODE;
  50.              else
  51.               {
  52.                 dirend->next=NEWNODE;
  53.                 dirend=dirend->next;
  54.                }
  55.            dirend->next=NULL;
  56.            printf("\n Enter A Filename :");
  57.            scanf("%s",dirend->fname);
  58.            printf("\nEnter the blocks to allocate :");
  59.            scanf("%d",&n);
  60.            dirend->blist=NULL;
  61.            i=0;
  62.            while(n>0)
  63.             {
  64.             if(bitvector[i]==1)//found ith block free
  65.            
  66.             curr=NEWBLK;
  67.             curr->bno=i;
  68.             curr->next=NULL;
  69.             if(dirend->blist==NULL)
  70.             {
  71.             dirend->start=i;
  72.             dirend->blist=curr;
  73.             prev=curr;
  74.                }
  75.               else
  76.               {
  77.               prev->next=curr;
  78.               prev=curr;
  79.               }
  80.             bitvector[i]=0;//mark ith block allocated
  81.             n--;
  82.             if(n==0)
  83.        break;
  84.        }
  85.        i++;
  86.      }
  87.      dirend->end=i;
  88.      break;
  89.      
  90. case 3:
  91.    printf("\nDirectory");
  92.    printf("\n---------------------------");
  93.        printf("\nFilename   Start      End");       
  94.        printf("\n--------------------------------------"); 
  95.        for(t1=dirst; t1!=NULL; t1=t1->next)
  96.           {
  97.           printf("\n%-14s %5d %3d",t1->fname, t1->start, t1->end);
  98.           printf("(");
  99.           for(curr=t1->blist; curr!=NULL; curr=curr->next)
  100.           {
  101.           printf("%3d",curr->bno);
  102.          
  103.           }
  104.           printf(")");
  105.         } 
  106.        printf("\n---------------------------------------------------");
  107.        break;
  108.        
  109.   case 4:
  110.         printf("\nEnter a Filename :");
  111.         scanf("%s",fname);
  112.         t1=dirst;
  113.         while(t1!=NULL)
  114.         {
  115.         if(strcmp(t1->fname,fname)==0)
  116.    break;
  117.    t2=t1;
  118.    t1=t1->next;
  119.         }  
  120.     if(t1!=NULL)//file found
  121.      {
  122.      for(curr=t1->blist; curr!=NULL;)
  123.      {
  124.       bitvector[curr->bno]=1;
  125.       prev=curr;
  126.       curr=curr->next;
  127.       free(prev);
  128.     }
  129.     
  130.     if(t1==dirst)
  131.      dirst=dirst->next;
  132.     else
  133.       t2->next=t1->next;
  134.           if(dirst==NULL)
  135.                dirend=NULL;
  136.                
  137.                free(t1);
  138.        }
  139.          else
  140.          printf("\nFile not found...\n");
  141.          break;
  142.      }
  143.   }
  144. }

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. 

CODE:

👇

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define SIZE 100
  5. #define NEWNODE (struct direntry*)malloc(sizeof(struct direntry))

  6. struct direntry
  7. {
  8. char fname[14];
  9. int ibno,blist[20],k;
  10. struct direntry *next;
  11. }*dirst=NULL, *dirend=NULL;

  12.  int bitvector[SIZE];
  13.  
  14. void main()
  15. {
  16.  int ch1=0,i,j,n,flag;
  17.  char fname[14];
  18.  int rand();
  19.  struct direntry *t1, *t2;
  20.  for(i=0; i<SIZE;i++)
  21.     bitvector[i]=rand()%2;   
  22.  
  23.  while(ch1!=5)
  24.  {
  25.    printf("\n\n1.Print Bit Vector");
  26.    printf("\n\n2.Create File");
  27.    printf("\n\n3.Print Directory");    
  28.    printf("\n\n4.Delete File");
  29.    printf("\n\n5.Exit");
  30.     printf("\nEnter Your Choice :");
  31.     scanf("%d",&ch1);
  32.     
  33. switch(ch1)

  34. case 1:
  35.   for(i=0; i<SIZE; i++)
  36.     printf("%4d",bitvector[i]);
  37.           break;
  38.    
  39.   case 2:
  40.           if(dirst==NULL)
  41.           dirst=dirend=NEWNODE;
  42.              else
  43.               {
  44.                 dirend->next=NEWNODE;
  45.                 dirend=dirend->next;
  46.                }
  47.            dirend->next=NULL;
  48.            printf("\n Enter A Filename :");
  49.            scanf("%s",dirend->fname);
  50.            printf("\nEnter the blocks to allocate :");
  51.            scanf("%d",&n);
  52.            dirend->k=n;
  53.            i=j=flag=0;
  54.            while(n>0)
  55.             {
  56.             if(bitvector[i]==1)//found ith block free
  57.            
  58.             if(flag==0)
  59.             {
  60.             dirend->ibno=i;
  61.             flag=1;
  62.             }
  63.        else//allocate data block
  64.        {
  65.           dirend->blist[j++]=i;  //store block i in blist[j]
  66.           n--;
  67.         }
  68.       bitvector[i]=0;//mark ith block allocate    
  69.        
  70.         if(n==0)
  71.         
  72.      break;
  73.        }//if
  74.        i++;
  75.      }//while
  76.      
  77.      break;
  78.      
  79. case 3:
  80.    printf("\nDirectory");
  81.    printf("\n---------------------------");
  82.        printf("\nFilename   IndexBlockNo     BlockList");       
  83.        printf("\n--------------------------------------------------------"); 
  84.        for(t1=dirst; t1!=NULL; t1=t1->next)
  85.           {
  86.           printf("\n%-10s %6d ",t1->fname, t1->ibno);
  87.           for(j=0; j<t1->k; j++)
  88.           printf("%6d",t1->blist[j]);
  89.         } 
  90.        printf("\n---------------------------------------------------");
  91.        break;
  92.       
  93.  case 4:
  94.         printf("\nEnter a Filename :");
  95.         scanf("%s",fname);
  96.         t1=dirst;
  97.         while(t1!=NULL)
  98.         {
  99.         if(strcmp(t1->fname,fname)==0)
  100. break;
  101.    t2=t1;
  102.    t1=t1->next;
  103.         }  
  104.     if(t1!=NULL)//file found
  105.      {
  106.      for(j=0; j<t1->k; j++)//mark data block as free
  107.           bitvector[t1->blist[j]]=1;
  108.         bitvector[t1->ibno]=1; 
  109.     if(t1==dirst)
  110.      dirst=dirst->next;
  111.     else
  112.       t2->next=t1->next;
  113.           if(dirst==NULL)
  114.                dirend=NULL;
  115.                
  116.                free(t1);
  117.        }
  118.          else
  119.          printf("\nFile not found...\n");
  120.          break;
  121.      }
  122.   }
  123. }

Comments