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:
👇
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define SIZE 100
- #define NEWNODE (struct direntry*)malloc(sizeof(struct direntry))
- struct direntry
- {
- char fname[14];
- int start,count;
- struct direntry *next;
- }
- *dirst=NULL, *dirend=NULL;
- int bitvector[SIZE];
- void main()
- {
- int ch1=0,i,j,k,n,flag;
- char fname[14];
- int rand();
- struct direntry *t1, *t2;
- for(i=0; i<SIZE;i++)
- {
- bitvector[i]=rand()%2;
- }
- while(1)
- {
- printf("\n\n1.Print Bit Vector");
- printf("\n2.Create File");
- printf("\n3.Print Directory");
- printf("\n4.Delete File");
- printf("\n5.Exit");
- printf("\nEnter your choice :");
- scanf("%d",&ch1);
- switch(ch1)
- {
- case 1:
- for(i=0; i<SIZE; i++)
- printf("%4d",bitvector[i]);
- break;
- case 2:
- if(dirst==NULL)
- dirst=dirend=NEWNODE;
- else
- {
- dirend->next=NEWNODE;
- dirend=dirend->next;
- }
- dirend->next=NULL;
- printf("\n Enter A Filename :");
- scanf("%s",dirend->fname);
- printf("\nEnter the blocks to allocate :");
- scanf("%d",&n);
- dirend->count=n;
- for(i=0; i<100; i++)
- {
- if(bitvector[i]==1)//found free block
- {
- for(j=i; j<i+n; j++)//find next n free block
- {
- if(bitvector[j]!=1)//not free
- break;
- }//for
- if(j==i+n)//found n contiguous free block
- {
- dirend->start=i;
- for(k=i; k<j; k++)//allocate block
- bitvector[k]=0;
- break;
- }
- }
- }
- break;
- case 3:
- printf("\nDirectory :");
- printf("\n---------------------------");
- printf("\nFilename Start Count");
- printf("\n-------------------------------");
- for(t1=dirst; t1!=NULL; t1=t1->next)
- printf("\n%-10s %5d %5d ",t1->fname,t1->start,t1->count);
- printf("\n------------------------------------------------");
- break;
- case 4:
- printf("\nEnter a Filename :");
- scanf("%s",fname);
- t1=dirst;
- while(t1!=NULL)
- {
- if(strcmp(t1->fname,fname)==0)
- break;
- t2=t1;
- t1=t1->next;
- }
- if(t1!=NULL)//file found
- {
- for(i=t1->start;i<t1->start+t1->count; i++)
- bitvector[i]=1;//free blocks
- if(t1==dirst)
- dirst=dirst->next;
- else
- t2->next=t1->next;
- if(dirst==NULL)
- dirend=NULL;
- free(t1);
- }
- else
- printf("\nFile not found...\n");
- break;
- case 5:
- exit(0);
- }
- }
- }
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:
👇
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define SIZE 100
- #define NEWNODE (struct direntry*)malloc(sizeof(struct direntry))
- #define NEWBLK (struct blknode*)malloc(sizeof(struct blknode))
- struct blknode
- {
- int bno;
- struct blknode *next;
- }*curr,*prev;
- struct direntry
- {
- char fname[14];
- int start,end;
- struct blknode *blist;
- struct direntry *next;
- }
- *dirst=NULL, *dirend=NULL;
- int bitvector[SIZE];
- void main()
- {
- int ch1=0,i,j,n;
- char fname[14];
- int rand();
- struct direntry *t1, *t2;
- for(i=0; i<SIZE;i++)
- bitvector[i]=rand()%2;
- while(ch1!=5)
- {
- printf("\n\n1.Print Bit Vector");
- printf("\n\n2.Create File");
- printf("\n\n3.Print Directory");
- printf("\n\n4.Delete File");
- printf("\n\n5.Exit");
- printf("\nEnter Your Choice :");
- scanf("%d",&ch1);
- switch(ch1)
- {
- case 1:
- for(i=0; i<SIZE; i++)
- printf("%4d",bitvector[i]);
- break;
- case 2:
- if(dirst==NULL)
- dirst=dirend=NEWNODE;
- else
- {
- dirend->next=NEWNODE;
- dirend=dirend->next;
- }
- dirend->next=NULL;
- printf("\n Enter A Filename :");
- scanf("%s",dirend->fname);
- printf("\nEnter the blocks to allocate :");
- scanf("%d",&n);
- dirend->blist=NULL;
- i=0;
- while(n>0)
- {
- if(bitvector[i]==1)//found ith block free
- {
- curr=NEWBLK;
- curr->bno=i;
- curr->next=NULL;
- if(dirend->blist==NULL)
- {
- dirend->start=i;
- dirend->blist=curr;
- prev=curr;
- }
- else
- {
- prev->next=curr;
- prev=curr;
- }
- bitvector[i]=0;//mark ith block allocated
- n--;
- if(n==0)
- break;
- }
- i++;
- }
- dirend->end=i;
- break;
- case 3:
- printf("\nDirectory");
- printf("\n---------------------------");
- printf("\nFilename Start End");
- printf("\n--------------------------------------");
- for(t1=dirst; t1!=NULL; t1=t1->next)
- {
- printf("\n%-14s %5d %3d",t1->fname, t1->start, t1->end);
- printf("(");
- for(curr=t1->blist; curr!=NULL; curr=curr->next)
- {
- printf("%3d",curr->bno);
- }
- printf(")");
- }
- printf("\n---------------------------------------------------");
- break;
- case 4:
- printf("\nEnter a Filename :");
- scanf("%s",fname);
- t1=dirst;
- while(t1!=NULL)
- {
- if(strcmp(t1->fname,fname)==0)
- break;
- t2=t1;
- t1=t1->next;
- }
- if(t1!=NULL)//file found
- {
- for(curr=t1->blist; curr!=NULL;)
- {
- bitvector[curr->bno]=1;
- prev=curr;
- curr=curr->next;
- free(prev);
- }
- if(t1==dirst)
- dirst=dirst->next;
- else
- t2->next=t1->next;
- if(dirst==NULL)
- dirend=NULL;
- free(t1);
- }
- else
- printf("\nFile not found...\n");
- break;
- }
- }
- }
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:
👇
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define SIZE 100
- #define NEWNODE (struct direntry*)malloc(sizeof(struct direntry))
- struct direntry
- {
- char fname[14];
- int ibno,blist[20],k;
- struct direntry *next;
- }*dirst=NULL, *dirend=NULL;
- int bitvector[SIZE];
- void main()
- {
- int ch1=0,i,j,n,flag;
- char fname[14];
- int rand();
- struct direntry *t1, *t2;
- for(i=0; i<SIZE;i++)
- bitvector[i]=rand()%2;
- while(ch1!=5)
- {
- printf("\n\n1.Print Bit Vector");
- printf("\n\n2.Create File");
- printf("\n\n3.Print Directory");
- printf("\n\n4.Delete File");
- printf("\n\n5.Exit");
- printf("\nEnter Your Choice :");
- scanf("%d",&ch1);
- switch(ch1)
- {
- case 1:
- for(i=0; i<SIZE; i++)
- printf("%4d",bitvector[i]);
- break;
- case 2:
- if(dirst==NULL)
- dirst=dirend=NEWNODE;
- else
- {
- dirend->next=NEWNODE;
- dirend=dirend->next;
- }
- dirend->next=NULL;
- printf("\n Enter A Filename :");
- scanf("%s",dirend->fname);
- printf("\nEnter the blocks to allocate :");
- scanf("%d",&n);
- dirend->k=n;
- i=j=flag=0;
- while(n>0)
- {
- if(bitvector[i]==1)//found ith block free
- {
- if(flag==0)
- {
- dirend->ibno=i;
- flag=1;
- }
- else//allocate data block
- {
- dirend->blist[j++]=i; //store block i in blist[j]
- n--;
- }
- bitvector[i]=0;//mark ith block allocate
- if(n==0)
- break;
- }//if
- i++;
- }//while
- break;
- case 3:
- printf("\nDirectory");
- printf("\n---------------------------");
- printf("\nFilename IndexBlockNo BlockList");
- printf("\n--------------------------------------------------------");
- for(t1=dirst; t1!=NULL; t1=t1->next)
- {
- printf("\n%-10s %6d ",t1->fname, t1->ibno);
- for(j=0; j<t1->k; j++)
- printf("%6d",t1->blist[j]);
- }
- printf("\n---------------------------------------------------");
- break;
- case 4:
- printf("\nEnter a Filename :");
- scanf("%s",fname);
- t1=dirst;
- while(t1!=NULL)
- {
- if(strcmp(t1->fname,fname)==0)
- break;
- t2=t1;
- t1=t1->next;
- }
- if(t1!=NULL)//file found
- {
- for(j=0; j<t1->k; j++)//mark data block as free
- bitvector[t1->blist[j]]=1;
- bitvector[t1->ibno]=1;
- if(t1==dirst)
- dirst=dirst->next;
- else
- t2->next=t1->next;
- if(dirst==NULL)
- dirend=NULL;
- free(t1);
- }
- else
- printf("\nFile not found...\n");
- break;
- }
- }
- }
Comments
Post a Comment