Академический Документы
Профессиональный Документы
Культура Документы
3. Caching is a method used for speeding up access. If files are on a disk what
is the purpose of a disk cache (in the context of this chapter)?
Cached data is still there during recovery and doesnt need to be fetched
again therefore is more reliable.
4. Submit any code or results that you have obtained from this weeks lab.
________________
Queue between 0-199 (200)
Head:
Queue:
SSTF:
SCAN:
C-SCAN:
158
[ 137, 145, 121, 169, 162, 25, 139, 90, 128, 47 ]
155
193
186
Head:
Queue:
SSTF:
SCAN:
C-SCAN:
30
[ 134, 64, 55, 10, 122, 112, 173, 128, 136, 77 ]
183
169
179
Head:
Queue:
SSTF:
SCAN:
C-SCAN:
90
[ 56, 22, 74, 47, 25, 139, 140, 144, 159, 56 ]
205
283
183
#define NUM_REQUESTS 10
#define NUM_ADDRESSES 200
void init();
int SSTF();
int SCAN();
int CSCAN();
int cmp_asc ( const void *a, const void *b );
void print_head();
void print_queue();
void print_all();
init();
sstf = SSTF();
scan = SCAN();
cscan = CSCAN();
}
print_all();
putchar( \n );
return EXIT_SUCCESS;
}
void init()
{
int i;
srand( time(NULL) );
_head_pos = rand() % NUM_ADDRESSES;
for ( i = 0; i < NUM_REQUESTS; i++ )
{
_queue[i] = rand() % NUM_ADDRESSES;
}
}
int SSTF()
{
int head_pos, queue[NUM_REQUESTS];
int seek_time;
int i, j;
head_pos = _head_pos;
memcpy( queue, _queue, sizeof(queue) );
seek_time = 0;
for ( i = 0; i < NUM_REQUESTS; i++ )
{
int min_index;
int min_seek = NUM_ADDRESSES;
for ( j = 0; j < NUM_REQUESTS; j++ )
{
/* ignore completed */
if ( queue[j] != -1 )
{
int j_seek_time;
j_seek_time = abs( head_pos - queue[j] );
if ( j_seek_time < min_seek )
{
min_seek = j_seek_time;
min_index = j;
}
j_seek_time = abs( queue[j] - head_pos );
if ( j_seek_time < min_seek )
{
min_seek = j_seek_time;
min_index = j;
}
}
}
/*printf( "[%i] %i [%i]\n", head_pos, queue[min_index], min_seek
);*/
seek_time += min_seek;
head_pos = queue[min_index];
queue[min_index] = -1;
}
return seek_time;
}
int SCAN()
{
int
int
int
int
head_pos, queue[NUM_REQUESTS];
seek_time;
i;
remaining;
head_pos = _head_pos;
memcpy( queue, _queue, sizeof(queue) );
seek_time = 0;
remaining = NUM_REQUESTS;
qsort( queue, NUM_REQUESTS, sizeof(int), cmp_asc );
head_pos, queue[NUM_REQUESTS];
seek_time;
i;
remaining;
head_pos = _head_pos;
memcpy( queue, _queue, sizeof(queue) );
seek_time = 0;
remaining = NUM_REQUESTS;
qsort( queue, NUM_REQUESTS, sizeof(int), cmp_asc );
for ( i = 0; i < NUM_REQUESTS; i++ )
{
if ( queue[i] > head_pos )
{
break;
}
}
for ( ; i < NUM_REQUESTS; i++ )
{
/*printf( "[%i] %i [%i]\n", head_pos, queue[i], queue[i] - head_
pos );*/
seek_time += queue[i] - head_pos;
head_pos = queue[i];
queue[i] = -1;
remaining--;
}
if ( remaining == 0 ) return seek_time;
/*printf( "[%i] %i [%i]\n", head_pos, NUM_ADDRESSES - 1, (NUM_ADDRESSES
- 1) - head_pos);*/
seek_time += (NUM_ADDRESSES - 1) - head_pos;
head_pos = NUM_ADDRESSES - 1;
/*printf( "[%i] %i [%i]\n", head_pos, 0, head_pos );*/
/* don t include jump as head movement */
/* seek_time += head_pos; */
head_pos = 0;
for ( i = 0; i < NUM_REQUESTS; i++ )
{
if ( queue[i] != -1 )
{
/*printf( "[%i] %i [%i]\n", head_pos, queue[i], queue[i]
- head_pos );*/
seek_time += queue[i] - head_pos;
head_pos = queue[i];
queue[i] = -1;
}
}
return seek_time;
}
void print_head()
{
printf( "Head: %i\n", _head_pos );
}
void print_queue()
{
int i;
printf( "Queue: [" );
for ( i = 0; i < NUM_REQUESTS; i++ )
{
printf( " %i%s", _queue[i], (i+1 < NUM_REQUESTS) ? "," : " " );
}
printf( "]\n" );
}
void print_all()
{
print_head();
print_queue();
printf( "SSTF: %i\n", SSTF() );
printf( "SCAN: %i\n", SCAN() );
printf( "CSCAN: %i\n", CSCAN() );
}