-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
154 lines (152 loc) · 5.54 KB
/
FileSystem.cpp
File metadata and controls
154 lines (152 loc) · 5.54 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILE_SIZE 100
#define MAX_FILE_NAME 20
#define DISK_SIZE 1000
typedef struct {
char name[MAX_FILE_NAME];
int start_block;
int size;
} file;
int allocate_blocks_contiguous(int size, int *disk) {
int start_block = -1;
int consecutive_blocks = 0;
for (int i = 0; i < DISK_SIZE; i++) {
if (disk[i] == 0) {
if (consecutive_blocks == 0) {
start_block = i;
}
consecutive_blocks++;
if (consecutive_blocks == size) {
for (int j = start_block; j < start_block + size; j++) {
disk[j] = 1;
}
return start_block;
}
} else {
consecutive_blocks = 0;
}
}
return -1;
}
void delete_file(file *files, int *file_count, char *name, int *disk) {
for (int i = 0; i < *file_count; i++) {
if (strcmp(files[i].name, name) == 0) {
for (int j = files[i].start_block; j < files[i].start_block + files[i].size; j++) {
disk[j] = 0;
}
for (int j = i; j < *file_count - 1; j++) {
files[j] = files[j+1];
}
(*file_count)--;
printf("File %s deleted\n", name);
return;
}
}
printf("File %s not found\n", name);
}
void rename_file(file *files, int file_count, char *old_name, char *new_name) {
for (int i = 0; i < file_count; i++) {
if (strcmp(files[i].name, old_name) == 0) {
strcpy(files[i].name, new_name);
printf("File %s renamed to %s\n", old_name, new_name);
return;
}
}
printf("File %s not found\n", old_name);
}
void move_file(file *files, int file_count, char *name, int new_start_block, int *disk) {
for (int i = 0; i < file_count; i++) {
if (strcmp(files[i].name, name) == 0) {
int old_start_block = files[i].start_block;
int size = files[i].size;
if (new_start_block + size > DISK_SIZE) {
printf("Error: Not enough space to move file\n");
return;
}
for (int j = old_start_block; j < old_start_block + size; j++) {
disk[j] = 0;
}
files[i].start_block = new_start_block;
for (int j = new_start_block; j < new_start_block + size; j++) {
disk[j] = 1;
}
printf("File %s moved from block %d to block %d\n", name, old_start_block, new_start_block);
return;
}
}
printf("File %s not found\n", name);
}
int main() {
int disk[DISK_SIZE] = {0};
int file_count = 0;
file files[MAX_FILE_SIZE];
for (int time = 0; time < 10; time++) {
char new_file_name[MAX_FILE_NAME];
int new_file_size;
printf("Enter name and size of new file: ");
scanf("%s %d", new_file_name, &new_file_size);
if (new_file_size > DISK_SIZE) {
printf("Error: File size too large\n");
continue;
}
int start_block = allocate_blocks_contiguous(new_file_size, disk);
if (start_block == -1) {
printf("Error: Not enough contiguous space to create file\n");
continue;
}
file new_file;
strcpy(new_file.name, new_file_name);
new_file.start_block = start_block;
new_file.size = new_file_size;
files[file_count] = new_file;
file_count++;
printf("File %s created with size %d starting at block %d\n", new_file_name, new_file_size, start_block);
char delete_file_name[MAX_FILE_NAME];
printf("Enter name of file to delete: ");
scanf("%s", delete_file_name);
delete_file(files, &file_count, delete_file_name, disk);
char old_name[MAX_FILE_NAME];
char new_name[MAX_FILE_NAME];
printf("Enter name of file to rename: ");
scanf("%s", old_name);
printf("Enter new name for file: ");
scanf("%s", new_name);
rename_file(files, file_count, old_name, new_name);
char move_file_name[MAX_FILE_NAME];
int new_start_block;
printf("Enter name of file to move: ");
scanf("%s", move_file_name);
printf("Enter new start block for file: ");
scanf("%d", &new_start_block);
move_file(files, file_count, move_file_name, new_start_block, disk);
printf("Disk state at end of time unit %d:\n", time);
for (int i = 0; i < DISK_SIZE; i++) {
printf("%d ", disk[i]);
}
printf("\n");
int consecutive_free_blocks = 0;
int fragment_count = 0;
int wasted_blocks = 0;
for (int i = 0; i < DISK_SIZE; i++) {
if (disk[i] == 0) {
consecutive_free_blocks++;
} else {
if (consecutive_free_blocks > 0) {
fragment_count++;
wasted_blocks += consecutive_free_blocks;
consecutive_free_blocks = 0;
}
}
}
if (consecutive_free_blocks > 0) {
fragment_count++;
wasted_blocks += consecutive_free_blocks;
}
float avg_fragment_size = (float) wasted_blocks / fragment_count;
printf("Average fragment size: %.2f\n", avg_fragment_size);
printf("Number of wasted blocks: %d\n", wasted_blocks);
}
return 0;
}