Linux0.11 kernel-buffer mechanism analysis, linux0.11 Buffer

Source: Internet
Author: User

Linux0.11 kernel-buffer mechanism analysis, linux0.11 Buffer

The file system contains too many files and is a copy of the MINIX file system. The buffer mechanism is closely related to the file system, so we will analyze the buffer mechanism here.

The buffer. c program is used to operate and manage high-speed buffers (pools. The high-speed buffer is located between the kernel code block and the main memory area, as shown in Figure 9-9. The high-speed buffer zone serves as a bridge between the block device and other programs in the kernel. In addition to the block device driver, if the kernel program needs to access the data in the block device, it needs to go through the high-speed buffer for indirect operations.

Because it takes a lot of time to read disk data, the buffer zone is used to store read disk data. The buffer zone will be read directly from the buffer zone as needed next time. The buffer zone is the memory zone and the read speed is very fast.

In the figure, the starting position of the high-speed buffer starts from the end mark of the last segment of the kernel module. end is a value set by the link Program (ld) during the connection of the kernel module. This symbol is not defined in the kernel code. When the system module is generated through a connection, the digest_symbols () function of the ld program generates this symbol. This function is mainly used to assign values to global variables and calculate the start and size of each connected file. The end value is also set, which is equal to data_start + datasize + bss_size, that is, the last segment of the kernel module.

The entire high-speed buffer is divided into 1024-byte buffer blocks, which are exactly the same size as the disk logical block on the block device.. High-speed buffering uses hash tables and idle buffer block queues for operation management. During the buffer initialization process, the buffer block header structure is set and corresponding buffer blocks are divided from both ends of the buffer zone.The high-end of the buffer zone is divided into 1024-byte buffer blocks. At the low-end, the buffer header structure buffer_head (include/linux/fs. h, 68 rows), used to describe the attributes of the corresponding buffer block and connect all the buffer headers to a linked list.. The buffer blocks cannot be further divided between them, as shown in Figure 9-10. Each buffer_head is linked to an idle buffer block bidirectional linked list structure. The detailed structure is shown in Figure 9-11.

For the approximate structure of the buffer, refer to the buffer_init function of buffer. c:

Extern int end; // the variable that indicates the end of the program generated by the Connection Program ld. [???] Struct buffer_head * start_buffer = (struct buffer_head *) & end; struct buffer_head * hash_table [NR_HASH]; // NR_HASH = item 307. Static struct buffer_head * free_list; // buffer initialization function. // The buffer_end parameter is the end of the specified buffer memory. If the system has 16 MB memory, the buffer end is set to 4 MB. // For the system with 8 MB memory, set the buffer end to 2 MB. Voidbuffer_init (long buffer_end) {struct buffer_head * h = start_buffer; void * B; int I; // if the high-end buffer is equal to 1 Mb, the memory and BIOS usage are displayed from 640KB-1MB, therefore, the actual available buffer memory // the high end should be 640KB. Otherwise, the high-end memory must be greater than 1 MB. If (buffer_end = 1 <20) B = (void *) (640*1024); else B = (void *) buffer_end; // This code is used to initialize the buffer zone, create an idle buffer chain table and obtain the number of buffer blocks in the system. // The operation process is to divide a buffer block of 1 K from the high end of the buffer zone, and establish a structure buffer_head describing the buffer block at the low end of the buffer zone, and form these buffer_head into a two-way linked list. // H is the pointer to the buffer header structure, while h + 1 is the next consecutive buffer header address pointing to the memory address. It can also be said to be pointing to the end of the h // buffer header. To ensure that there is enough memory to store a buffer header structure, you need to point B to the memory block // address> = The End Of The h buffer header, that is to say, we must> = h + 1. While (B-= BLOCK_SIZE)> = (void *) (h + 1) {h-> B _dev = 0; // The device number that uses the buffer. H-> B _dirt = 0; // dirty flag, that is, the buffer modification flag. H-> B _count = 0; // The buffer reference count. H-> B _lock = 0; // The buffer lock flag. H-> B _uptodate = 0; // The buffer update flag (or the data valid flag ). H-> B _wait = NULL; // points to the process waiting for the buffer to be unlocked. H-> B _next = NULL; // point to the next buffer header with the same hash value. H-> B _prev = NULL; // point to the first buffer header with the same hash value. H-> B _data = (char *) B; // point to the corresponding buffer data block (1024 bytes ). H-> B _prev_free = h-1; // point to the first item in the linked list. H-> B _next_free = h + 1; // point to the next item in the linked list. H ++; // h indicates the position of the next buffer header. NR_BUFFERS ++; // The number of buffer blocks is accumulated. If (B = (void *) 0x100000) // if address B is decreased to equal to 1 MB, skip 384KB, B = (void *) 0xA0000; // point B to address 0xA0000 (640KB.} H --; // Let h point to the last valid buffer header. Free_list = start_buffer; // Let the idle linked list header point to the first buffer header. Free_list-> B _prev_free = h; // B _prev_free in the chain table header indicates the first item (that is, the last item ). H-> B _next_free = free_list; // The next pointer of h points to the first item to form a chain link. // Initialize the hash table (hash table and hash table) and set all pointers in the table to NULL. For (I = 0; I <NR_HASH; I ++) hash_table [I] = NULL ;}

Based on the above theoretical knowledge, this code is well analyzed and will not be explained much.

 

Related Article

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.