Apue (3)---file I/O (2)

Source: Internet
Author: User

Seven. function write

#include <unistd.h>size_t write (intconstvoid *buf, size_t nbytes); // If successful, returns the number of bytes written, or 1 if an error occurs

For normal files, the write operation starts at the current offset of the file. If the O_append option is set when the file is opened, the file offset is set at the current end of the file before each write operation. After a successful time, the file offset increases the number of bytes actually written.

Viii. efficiency of I/O

Most file systems use some kind of pre-read (read ahead) technology to improve performance. When sequential reads are detected, the system attempts to read more data than is required by the application and assumes that the application will soon read the data.

Ix. file Sharing

UNIX systems support the sharing of open files between different processes. The kernel uses 3 data structures to represent open files, and the relationship between them determines the possible impact a process might have on another process in terms of file sharing:

1. Each process has a record entry in the process table, and the record entry contains an open file descriptor table, which can be treated as a vector, with each descriptor occupying one item. associated with each file description typeface: A. File descriptor flag (close_on_exec); b. Just want a pointer to a file table entry

2. The kernel maintains a single file table for all open files. Each file table entry contains: A. File status flags (read, write, add, synchronize, and non-blocking, etc.); b. current file offset; c. Pointer to v-node representation of a file

3. Each open file and device has a v-node structure. The V node contains the file type and pointers to various manipulation functions for this file. For most files, the V node also contains the I node of the file, which contains the owner of the file, the length of the file, a pointer to the location of the file's actual data block on disk, which is read from the disk when the file is opened and is ready to be used.

When two processes open a file at the same time, two processes get a separate file table entry because this allows each process to have its own current offset to the file, but only one v-node table entry for a particular file. The following is further explained:

1. After each write is completed, the current file offset in the file table entry increases the number of bytes written. If this causes the current file offset to exceed the current file length, the current setting in the I node table entry becomes the current file offset

2. If a file is opened with the O_append flag, the corresponding flag is also set to the file status flag of the file table entry. Each time an operation is performed on this file with an append write flag, the current file offset in the file table entry is first referred to as the length of the file in the I node table entry. This allows the data to be written each time it is appended to the current end of the file.

3. If a file is anchored to the current end of the file with Lseek, the current file offset in the file table entry is set to the current file length in the I Node table entry (Lseek is one-time, o_append is duplicated)

4.lseek function to modify the current file offset in the file table entry without any I/O operations.

There may be multiple file descriptor entries that point to the same file table entry (the DUP function), and after the fork function, each open file descriptor of the parent process and child processes share the same file table entry. The file descriptor flag is used only for one descriptor of a process, and the file status flag is applied to all descriptors in any process that points to the given file table entry.

X. Atomic operation

Generally speaking: atomic operations (atomic operation) refer to an operation consisting of multiple steps. If the operation is performed atomically, either all steps are executed, or no step is performed, it is not possible to perform only a subset of all the steps.

1. Append to a file

Earlier UNIX did not provide o_append, append operation can only be done by Lseek and write two operations, but in these two operations are not atomic, when multiple processes simultaneously manipulate a file, a process can occur lseek to the end of the file, B process at the end of the file to write some data, When a process is written again, it may overwrite the data of B. UNIX now provides a o_append operation to solve this problem, in which the Lseek and write are atomic, so that the write must be at the end.

2. Functions Pread and Pwrite

Unlike append, this is a two read-write enhanced version

3. Create a file

For the open function existing o_creat and O_EXCL, when there is no file to be created, the file is created and returned successfully, and if the file already exists, it is returned, which is an atomic operation. If there is no such an atomic operation, we can only try to open,open the failure and then to create, but there may be other operations in these two operating gaps to create the corresponding file, resulting in an error.

Xi. function DUP and dup2

#include <unistd.h>int dup (int  FD); int dup2 (intint  fd2); // If successful, returns a new file descriptor, or 1 if it fails

The new file descriptor that the DUP returns each time must be the minimum value in the currently available file descriptor. For dup2, you can specify the value of the new descriptor with FD2. If the FD2 is already open, turn it off. If FD equals FD2, then Dup2 returns FD2 without closing it. FD2 fd_cloexec file descriptor flags are cleared so that FD2 is turned on when the process calls exec. and the new file descriptor returned by DUP and Dup2 is a shared file table entry, which means they share information such as file status flags, file offsets, and so on. Dup2 is also an atomic operation, equivalent to a close plus fcntl, with subtle differences.

Apue (3)---file I/O (2)

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.