System-level IO in Linux/Unix

Source: Internet
Author: User

System-level IO in Linux/Unix

Linux/Unix I/O: You can map a device to a file to allow the Unix kernel to provide a simple and low-level application interface.

Linux/Unix IO systems call functions very easily. There are only five functions: open, close, read, and write) and lseek (positioning ). However, the overhead of system I/O calls is relatively large, which is generally not directly called. Instead, the system calls the Rio package for robust reading and writing, or calls the standard I/O of C language for reading and writing. Even so, both the Rio package and the standard IO package encapsulate unix I/O, so learning the IO call of the system can better understand the principle of advanced IO.

1. open the file and return a small non-negative integer, that is, the descriptor. Use descriptors to identify files. Each process has three open files: standard input (0), standard output (1), and standard error (2)

# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>

Int open (char * filename, int flags, mode_t mode );
// Return: if the operation succeeds, it is the new file descriptor. if the error is-1

Flags: how the process intends to access files

O_RDONLY: Read-Only O_WRONLY: Write-only O_RDWR: readable and writable

It can also be one or more bit masks or:

O_CREAT: if the file does not exist, create

O_TRUNC: if the file already exists, it is truncated.

O_APPEND: For each write operation, set k to the end of the file

Mode: Specify the access limit for the new file.

Each process has an umask, which is set by calling the umask function. Therefore, the File Permission is set to mode &~ Umask

2. Change the byte offset of the current file position starting from the beginning of the file. The system kernel maintains a file location k. The starting value of each opened file is 0. The application executes seek and sets the current position k. by calling the lseek function, it displays and modifies the current file location.

3. Read and Write files. Read operation: From Copying n Bytes to the storage, increasing k to k + n starting from the current file location k. For a file in MB size, when k> = m, the read operation triggers an EOF condition. Write operation: Copy n Bytes from the memory to the file, and update k to k + n.

# Include <unistd. h>

Ssize_t read (int fd, void * buf, size_t n );
// Return: the number of bytes read if the operation succeeds, 0 if the EOF operation is successful, and-1 if the error occurs.

Ssize_t write (int fd, const void * buf, size_t n );
// Return: the number of written bytes if the operation succeeds, and-1 if an error occurs.

Read function: Copy up to n Bytes from the current file location where the descriptor is fd to the buf in the storage location. -1 indicates an error, and 0 indicates EOF. Otherwise, the actual number of bytes read is returned.

Write function: Copy up to n Bytes from the storage location buf to the current file location of the descriptor fd.

Ps: ssize_t and size_t: unsigned int, ssized_t: int.

4. close the file: The data structure created when the kernel releases the file and restores the descriptor to the descriptor pool. The process closes an open file by calling the close function. An error occurs when you disable a disabled descriptor.

# Include <unistd. h>

Int close (int fd );
// Return value: 0 if the request succeeds, and-1 if the request fails.

This article permanently updates the link address:

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.