A question about the application of algorithms for the postgraduate entrance examination, just talk about the train of thought.

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:
  1. gives a data structure that can be used to manage Cache;
  2. 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.

Dec.23,2021

is divided into parts in the cache to identify which data of the hard disk is in the cache, and the storage time (that is, a counter); each time you look up to see if there is a specified cluster, if so, take it directly; otherwise, read the hard disk and add or replace the hard disk cache of


16m and each cluster is 4K, that is to say, there are 4096 clusters.
to ensure the longest missed elimination mechanism, time sorting should be used, and secondly, linked lists can be used to easily adjust the order of clusters.
after determining the data structure of cache, start to design the read function:
first request to come in, first traverse the clusters on the linked list, and then return buf, if the cid, match is successful (cache hit). If the cache does not match (cache missed), call the readhd function to read the data of the hard disk. And determine whether the cache is full.
is divided into two cases (it can be dealt with directly according to case 1, and there are two types of optimization. Anyway, the question is required to be replaced if it is not hit, and the cache is dissatisfied):
1.cache is full
replace the cluster read by the hard disk to the beginning of the linked list, initialize the time, and delete the last node of the linked list.
2.cache not full
move the hit cluster to the beginning of the linked list and update the time.

Menu