-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_emu.c
143 lines (120 loc) · 3.67 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
#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;
/*----------------------------------------------------------*/
/*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;
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)
{
BLOCK_SIZE = block_size;
MAX_BLOCK = num_blocks;
/*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, s;
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 %d\n", start_address);
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)
{
s++;
fread(blockRead, BLOCK_SIZE, 1, fp);
memcpy((char *)buffer+(i*BLOCK_SIZE), blockRead, BLOCK_SIZE);
}
free(blockRead);
return s;
}
/*------------------------------------------------------------------*/
/*Writes a series of blocks to the disk from the buffer */
/*------------------------------------------------------------------*/
int write_blocks(int start_address, int nblocks, void *buffer)
{
int i, s;
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");
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, (char *)buffer+(i*BLOCK_SIZE), BLOCK_SIZE);
fwrite(blockWrite, BLOCK_SIZE, 1, fp);
fflush(fp);
s++;
}
free(blockWrite);
return s;
}