Linux I/O Method-general Linux technology-Linux programming and kernel information. For more information, see the following section. In Linux, there are two basic methods for processing input or output files: one is stream-based I/O, the other is the system-called I/O method.
Stream-based I/O calls are actually an encapsulation of system calls. These function calls automatically use the buffer output so that the program can use the system call as little as possible, this improves the program performance. Because in terms of performance, system calls are quite expensive. taking such measures can indeed speed up program execution. However, this also raises some problems. programmers may want to write data immediately. Or, if a programmer wants to mix system call I/O and stream-based I/O in an application, he must ensure that the two are always written immediately. Otherwise, the final output result is bound to cause confusion. The function used to complete this task is fflush (). This function takes a specified file handle as a parameter and immediately performs any hanging I/O operations on the file handle. An implicit refresh operation is triggered whenever you try to read or enter a line feed character.
When you need to talk to the I/O subsystem at a lower level, you must call the I/O method by means of the system. Generally, you do not need to use a system call to process files or general I/O operations. However, when dealing with network sockets, devices, pipelines, FIFO, or other special types of communication, the system calls the I/O method may be your only choice. It is also worth noting that the system calls the I/O function and cannot ensure that all the data required by the user is written and read immediately, even if there is no error. Generally, this latency is not observed when processing files, but it is common for network communication because the operating system must divide the data into data blocks for data transmission. The following function can be used in the program to ensure that data can be properly written and read.
Int write_buffer (int fd, const void * buf, int count)
{
Const void * pts = buf;
Int status = 0, n;
If (count <0) return (-1 );
While (status! = Count)
{
N = write (fd, pts + status, count-status );
If (n <0) return (n );
Status + = n;
}
Return (status );
}
Int read_buffer (int fd, void * buf, int count)
{
Void * pts = buf;
Int status = 0, n;
If (count <0) return (-1 );
While (status! = Count)
{
N = read (fd, pts + status, count-status );
If (n <1) return n;
Status + = n;
}
Return (status );
}
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.