-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential.c
159 lines (152 loc) · 3.04 KB
/
sequential.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
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
#include<stdio.h>
#define maxsize 100
struct fileEntry{
char *name;
int start;
int length;
}fileEntry;
int disk[maxsize] = { 0 };
int freeSpace = maxsize;
struct fileEntry files[30];
void init(){
int i;
for (i = 0; i < 30; i++){
files[i].name = NULL;
}
}
int getEmptySlot(){
int i;
for (i = 0; i < 30; i++){
if (files[i].name == NULL)
return i;
}
return -1;
}
void insertFile(char *name, int blocks){
if (blocks > freeSpace){
printf("\nFile size too big\n");
return;
}
if (searchFile(name) != -1){
printf("\nCannot insert the file\n");
return;
}
int i, j, free = 0;
for (i = 0; i<maxsize; i++){
if (disk[i] == 0)
free++;
else
free = 0;
if (free == blocks){
break;
}
}
if (free != blocks){
printf("\nCannot insert the file\n");
return;
}
int temp = i;
i -= blocks-1;
int slot = getEmptySlot();
files[slot].name = malloc(0);
strcpy(files[slot].name, name);
files[slot].length = blocks;
files[slot].start = i;
freeSpace -= blocks;
for (i; i <= temp; i++){
disk[i] = 1;
}
printf("Inserted\n");
}
void deleteFile(char *name){
int pos;
if ((pos = searchFile(name)) == -1){
printf("\nFile not found\n");
return;
}
int i;
for (i = files[pos].start; i<(files[pos].start + files[pos].length); i++){
disk[i] = 0;
}
freeSpace += files[pos].length - files[pos].start;
files[pos].name = NULL;
printf("Deleted\n");
}
int searchFile(char *name){
int i;
for (i = 0; i<30; i++){
if (files[i].name != NULL && strcmp(files[i].name, name) == 0)
return i;
}
return -1;
}
void displaySize(){
printf("Free space in disk = %d", freeSpace);
}
void displayDisk(){
int i;
printf("\nDISK:\n\n\t0\t1\t2\t3\t4\t5\t6\t7\t8\t9\n");
for (i = 0; i<maxsize; i++){
if (i % 10 == 0)
printf("\n%d\t",i);
if (disk[i] == 0)
printf("%d\t", 0);
else
printf("%d\t", 1);
}
printf("\n");
}
void displayFiles(){
int i;
printf("Files in disk:\n");
printf("Name\tStart\tLength\n");
for (i = 0; i < 30; i++){
if (files[i].name != NULL){
printf("%s\t%4d\t%3d\n", files[i].name, files[i].start, files[i].length);
}
}
printf("\n");
}
int main(){
int option;
char* name = (char*)malloc(20*sizeof(char));
int blocks, start;
init();
printf("Sequential File allocation technique\n\n");
printf("\n1. Insert a File");
printf("\n2. Delete a File");
printf("\n3. Display the disk");
printf("\n4. Display all files");
printf("\n5. Exit\n");
while (1){
displaySize();
printf("\nEnter option: ");
scanf_s("%d", &option);
switch (option){
case 1:
printf("Enter file name: ");
getchar();
gets_s(name, 20);
printf("Enter number of blocks: ");
scanf_s("%d", &blocks);
insertFile(name, blocks);
break;
case 2:
printf("Enter file name to delete: ");
getchar();
gets_s(name, 20);
deleteFile(name);
break;
case 3:
displayDisk();
break;
case 4:
displayFiles();
break;
case 5:
exit(1);
default:
exit(1);
}
}
}