Linux System Programming Basics (vii) The concept of read/write function and (non) blocking I/O

Source: Internet
Author: User

First, Read/write function

The read function reads data from an open device or file.

#include <unistd.h>

ssize_t Read (int fd, void *buf, size_t count);

Return value: Successfully returns the number of bytes read, error returns-1 and sets errno, if the end of the file is reached before the read is transferred, this read returns 0

Parameter count is the number of bytes requested to read, the read data is saved in the buffer buf, and the file's current read-write position is moved backwards. Note that this read-write location may be different from the read/write location when using the C standard I/O library, which is recorded in the kernel, while the read-write location when using the C-standard I/O library is the location in the user-space I/O buffer. For example, to read a byte with fgetc, fgetc might read 1024 bytes from the kernel into the I/O buffer and then return the first byte, where the read and write position recorded in the kernel is 1024, while the read and write position recorded in the file structure is 1. Note The return value type is ssize_t, representing a signed size_t that can return a positive number of bytes, 0 (indicating the end of the file) or negative-1 (indicates an error). When the Read function returns, the return value shows how many bytes before the buf are just read. In some cases, the actual number of bytes read (the return value) is less than the count of bytes requested for reading, for example:

1. When reading a regular file, the end of the file is reached before count bytes are read. For example, 100 bytes are requested to be read at the end of the file, then read returns 30, and the next read returns 0. From

2, the terminal equipment reads, usually in the behavior unit, reads the line-feed character to return.

3, read from the network, according to the different Transport layer protocol and kernel caching mechanism, the return value may be less than the requested number of bytes.

The Write function writes data to an open device or file.
#include <unistd.h>

ssize_t Write (int fd, const void *buf, size_t count);

Return value: Successfully returns the number of bytes written, error return-1 and set errno

When writing a regular file, the return value of write is usually equal to the number of bytes requested to write, but not necessarily to the terminal device or network.

Reading regular files is not blocked, and no matter how many bytes read, read must return within a limited amount of time. Reading from terminal equipment or network is not necessarily, if the data entered from the terminal does not have a line break, calling read read terminal is blocked, and if the packet is not received on the network, the call read from the network will block, as well as how long it will be blocked, if there is no data to arrive has been blocked there. Similarly, writing regular files is not blocked, and writing to a terminal device or network is not necessarily the case.

Related Article

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.