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

index allocation

The document presents a C program that implements an indexed file allocation system, allowing users to create, delete, and display files and free blocks on a simulated disk. Key functions include setting inode information, managing directory entries, and formatting the disk. The program provides a command-line interface for user interaction with options to create files, display free blocks, show directory contents, and delete files.

Uploaded by

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

index allocation

The document presents a C program that implements an indexed file allocation system, allowing users to create, delete, and display files and free blocks on a simulated disk. Key functions include setting inode information, managing directory entries, and formatting the disk. The program provides a command-line interface for user interaction with options to create files, display free blocks, show directory contents, and delete files.

Uploaded by

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

/*--------- Indexed File Allocation -----------------*/

#include<stdio.h>
#include<string.h>
#define super 0
#define blksz 64
#define root 16

unsigned char mem[256][blksz];


void Setinode(int inode,int filesz,int start,int index)
{ int i,j;
i=inode/16 + 1;
j=(inode%16) * 4;
mem[i][j]=inode;
mem[i][j+1]=filesz;
mem[i][j+2]=start;
mem[i][j+3]=index;
}
void Setindex(int index, int blk)
{ int i;
for(i=0;i<blksz; i++)
{ if(mem[index][i]==0) break;
}
mem[index][i]=blk;
}
void dispindexblk(int indx)
{ int i;
for(i=0;i<blksz; i++)
{ if(mem[indx][i]==0) break;
printf("%3d",mem[indx][i]);
}
}
void Format()
{ int i,j;
for(i=0;i<256;i++)
for(j=0;j<blksz;j++) mem[i][j]=0;

mem[super][0]=15*16;
mem[super][1]=1;
mem[super][2]=15;
mem[super][3]=18;
mem[super][4]=16;
Setinode(0,blksz,16,17);
Setindex(17,16);
for(i=18; i<256; i++) mem[i][0]=i+1;
mem[255][0]=0;
}
int AddDirEntry(int blk,char *fname)
{int i,j;
for(i=0;i<blksz;i=i+8)
{ if(mem[blk][i]==0) break;
}
mem[blk][i]=mem[super][1];
mem[super][1]++;
for(j=0; j<8 && fname[j]!='\0'; j++) mem[blk][i+j+1]=fname[j];

return mem[blk][i];
}
void Create()
{
char fn[30]; int i,inode,fsz,strtblk,indxblk,next;
printf("\nFile Name : ");
fflush(stdin);
scanf("%s",&fn);
printf("\nFile Size(in blocks) = "); scanf("%d",&fsz);
inode=AddDirEntry(root,fn);

indxblk=mem[super][3];
mem[super][3]=mem[indxblk][0];

strtblk=mem[super][3];
mem[super][3]=mem[strtblk][0];
mem[indxblk][0]=strtblk;

for(i=1; i<fsz;i++)
{ next=mem[super][3];
mem[indxblk][i]=next;
mem[super][3]=mem[next][0];
}
Setinode(inode,fsz,strtblk,indxblk);
printf("%s file created successfully...\n",fn);
}
void DispFreeBlks()
{ int i=mem[super][3];
printf("\nFree Space(Blocks) On the Disk : \n");
do
{ printf("%5d",i);
i=mem[i][0];
}while(mem[i][0]!=0);
}
void DispDir()
{
int i,j,inode,blk,k;
printf("Space(Blocks) Used by %s File is : \n\n",fn);
printf("\n inode File Size Startblk index");
printf("\n---------------------------------------");
for(i=0;i<blksz;i=i+8)
{ if(mem[root][i]==0) continue;
inode=mem[root][i];
printf("\n%4d ",inode);
for(j=i+1;j<i+8; j++) putchar(mem[root][j]);
blk=inode/16+1;
k=(inode%16)*4;
printf(" %4d %4d %4d |",mem[blk][k+1],mem[blk][k+2],mem[blk][k+3]);
dispindexblk(mem[blk][k+3]);
}
}
void Delete()
{ char fn[30]; int inode,i,j,k,blk,datablk,indxblk,strtblk,fsz;
printf("\nFile Name : "); fflush(stdin); scanf("%s",&fn);
for(i=0;i<blksz;i=i+8)
{ if(mem[root][i]==0) continue;

if(!strncmp(&mem[root][i+1],fn,7)) break;
}
if(i>=blksz) printf("\nSuch file does not exist!\n");
else
{ inode=mem[root][i];
mem[root][i]=0;
for(j=i+1;j<i+8; j++)
{
mem[root][j]=0;
}
blk=inode/16+1;
k=(inode%16)*4;
fsz=mem[blk][k+1];
strtblk=mem[blk][k+2];
indxblk=mem[blk][k+3];
for(j=k;j<k+4; j++) mem[blk][j]=0;

for(j=0;j<fsz;j++)
{ datablk=mem[indxblk][j];
mem[datablk][0]=mem[indxblk][j+1];
mem[indxblk][j]=0;
}
mem[indxblk][0]=strtblk;
mem[datablk][0]=mem[super][3];
mem[super][3]=indxblk;
printf("%s Deleted successfully..!\n\n",fn);
}
}
main()
{
Format();
int ch;
while(1)
{
printf("\n1:Create File\n2:Display free blks\n3:Disp Dir\n4:Delete File");
printf("\n0:Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : Create(); break;
case 2 : DispFreeBlks(); break;
case 3 : DispDir(); break;
case 4 : Delete(); break;
case 0 : exit(0);
default : printf("\nPlease Choose Correct Choice..!");
}
}
}
/*
[root@localhost ~]# ./a.out

1:Create File
2:Display free blks
3:Disp Dir
4:Delete File
0:Exit
Enter your choice : 1

File Name : swap.txt


File Size(in blocks) = 10
swap.txt file created successfully...
1:Create File
2:Display free blks
3:Disp Dir
4:Delete File
0:Exit
Enter your choice : 2

Free Block List :


29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
242 243 244 245 246 247 248 249 250 251 252 253 254
1:Create File
2:Display free blks
3:Disp Dir
4:Delete File
0:Exit
Enter your choice : 3

inode File Size Startblk index


-----------------------------------
1 swap.tx 10 19 18 | 19 20 21 22 23 24 25 26 27 28
116 0 0 0 |240 2 15 29 16
1:Create File
2:Display free blks
3:Disp Dir
4:Delete File
0:Exit
Enter your choice : 4

File Name : swap.txt


swap.txt Deleted successfully..!
1:Create File
2:Display free blks
3:Disp Dir
4:Delete File
0:Exit
Enter your choice : 0

You might also like