Linux Basics five--buffering

Source: Internet
Author: User
Tags hosting

In modern operating systems, almost all I/O devices in the exchange of data with the processor using a buffer mechanism, buffer is a storage area, can be a specialized hardware register composition but because the cost of hardware is also small, generally, more memory to use as a buffer.

Buffer Management : organizes buffers and provides the means to obtain and release buffers.

Buffering technology is a technique used to coordinate data calls between devices with large throughput speeds.

The issues to be resolved by the buffered references are actually:

1. Improve the speed mismatch between CPU and I/O devices.

2, can reduce the number of CPU interrupts I/O devices and relax the CPU interrupt response time requirements.

3. Improve parallelism between CPU and I/O devices

Eg: when a buffer is set between the producer (CPU) and the consumer (printer), the producer produces a batch of data and puts it into the buffer and then puts it into the next production, and consumption can then take out the data so that producers and consumers can be in parallel working condition. If there were no buffers between them, producers would have to stop working and wait for consumers to produce a good batch of data, which would make the producers less efficient.

1. Single buffer

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/10/wKiom1dJq7bhJpH6AAAc0fYLJ9c537.png "title=" single buffer. PNG "alt=" Wkiom1djq7bhjph6aaac0fylj9c537.png "/>

Assuming that the data is passed from the I/O device to the buffer at the time of T,CPU processing time for the data is C, the buffer transmits the data to the CPU for M, then the CPU can process the data at the same time when the I/O device transmits data to the buffer, so the total system data is Max (c,t) +m .

* * buffer is a row of data used to stage user input during character device input , while the user process suspends waiting during input, and when output, the user process enters a row of data into the buffer and continues processing. User processes are blocked when the user program already has a second row of data output but the first row of data has not yet been extracted.

2. Double buffers

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/0F/wKioL1dJrMSBB5dpAAAkf_dS-w8382.png "title=" double buffers. PNG "alt=" Wkiol1djrmsbb5dpaaakf_ds-w8382.png "/>

The two buffers are actually intended to solve the problem of mutual exclusion between consumers and producers when using buffers. If the consumer has not taken the data from the buffer, the producer cannot feed the buffer even if it generates new data.

Double buffering is also known as buffer swap, in the case of the device is true, the data can be fed into the first buffer, wait until the first buffer is full and then turn to the second buffer, the operating system can move from the first buffer data into the user process, let the CPU processing data.

If they only set a single buffer when communicating between the two machines, they can only implement one-way data transfer at any one time, and in order to achieve two-way data transmission, the send buffer and the receive buffer must be set separately on each machine.

3. Ring Buffer

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/82/10/wKiom1dJq9fDylP7AAAgM2w_4aE917.png-wh_500x0-wm_3 -wmp_4-s_1238810070.png "title=" ring buffer. PNG "alt=" Wkiom1djq9fdylp7aaagm2w_4ae917.png-wh_50 "/>

The ring buffer contains multiple buffers, each of which has the same size and is divided into three types:

R: Load input data

G: buffer filled with data

C: Buffer that the process is using

A ring buffer contains three pointers:

Nexti: Points to the next available R for the input process

NXETG: Point to the next available G for the compute process

cuurent: Pointing to the buffer that the process is using

There are two important ways to handle a ring buffer:

1.getbuf: Provide the buffer pointed to by the pointer NextG to the process and let the value of current be modified, and finally let NextG point to the next G-buffer

2.releasebuf: Will use the completed C buffer release to modify it to R, when the R buffer is full, modify it to G

The following two types of problems occur in the ring buffer:

1. System Calculation Limited: Nexti catch up with NextG

2. System I/O Limited: NextG catch up with Nexti

Buffer pool

The buffers we mentioned earlier can be called dedicated buffers. When the system is large, in order to improve utilization, we use a common buffer pool that can be input and output, and set several buffers in the pool.

The composition of the buffer pool:

1. Blank buffer queue: Empty buffers are chained to

2. Input queue: A buffer with full input data is chained to

3. Output queue: Buffers with full output data are chained to

How buffers work: hosting inputs, hosting outputs, extracting inputs, extracting outputs

In our learning process, we are actually familiar with the buffer, but we do not turn a blind eye, we know that there are three buffering mechanisms in Linux, row buffer, full buffer, no buffering.

full buffering : Call system I/O until the buffer fills up

Row buffering : Call system I/O until a newline is encountered or the row buffer fills up

Let's take a look at two examples of row buffers:

The first example: The Mysterious disappearing Hello World

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/10/wKiom1dJq_bDCUAKAABnL7qEKmo760.png "title=" row buffer skipped. PNG "alt=" Wkiom1djq_bdcuakaabnl7qekmo760.png "/>

In the above program we do not use ' \ n ' in the standard output and use _exit (0) to cause the content of the row buffer to not be processed

So when we run the program, we don't see Hello World

**

exit (). after calling the Exit function, it first performs a series of cleanup processes, including calling the execution of each termination handler, shutting down all standard IO streams, and then entering the kernel.

_exit (). Unlike exit, it goes directly into the kernel without cleaning up the work.

_exit (). Similarly, it does not perform cleanup work and goes directly into the kernel.

The second example: the progress bar small range

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/0F/wKioL1dJrQXCMGsJAAEFPVnQStY224.png "title=" Progress bar Applet . PNG "alt=" Wkiol1djrqxcmgsjaaefpvnqsty224.png "/>

Obviously in this case, we use the fflush to refresh our buffer, which calls system I/O regardless of whether the buffer is filled.

Again, we have to understand the different concepts of line break and carriage return , which means jumping to the next line of the current row, and the carriage return refers to the beginning of the line, this example we use ' \ R ' to control each time from the beginning of the output of the overlay to achieve progress bar, is not very magical, you also quickly try it! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0038.gif "alt=" J_0038.gif "/>


This article is from the "Zimomo" blog, make sure to keep this source http://zimomo.blog.51cto.com/10799874/1784147

Linux Basics five--buffering

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.