1. Cache: Buffer, is a cache, is located between the CPU and the main memory of small but fast memory, because the CPU speed is much higher than the speed of main memory, the CPU from memory to read the data to wait a long time, and The cache holds the data that the CPU has just used or the part of the data that is recycled, and it is faster to read the data from the cache, reducing the CPU wait time and improving the performance of the system.
The cache is not a cache file, but rather a buffer block (block is the smallest unit of I/O reading and writing), the cache is generally used on I/O requests, if more than one process to access a file, you can read this file into the cache, This allows the next process to gain CPU control and access this file directly from the cache to improve system performance.
2. Buffer: buffers, which are used to transfer data between devices that are not synchronized, or between devices with different priority levels, buffer can reduce the time between interprocess communication, when the storage speed of the device and storage slow devices to communicate, the storage of slow data first store data in Buffer, A device that reaches a certain level of storage and then reads the data from buffer, during which the CPU of the device that stores it quickly can do other things.
Buffer: Typically used to write to disk, for example: a process requires multiple fields to be read in, and fields that have been read before all required fields are read into buffer are first placed.
Assuming a natural disaster (such as an earthquake), residents deprived, and send fire trucks to give water to a number of settlements.
The fire truck arrived at the first settlement, opening the water, the common people took pots to pick up.
If a fire truck stops at a settlement for 100 minutes and then dumps the water for half an hour, it will open down a settlement. Such a day to come and go back and forth, that is, 4-5 settlements.
But we think about how the fire truck is there, if the tap is completely open, its powerful water pressure can easily go up to 10 floors above, 10 minutes can be put out of the whole. But because the residents are to take water, 100% open the faucet that is to give people a bath, so can only open a small portion (such as 10% of the flow). But this reduces the efficiency of the water release (only the original 10%), 10 minutes to 100 minutes.
So, can we improve the water release process so that the fire engines will be able to finish up with the highest efficiency and go to the next settlement as soon as possible?
The method is: to build a reservoir in the settlements.
The fire engine put water in the reservoir because it was 100% efficient, 10 minutes and then left. The residents then received 1.1 points of water from the reservoir.
If we analyze this example, we can know the meaning of the cache.
Fire trucks to give water to residents, residents to get water from fire trucks, that is, residents and fire trucks have interaction between, there is a connection.
But fire trucks are "high-speed equipment", residents are "low-speed equipment", low-speed residents can't keep up with high-velocity fire trucks, so fire trucks are forced to reduce the water speed to accommodate residents.
To avoid this, a "cistern", which is a "cache" of fire trucks and residents, has been more than 100% efficient in dealing with fire trucks on the one hand, and 10% inefficient and resident, freeing fire trucks to operate at peak efficiency without being dragged down by slow residents. So the fire trucks only need to stay in a settlement for 10 minutes.
Therefore, the reservoir is a "live Lei Feng", the efficient left to others, the inefficient left to themselves. Leave 10 minutes to the fire truck and leave 100 minutes to yourself.
As can be seen from the above example, the so-called cache, is "to make up for high-speed equipment and low-speed equipment between the contradiction" and set up an intermediary. Because in reality, high-speed equipment often have to deal with low-speed equipment, resulting in slow-speed equipment dragged down the situation.
Take the PC as an example. The CPU is fast, but the CPU executes instructions that are taken out of memory, and the computed results are written back to memory, but the memory is not responsive to the CPU.
CPU and Memory said: You send me the instructions of XXX address. Memory heard, but because of slow, delay the command to return, this period of time, the CPU can only idle wait. In this way, the faster CPU also does not work efficiently.
What do we do? Add a "reservoir" between the CPU and memory, which is the cache (on-chip caching), which is faster than memory, and does not need to wait for the cache to take instructions.
When the CPU wants to read the memory instruction, read the cache and then read the memory, but the first cache is empty, only from the inside access, this time is really slow, the CPU needs to wait.
But retrieving from memory is not just the instructions required by the CPU, there are other, currently unwanted, commands that are then present in the cache for backup.
When the CPU takes the instruction, read the cache first, see if there are any required instructions, if it happens to be directly from the cache, do not wait to return (hit), which frees the CPU, improve efficiency. (Not 100% hits, of course, because cache size is smaller than memory)
CPU cache, can have several layers, but also the data cache and the command cache
Disk cache is the same, just said memory is a slow device, so need on-chip cache, but this "slow" is relative to the CPU, relative to the mechanical HDD HDD, memory speed can be much faster.
For disk read and write operations, a long time ago, the read and write process requires CPU participation, and later "dma/Direct Memory Access" no longer requires the CPU, but even so, high load, long disk read and write is also very time-consuming, because the disk is a mechanical rotating parts, Its read and write speed compared to the CPU and memory bar of the binary voltage change speed, that is the difference between the speed of steam engine and rocket.
To speed up the reading and writing of the data, a cache is also inserted between the disk and the memory (Windows divides an area in memory as the cache, and the hard disk has the onboard cache.) )
Writes the data to the cache first, because the cache is fast, so the data is written quickly.
For example, 1G of data, if it takes 10 seconds to write directly to the hard disk, it only takes 1 seconds to write to the cache (that is, system memory).
As a result, the user has a fast system "illusion". But this is just a decoy, the data in the cache is not actually written to disk, and so on when the system idle time to write slowly.
Similarly, when reading data, in addition to the required data, there is a bunch of data that is not currently needed to be read into the memory cache. The next time you read it, if you happen to have the data you need in the cache, you can read it directly (hit), which avoids the embarrassment of reading data from a slow HDD. The user experience is also very fast. (also not 100% hits, because RAM capacity is much smaller than HDD capacity)
The PC has 16G of memory, disk CAHCE occupies 3.59G, which is dynamic, will automatically adjust the size
The cache is also built into the hard drive. An ad for a branded hard drive highlights the benefits of a big cache
Up to 3 chestnuts: cistern, CPU cache, disk cache
What is the cache's existence to solve? The speed is too slow, to speed up!
What about buffer? Please allow me to lift the chestnuts again.
For example, Turpan grape ripe, to use a truck loaded with grapes shipped out to sell
Orchard of the girl picking grapes, of course, not before the hand of the grapes off, the hands on the truck, but the need for a middle process "basket": picking grapes → put in the basket → The grapes into the basket into the truck.
In other words, although the ultimate goal is to "pour the grapes into the truck", but the middle must go through the "basket" of hands, the basket here is buffer. It is the "space for temporary storage of items".
Note 2 words: Temporary, Space
In other words, in order to achieve the final goal: put the grapes into the truck space , you need to temporarily put the grapes into the basket space.
To BT, for example, the BT download takes a long time to hang the machine, the computer may be 24 hours clock, but the data downloaded by BT is fragmented, reflected in the hard disk write is also fragmented, because the hard disk is a mechanical addressing device, this fragmented writing will cause the hard disk long-time high-load mechanical movement, causing the hard disk premature aging damage A large number of hard drives were damaged due to BT download.
So the new BT software in memory to open the buffer, the data is temporarily written to buffer, save to a certain size (such as 512M) and then write to the hard disk, this "peers" writing method greatly reduces the load on the hard disk.
This is: To complete the final goal: to write data to the hard disk space , you need to temporarily write buffer space .
In the case of programming, suppose you want to implement a function: accept the string that the user typed and assign to a string variable
The process is as follows:
1: Open a "keyboard buffer" in memory to accept strings typed by the user
2: Copy the string in the buffer to the memory space (that is, the assignment process) pointed to by the string variable defined in the program
In other words, in order to achieve the final goal: put the string into the string variable point of space , you need to temporarily put the string into the "keyboard buffer" space .
The above 3 chestnuts: baskets, BT buffer, keyboard buffers
What is the existence of buffer in order to solve the problem? Find a temporary storage space!
Summarize:
The same point of the cache and buffer: Both are the middle tier between 2 levels, all memory.
Different points for cache and buffer:
The cache solves the problem of time, and buffer solves the problem of space.
To improve speed, the middle layer of the cache is introduced.
In order to find a temporary space for the information, the middle layer of buffer is introduced.
In order to solve the problem of 2 different dimensions (time, space), happens to take the same solution: join a middle layer, first write the data to the middle tier, and then write to the target.
This middle layer is the memory "RAM", since it is the memory has 2 parameters: the speed of writing a number of blocks (speed), how many things can be loaded (capacity)
The cache leverages the high read and write speed provided by RAM, and buffer uses the storage capacity (space) provided by RAM.
1.
Buffer(buffer) is the system on both ends of processing
Speed Balance(viewed from a long-time scale). It is introduced to reduce the impact of sudden I/O in the short term and play
Traffic Shaping's role. For example, the producer-consumer problem, they produce and consume the resources of the approximate speed, plus a buffer can offset the resource just when the generation/consumption of sudden changes.
2.
Cache(cache) is the system on both ends of processing
Speed mismatchWhen one of the
Compromise Strategy。 Because the speed difference between CPU and memory is increasing, people take full advantage of the data locality (locality) feature and reduce the impact of this difference by using the storage-system grading (memory hierarchy) strategy.
3, assume that after the memory access becomes as fast as the CPU calculation, the cache can disappear, but the buffer still exists. For example, downloading things from the network, the instantaneous rate may have a large change, but in the long run is stable, so that by introducing a buffer to make the OS receive data more stable rate, further reduce the damage to the disk.
4, TLB (translation lookaside buffer, translation backup buffer) The name is wrong, in fact it is a cache.
When monitoring Linux resources, after entering the top command, you find memory-related mem and swap buffer and cache, by the way.
What is a cache? What is a buffer? What is the difference between the two?
The difference between buffer and cache buffer is different from the object that the cache operates on.
1. Buffer (buffered) is designed to improve the speed of data exchange between memory and hard disk (or other I/O devices).
2. Cache (Cached)
from a CPU perspective, it is designed to increase the speed of data exchange between the CPU and memory , such as the usual first-level cache, level two cache, and level three cache. The instructions and read data used by the CPU in executing the program are for memory, which is obtained from memory. Due to the slow memory read and write, in order to improve the speed of data exchange between the CPU and memory, the cache is increased between the CPU and memory, it is faster than memory, but the cost is high, and because the CPU can not integrate too many integrated circuits, so the general cache is relatively small, After Intel and other companies in order to further improve the speed, but also increased the level two cache, or even the three cache, it is based on the local principle of the program design, is the CPU execution of instructions and access to the data is often in a certain piece of the concentration, so put this piece of content into the cache, The CPU does not have to access the memory, which increases the speed of access. Of course, if the cache does not have the content required by the CPU, or access to memory.
In terms of memory read and disk read, the cache can be understood as an operating system that uses memory to cache data that might be accessed again for higher read efficiency.
Buffer (buffers) is based on the disk read-write design, the decentralized write operations centralized, reduce disk fragmentation and hard disk repeatedly seek, thereby improving system performance. Linux has a daemon that periodically empties the buffered content (that is, writes to disk) or manually empties the buffer via the Sync command.
In a nutshell, buffer is about to be written to disk, and the cache is read from disk. Buffer is allocated by various processes and is used in areas such as input queues. A simple example is when a process requires multiple fields to be read, and before all fields are read into full, the process saves the previously read-in fields in buffer.
The cache is often used on disk I/O requests, and if more than one process accesses a file, the file is made into a cache for next access, which improves system performance.
Go
The difference between buffer and cache in Linux