-
Notifications
You must be signed in to change notification settings - Fork 12
/
disk_emu.c
182 lines (150 loc) · 4.6 KB
/
disk_emu.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "disk_emu.h"
FILE* fp = NULL;
double L, p;
double r;
int BLOCK_SIZE, MAX_BLOCK, MAX_RETRY, lru;
/*----------------------------------------------------------*/
/*Close the disk file filled when you don't need it anymore. */
/*----------------------------------------------------------*/
int close_disk()
{
if(NULL != fp)
{
fclose(fp);
}
return 0;
}
/*---------------------------------------*/
/*Initializes a disk file filled with 0's*/
/*---------------------------------------*/
int init_fresh_disk(char *filename, int block_size, int num_blocks)
{
int i, j;
/*Set up latency at 0.02 second*/
L = 00000.f;
/*Set up failure at 10%*/
p = -1.f;
/*Set up max retry attempts after failure to 3*/
MAX_RETRY = 3;
BLOCK_SIZE = block_size;
MAX_BLOCK = num_blocks;
/*Initializes the random number generator*/
srand((unsigned int)(time( 0 )) );
/*Creates a new file*/
fp = fopen (filename, "w+b");
if (fp == NULL)
{
printf("Could not create new disk file %s\n\n", filename);
return -1;
}
/*Fills the file with 0's to its given size*/
for (i = 0; i < MAX_BLOCK; i++)
{
for (j = 0; j < BLOCK_SIZE; j++)
{
fputc(0, fp);
}
}
return 0;
}
/*----------------------------*/
/*Initializes an existing disk*/
/*----------------------------*/
int init_disk(char *filename, int block_size, int num_blocks)
{
/*Set up latency at 0.02 second*/
L = 00000.f;
/*Set up failure at 10%*/
p = -1.f;
/*Set up max retry attempts after failure to 3*/
MAX_RETRY = 3;
BLOCK_SIZE = block_size;
MAX_BLOCK = num_blocks;
/*Initializes the random number generator*/
srand((unsigned int)(time( 0 )) );
/*Opens a file*/
fp = fopen (filename, "r+b");
if (fp == NULL)
{
printf("Could not open %s\n\n", filename);
return -1;
}
return 0;
}
/*-------------------------------------------------------------------*/
/*Reads a series of blocks from the disk into the buffer */
/*-------------------------------------------------------------------*/
int read_blocks(int start_address, int nblocks, void *buffer)
{
int i, j, e, s;
e = 0;
s = 0;
/*Sets up a temporary buffer*/
void* blockRead = (void*) malloc(BLOCK_SIZE);
/*Checks that the data requested is within the range of addresses of the disk*/
if (start_address + nblocks > MAX_BLOCK)
{
printf("out of bound error\n");
return -1;
}
/*Goto the data requested from the disk*/
fseek(fp, start_address * BLOCK_SIZE, SEEK_SET);
/*For every block requested*/
for (i = 0; i < nblocks; ++i)
{
/*Pause until the latency duration is elapsed*/
usleep(L);
s++;
fread(blockRead, BLOCK_SIZE, 1, fp);
for (j = 0; j < BLOCK_SIZE; j++)
{
memcpy(buffer+(i*BLOCK_SIZE), blockRead, BLOCK_SIZE);
}
}
free(blockRead);
/*If no failure return the number of blocks read, else return the negative number of failures*/
if (e == 0)
return s;
else
return e;
}
/*------------------------------------------------------------------*/
/*Writes a series of blocks to the disk from the buffer */
/*------------------------------------------------------------------*/
int write_blocks(int start_address, int nblocks, void *buffer)
{
int i, e, s;
e = 0;
s = 0;
void* blockWrite = (void*) malloc(BLOCK_SIZE);
/*Checks that the data requested is within the range of addresses of the disk*/
if (start_address + nblocks > MAX_BLOCK)
{
printf("out of bound error\n");
exit(0);
return -1;
}
/*Goto where the data is to be written on the disk*/
fseek(fp, start_address * BLOCK_SIZE, SEEK_SET);
/*For every block requested*/
for (i = 0; i < nblocks; ++i)
{
/*Pause until the latency duration is elapsed*/
usleep(L);
memcpy(blockWrite, buffer+(i*BLOCK_SIZE), BLOCK_SIZE);
fwrite(blockWrite, BLOCK_SIZE, 1, fp);
fflush(fp);
s++;
}
free(blockWrite);
/*If no failure return the number of blocks written, else return the negative number of failures*/
if (e == 0)
return s;
else
return e;
}