-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexed_allocation.c
130 lines (128 loc) · 2.43 KB
/
Indexed_allocation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
#include<stdio.h>
#include<stdlib.h>
int count=1,a[200];
struct file
{
int name;
int numblocks;
int indblock[10];
int indtab[20];
}f[100];
int create(int n,int tab)
{
int t=0,ti=0,pt=0;
printf("enter file name you want to create\n");
scanf("%d",&f[count].name);
printf("enter number of blocks in file\n");
scanf("%d",&f[count].numblocks);
int it=rand()%n;
if(a[it]==-1)
{
f[count].indblock[pt++]=it;
a[it]=f[count].name;
}
for(int i=0;i<f[count].numblocks;i++)
{
int p=rand()%n;
if(a[p]==-1 && t<=tab)
{
a[p]=f[count].name;
f[count].indtab[ti++]=p;
t++;
}
else
{
int pu=rand()%n;
f[count].indblock[pt++]=pu;
a[pu]=f[count].name;
t=0;
}
}
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int del(int n,int tab)
{
int name;
printf("enter the file you want to delete\n");
scanf("%d",&name);
f[name].name=-1;
int o=f[name].numblocks/tab;
for(int q=0;q<o;q++)
{
a[f[name].indblock[q]]=-1;
}
for(int ik=0;ik<f[name].numblocks;ik++)
{
a[f[name].indtab[ik]]=-1;
}
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int traverse(int n,int tab)
{
printf("file name\t index blocks\t blocks in index block\n");
for(int i=1;i<=count;i++)
{
//printf("i:%d\n",i);
if(f[i].name!=-1)
{
printf("%d\t ",f[i].name);
int o=f[i].numblocks/tab;
for(int ip=0;ip<=o;ip++)
{
//printf("hello");
printf("%d ",f[i].indblock[ip]);
}
printf("\t\t");
for(int ik=0;ik<f[i].numblocks;ik++)
{
//printf("hii\n");
printf("%d ",f[i].indtab[ik]);
}
printf("\n");
}
}
}
int main()
{
int n,size,t=0,tab,choice;
printf("enter total number of blocks\n");
scanf("%d",&n);
printf("enter block size\n");
scanf("%d",&size);
for(int i=0;i<n;i++)
{
a[i]=-1;
}
printf("enter the index table size\n ");
scanf("%d",&tab);
int it[tab];
do
{
printf("enter your choice\n");
printf("1.create 2.del 3.traverse\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("file is being created\n");
create(n,tab);
printf("file created sucessfully\n");
break;
case 2 : printf ("file is being deleted\n");
del(n,tab);
break;
case 3 : printf("file is being traversed\n");
traverse(n,tab);
break;
}
printf("do u want to continue press 1 if u wish to continue\n");
scanf("%d",&t);
}while(t==1);
}