0% found this document useful (0 votes)
4 views

File Allocation

The document contains two C programs that simulate file allocation methods: Contiguous and Linked. Each program includes functionalities for showing a bit vector, creating and deleting files, and displaying a directory of files. The programs utilize structures and linked lists to manage file allocation and deallocation on a simulated disk with a specified number of blocks.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

File Allocation

The document contains two C programs that simulate file allocation methods: Contiguous and Linked. Each program includes functionalities for showing a bit vector, creating and deleting files, and displaying a directory of files. The programs utilize structures and linked lists to manage file allocation and deallocation on a simulated disk with a specified number of blocks.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Question 1: Write a program to simulate Contiguous file allocation method.

Assume disk with n number


of blocks. Give value of n as input. Randomly mark some block as allocated and accordingly maintain
the list of free blocks. Write menu driver program with menu options as mentioned above and implement
each option.
• Show Bit Vector
• Create New File
• Show Directory
• Delete File
• Exit

Slip No: 8, 14, 16

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int n, i, j, freecount, file_count=0;


int bv[50];
int blocks_required, no_free_blocks, SBlock;
char fname[20];

struct dir
{
char filename[20];
int startblock, fileLen;
}directory[50];

void initBV()
{
for(i=0;i<n;i++)
{
bv[i]=rand()%2;
}
}

void displayBV()
{
printf("\nBit vector Contents:\n");
for(i=0;i<n;i++)
{
printf("%d",bv[i]);
}
}

void createFile()
{
printf("\nEnter file name:");
scanf("%s",fname);

if(file_count>50)
{
printf("Error:Maximum no.of files reached\n");
return;
}
for(i=0;i<n;i++)
{
if(strcmp(directory[i].filename,fname)==0)
{
printf("File Name already exist...");
return;
}
}

no_free_blocks=0;
for(i=0;i<n;i++)
{
if(bv[i]==0)
{
no_free_blocks++;
}
}

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


scanf("%d",&SBlock);

printf("Enter no_of Blocks required:");


scanf("%d",&blocks_required);

if(blocks_required > no_free_blocks)


{
printf("\nNo enough blocks are available..");
return;
}

// To search availiability of countinuous blocks


freecount=0;
for(i=SBlock;i<n;i++)
{
if(bv[i]==1)
break;
if(bv[i]==0)
freecount++;
if(freecount==blocks_required)
break;
}

if(freecount<blocks_required)
{
printf("Error:Not enough contigious free blocks to allocate the file!\n");
return;
}

// to add file details in directory


strcpy(directory[file_count].filename,fname);
directory[file_count].startblock=SBlock;
directory[file_count].fileLen=blocks_required;

for(i=SBlock;i<SBlock+blocks_required;i++)
{
bv[i]=1;
}
file_count++;
printf("File %s is created successfully..\n",fname);
}

void displayDirectory()
{
printf("Directory Contents:\n");
printf("%-20s %-12s %-7s %-15s\n", "FileName", "startBlock", "Length","Allocated Blocks");

for(i=0;i<file_count;i++)
{
printf("%-20s %-12d %-7d ", directory[i].filename, directory[i].startblock, directory[i].fileLen);
for (j=directory[i].startblock; j<directory[i].startblock+directory[i].fileLen; j++)
{
printf("%d, ",j);
}
printf("\n");
}
}

void deleteFile()
{
printf("\nEnter file name:");
scanf("%s",fname);

for(i=0;i<file_count;i++)
{
if(strcmp(directory[i].filename,fname)==0)
{
SBlock=directory[file_count].startblock;
blocks_required=directory[file_count].fileLen;
for(i=SBlock;i<SBlock+blocks_required;i++)
{
bv[i]=0;
}
break;
}
}

for ( ; i<file_count; i++)


{
directory[i] = directory[i+1];
}
file_count--;
}

void main()
{
int choice=0;
printf("Enter the number of Disk Blocks:\n");
scanf("%d",&n);
no_free_blocks=n;
initBV();
do
{
printf("\n********Menu********\n");
printf("\n1.Show Bit Vector”):
printf("\n2.Create New File”);
printf("\n3.Show Directory\n”);
printf(“\n4.Delete File”);
printf("\n5.Exit\n");
printf("\nEnter your choice:\n");
scanf("%d",&choice);

switch(choice)
{
case 1: displayBV();
break;
case 2: createFile();
break;
case 3: displayDirectory();
break;
case 4: deleteFile();
break;
case 5: break;

default:printf("Sorry!!! You have selected wrong option\n");


}
} while (choice!=5);

Question 2: Write a program to simulate Linked file allocation method. Assume disk with n number of
blocks. Give value of n as input. Randomly mark some block as allocated and accordingly maintain the
list of free blocks Write menu driver program with menu options as mentioned below and implement
each option.
• Show Bit Vector
• Create New File
• Show Directory
• Delete File
• Exit

Slip No: 2, 6, 15, 25

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 100

struct NODE
{
int data;
struct NODE *next;
};
struct direct
{
char filename[20];
int start, end;
struct NODE *listhead;
}directory[20];

struct NODE *head, *temp, *last;

int no_free_blocks, blocks_required;


int bv[MAX], i, j, n, file_count;

void init_bitVector()
{
int i;
printf("Enter total no. of disk blocks : ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
bv[i]=rand()%2;
}
file_count=0;
head=NULL;
}

void display_bitvector()
{
int i;
for (i=0; i<n; i++)
printf("%d ",bv[i]);
printf("\n");
}

void display_dir()
{
if (file_count==0)
{
printf("No file is created yet\n");
return;
}
printf("\nDirectory : ");
printf("\nFile Name Start Block Number End Block Number Allocated blocks\n");
for (i=0; i<file_count; i++)
{
printf("%s %10d %10d\t", directory[i].filename, directory[i].start, directory[i].end);
for (temp=directory[i].listhead; temp!=NULL; temp=temp->next)
{
printf("%d->",temp->data);
}
printf("NULL\n\n");
}
}
void createFile()
{
char fname[30];
no_free_blocks =0;
printf("Enter file name : ");
scanf("%s", fname);
printf("Enter the no of blocks : ");
scanf("%d",&blocks_required);

// count no of free blocks


for (i=0;i<n;i++)
{
if (bv[i]==0)
no_free_blocks ++;
}

// check availiability of sufficient blocks for allocation


if (blocks_required > no_free_blocks)
{
printf("Failed to create file %s\n",fname);
return;
}

// check for duplication of file name


for(i=0; i<file_count; i++)
{
if (strcmp(directory[i].filename, fname) == 0)
{
printf("File %s already exist\n");
return;
}
}

// make entry for new file in directory


strcpy(directory[file_count].filename, fname);
directory[file_count].start=-1;
head=NULL;
// allocating blocks for new file
for (i=0;i<n;i++)
{
if (bv[i]==0)
{
temp=(struct NODE *)malloc(sizeof(struct NODE));
blocks_required --;
bv[i]=1;
temp->data=i;
temp->next=NULL;
if (directory[file_count].start == -1)
{
directory[file_count].start = i;
directory[file_count].listhead = temp;
}
if (head==NULL)
head=temp;
else
last->next=temp;
last=temp;
}
last->next=NULL;
if (blocks_required == 0)
break;
}

directory[file_count].end=i;
file_count++;
printf("File %s created successfully \n",fname);
}

void deleteFile()
{
int dirIndex;
char fname[30];
printf("Enter file name : ");
scanf("%s", fname);

// search for a file entry in the directory and de-allocate all blocks
for(i=0; i<file_count; i++)
{
if (strcmp(directory[i].filename, fname) == 0)
{
dirIndex=i;
temp = head = directory[i].listhead;
while ( temp != NULL)
{
bv[temp->data]=0;
temp = head;
head = head->next;
free(temp);
}
}
}

// removing file entry from directory


for(i=dirIndex; i<file_count; i++)
{
directory[i]=directory[i+1];
}
file_count--;
}

int main()
{
int choice;
init_bitVector();
while(1)
{
printf("\n*******Menu*******\n");
printf("\n1 : Show bit Vector");
printf("\n2 : Create New File");
printf("\n3 : Show Directory");
printf("\n4 : Delete file");
printf("\n5 : Exit\n");
printf("\nEnter Your Choice : \n");
scanf("%d",&choice);

switch (choice)
{
case 1: display_bitvector();
break;
case 2: createFile();
break;
case 3: display_dir();
break;
case 4: deleteFile();
break;
case 5: return;
default: printf("Invalid option\n");
}
}
}

You might also like