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

A2_Linked

This C program simulates the Linked File Allocation Method for managing disk space. It allows users to allocate and deallocate space for files, display used and free space on the disk, and manage file entries using linked lists. The program includes a menu-driven interface for user interaction and handles memory allocation dynamically.

Uploaded by

thirdeye353
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

A2_Linked

This C program simulates the Linked File Allocation Method for managing disk space. It allows users to allocate and deallocate space for files, display used and free space on the disk, and manage file entries using linked lists. The program includes a menu-driven interface for user interaction and handles memory allocation dynamically.

Uploaded by

thirdeye353
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//Q) Write a C program to simulate Linked File Allocation Method.

Assume Disk of
size 'd', value of which should be taken from user. Use seperate tables(implemented
using linked lists) to keep tack of Used & Free space respectively. Make use of the
following Menu to perform Operations:
// - Allocate space for newly created file.
// - Deallocate space for now-deleted file.
// - Show Used and/or Free Space on Disk.
// - Exit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct File_tab
{
char name[10];
int start,end;
struct File_tab *next;
}File;
typedef struct BLOCK
{
int data;
int next;
}block;
File *first=NULL;
block *disk;
int ch,i,fcnt,acnt,size;
//initialization of disk blocks
void init()
{
for(i=0;i<size;i++)
{
disk[i].next=-1;
disk[i].data=0; //to free block
}
}
int search() //s is size of file
{
int cnt=0,j;
for(i=0;i<size;i++) //blcok size
{

if(disk[i].data==0) //allocated
return i;

}
return -1;
}
void allocate()
{
File *f1,*f2; //file table implementation
int res,fsize,j,cnt;
f1=(File *)malloc (sizeof(File));
printf("\nEnter the file name ");
scanf("%s",&(f1->name));
printf("\nEnter the file size ");
scanf("%d",&(fsize));
if(fcnt<fsize) //freecnt
printf("\nMemeory not availbale");
else
{
res=search(); //index of free block
f1->start=res;
f1->next=NULL;
cnt=0;
disk[res].data=1; //allocated
fcnt--;
acnt++;

for(i=res;i<size;i++)
{
j=i+1;
for(;cnt<fsize-1;j++)
{
if(disk[j].data==0)
{
disk[i].next=j;
disk[j].data=1;
fcnt--;
acnt++;
cnt++;
i=j;
}//if
}//for j
if(cnt==fsize-1)
{
f1->end=j-1;
disk[j-1].next=-1; //last block
break;
}
}//for i;

//table
if(first==NULL) //first entry
first=f1;
else
{
f2=first; //add entry at last
while(f2->next!=NULL)
f2=f2->next;
f2->next=f1;
}
}
}
void show()
{
File *ptr;
ptr=first;

printf("\nFilename start size \n");


while(ptr!=NULL)
{
printf("\n%s %d %d",ptr->name,ptr->start,ptr->end);
ptr=ptr->next;
}
printf("\nBlock Flag ");
for(i=0;i<size;i++)
{
printf("\n%d %s",i,disk[i].data==1?"allocated":"free");
}
printf("\nTotal freeblocks are %d",fcnt);
printf("\nTotal allocated blocks are %d",acnt);
}
void deallocate()
{
char name[10];
File *ptr,*p1;
printf("\nEnter the file to be deallocate ");
scanf("%s",name);
ptr=first;

//delete from file table


while(ptr!=NULL)
{
if(strcmp(ptr->name,name)==0)
{
for(i=ptr->start; i!=-1;)
{
disk[i].data=0;
i=disk[i].next;
fcnt++;
acnt--;
}
p1=ptr;
if(ptr==first)
first=first->next;
else
{
ptr=ptr->next;
}
free(p1);
printf("\nFile is deallocated ");
break;
}
ptr=ptr->next;
}
if(ptr==NULL)
printf("\nFile not found ");
}

main()
{
printf("Enter the size of disk ");
scanf("%d",&size);
fcnt=size;
acnt=0;

disk=(block *)malloc(sizeof(block)*size);
init();
while(1)
{
printf("\n\n\tMenu \n\n\t1.Allocate the memory for file\n\n\t2.Deallocate");
printf("\n\n\t3.show used and free space from disk \n\n\t4.Exit ");
printf("\n\n\tEnter the choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: allocate();
break;
case 2 :deallocate();
break;
case 3 :show();
break;
case 4: exit(0);
}
}
}

You might also like