File I/O (without buffering) I/O efficiency

Source: Internet
Author: User

The program in listing 3-3 uses the read and write Functions to copy files. Note the following points for this procedure:

It reads from the standard input and writes to the standard output, which is assumed that the standard input and output have been arranged by shell before the program is executed. Indeed, all common UNIX system shells provide a method that opens a file in the standard input for reading and creates (or overwrites) a file in the standard output. This makes it unnecessary for the program to open the input and output files.

Many applications assume that the standard input is file descriptor 0 and the standard output is file descriptor 1. In this example, two names defined in <unistd. h> are used: STDIN_FILENO and STDOUT_FILENO.

When a process is terminated, the UNIX system kernel will shut down all open file descriptors of the process, so this example will not close the input and output files.

For the UNIX system kernel, there is no difference between a text file and a binary code file, so this example can work for both files.

Program list 3-3 copy the standard input to the flag output

[Root @ localhost apue] # cat prog3-BUFFSIZE 4096 (n = read (STDIN_FILENO, buf, BUFFSIZE)> (write (STDOUT_FILENO, buf, n )! = (N <

Here is a question: how to select a BUFFSIZE value?

Use the program in program listing 3-3 to read the file, and its standard output is redirected to/dev/null. The file system used for this test is the Linux ext2 file system, and its block length is 4096 bytes (the block length is represented by st_blksize ). It is found that the Minimum CPU time of the system appears at the BUFFSIZE of 4096, and increasing the length of the buffer has almost no effect on the time.

Most file systems use some read ahead technology to improve their performance. When the system detects that it is reading data sequentially, it tries to read more data than the application requires, and assumes that the application will soon read the data.

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.