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.