Linux0.11 kernel--buffer mechanism for general analysis

Source: Internet
Author: User

File system file too many, and is copied Minix file system, do not want to continue analysis. The buffer mechanism is closely related to the file system, so here is a simple analysis of the buffer mechanism.

The BUFFER.C program is used to operate and manage high-speed buffer zones (pools). The buffer zone is located between the kernel code block and the main memory area, as shown in Figure 9-9. The high-speed buffer zone acts as a bridge between block devices and other kernel programs. In addition to block device drivers, if the kernel program needs to access the data in the block device, it will need to go through the buffer zone to operate indirectly.

Because reading the disk data is time-consuming, the function of the buffer is to store the read disk data, the next time there is a need to read directly from the buffer, the buffer is the memory area, read very fast.

The starting position of the buffer zone in the figure begins with the end label of the last segment of the kernel module, and end is a value set by the linker (LD) during the kernel module link, which is not defined in the kernel code. The Digest_symbols () function of the LD program produces this symbol when the system module is generated on a connection. This function is primarily used to assign a reference to a global variable, and to calculate the start and size of each connected file, which also sets the value of end, which equals Data_start + DataSize + bss_size, which is the last segment of the kernel module.

The entire buffer zone is divided into 1024-byte buffer blocks that are exactly the same size as the disk logical blocks on the block device . The cache uses hash table and idle buffer block queue for operation Management. During buffer initialization, starting at both ends of the buffer, the buffer block structure is set and the corresponding buffer blocks are partitioned separately. The high end of the buffer is divided into 1024 bytes of buffer block, the lower end of each buffer block corresponding to the buffer head structure Buffer_head (include/linux/fs.h,68 line), used to describe the corresponding buffer block properties and connect all the buffer head into the linked list . Until the buffer block is no longer divided between them, see figure 9-10. And each buffer_head is linked to a free buffer block doubly linked list structure. The detailed structure is shown in Figure 9-11.

The approximate structure of the

Buffer can refer to the Buffer_init function of buffer.c:

the extern int end;//the variable that is generated by the connector LD to indicate the end of the program. [??] struct Buffer_head *start_buffer = (struct Buffer_head *) &end;struct buffer_head *hash_table[nr_hash];//NR_HASH = 30 7 items. static struct Buffer_head *free_list;////buffer initialization function. The parameter buffer_end is the end of the specified buffer memory. For a system with 16MB of memory, the end of the buffer is set to 4MB. For the system has 8MB of memory, the end of the buffer is set to 2MB.  Voidbuffer_init (long buffer_end) {struct Buffer_head *h = start_buffer;  void *b; int i;//If the buffer high end equals 1Mb, then the actual available buffer memory//high-end should be 640KB because the memory is displayed from 640KB-1MB and the BIOS is occupied.  Otherwise the memory high end must be greater than 1MB.  if (buffer_end = = 1 <<) b = (void *) (640 * 1024); else B = (void *) buffer_end;//This code initializes the buffer, establishes an idle buffer ring list, and obtains the number of buffer blocks in the system. The process of operation is starting from the high end of the buffer partition 1K size buffer block, while at the low end of the buffer to establish a description of the buffer block//structure buffer_head, and these buffer_head form a doubly linked list. H is a pointer to the buffer head structure, and h+1 is the next buffer header address contiguous to the memory address, or to the end of the h//buffer head.  In order to ensure that there is enough memory to store a buffer head structure, the memory block//address of the >= H buffer head must be pointed to the end of the >=h+1.      while ((b-= block_size) >= ((void *) (H + 1)) {H->b_dev = 0;//uses the device number of the buffer.      H->b_dirt = 0;//Dirty Flag, also known as buffer modifier flag.  H->b_count = 0;//the buffer reference count.    H->b_lock = 0;//buffer lock flag.      h->b_uptodate = 0;//buffer update flag (or data valid flag).      H->b_wait = null;//points to the process waiting for the buffer to be unlocked.      H->b_next = null;//points to the next buffer header with the same hash value.      H->b_prev = null;//points to the previous buffer header with the same hash value.      H->b_data = (char *) b;//points to the corresponding buffer data block (1024 bytes).      H->b_prev_free = h-1;//points to the previous item in the list.      H->b_next_free = h + 1;//points to the next item in the list.      h++;//h refers to a downward new buffer head position.      nr_buffers++;//number of buffer blocks accumulated.    if (b = = (void *) 0x100000)//if address B decrements to equals 1MB, skip 384kb,b = (void *) 0xa0000;//let B point at address 0xa0000 (640KB).  } h--;//let H point to the last valid buffer header.  Free_list = start_buffer;//lets the idle-list head point to the head of a buffer header.  Free_list->b_prev_free = h;//The b_prev_free of a chain header refers to a forward item (that is, the last item). H->b_next_free = The next pointer to free_list;//H points to the first item, forming a ring chain.  Initialize the hash table (Hashtable, hash list), and all pointers in the table are null. for (i = 0; i < Nr_hash; i++) hash_table[i] = NULL;}

According to the above theoretical knowledge, this code is very good analysis, there is no more explanation.

Linux0.11 kernel--buffer mechanism for general analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.