topic description
hard disk Cache is a mechanism used to alleviate the speed difference between memory and external memory. Cache hardware is usually located on the hard disk and is managed by a special processor
. If the data to be read by the processor is saved in Cache, it is called Cache hit. If not
, it is called Cache miss. Now the following information is provided to you:
- has a hard disk with a Cache size of 16m Bytes, and a cluster of 4K Bytes, and the data on the hard disk is also clustered with 4K Bytes. The data in the hard disk and Cache are read and written in 1 cluster each time. The function for writing Cache is
write (int addr, unsigned char * buf);
, which means that the data in buf is written to the Cache with address addr, so addr must be a multiple of 4096; - the instruction for the processor to read the hard disk data is expressed as the function
void read (unsigned int cid, unsigned char * buf);
. Its function is to read the cluster with the cluster number cid, and the read data is stored in buf. In this function, the Cache hit needs to be considered.
There is a certain elimination mechanism for the data in - Cache. Currently, the longest unaccessed elimination mechanism is used, that is, if a cluster in Cache is not used for the longest time, if the Cache is not used for the longest time, it will be eliminated from Cache, and replaced with missed data. Therefore, you need to consider that Cache missed in the read function. Now, this question requires you to implement the read function, and you need to answer the following questions:
- gives a data structure that can be used to manage Cache;
- combined with (1), please implement the read function. Note: if you miss and really read data from the hard disk, please directly use the function readhd, to declare as follows:
void readhd (unsigned int cid, unsigned char * buf);
this function does not need you to implement.
Tip:
you can first write your program idea, and then write your program according to this idea:
for example: if reading cache hits, then Otherwise. If the cache is full, then. Otherwise. The example code for the use of
readhd and write is as follows;
void read(unsigned int cid, unsigned char * buf)
{
readhd(cid,buf); //
write(addr,buf); // cache
}
sources of topics and their own ideas
Zhejiang Institute of Technology 2018 postgraduate entrance examination, we can talk about the train of thought on the line, really can not think of any good way, can only use C language.