Apue (3)---file I/O

Source: Internet
Author: User

First, Introduction

Most files in UNIX systems require only 5 functions for I/O: Open/read/write/lseek and Close, which are non-buffered I/O (unbuffered I/O). As long as the sharing of resources between multiple processes is involved, the concept of atomic operations becomes very important, as described later: Dup/fcntl/sync/fsync and IOCTL.

Ii. file descriptors

For the kernel, many open files are referenced by a file descriptor. A file descriptor is a non-negative integer that returns a file descriptor to the process when an existing file is opened or a new file is created. By convention: The UNIX System Shell Associates file descriptor 0 (Stdin_fileno) with the standard input of a process, file descriptor 1 (stdout_fileno) is associated with a standard output, and file descriptor 2 (Fileerr_fileno) is associated with a standard error. These macros are defined in <unistd.h>.

Third, function open and Openat

#include <fcntl.h>int open (constcharint aflag,... /* mode_t Mode */ ); int openat (intcharint aflag, ... /* mode_t Mode */ )// If successful, returns the file descriptor; 1 If an error occurs
View Code

ISO C with ... This method indicates that the remaining number of parameters is extremely variable, and the file descriptor returned by the Open/openat function must be the smallest unused descriptor value. For the open function, the last parameter is used only when creating a new file. The path parameter is the name of the file to open or create, and the Aflag parameter can be used to illustrate several options for this function. The Aflag parameter consists of two classes:

First class, one must be specified in 5 and only one can be specified: o_rdonly (read-only), O_rdonly (write-only open), O_RDWR (read/write Open), o_exec (open only), O_serach (applied to directory, search open)

The second class, can be superimposed: o_append (Append the end of the file each time the write), o_create (if this file does not exist to create it, and you need to specify mode to set new file access), O_EXCL (if you specify O_create, when the file exists, Error), O_directory (if Path does not refer to a directory, error), O_nonblock (if Path refers to a FIFO, and so on, this option is for the file's open operation and subsequent I/O settings to be non-blocking), O_ SYNC (so that each write waits for the physical I/O operation to complete, including the I/O required by the file property update that is caused by the write operation), O_trunc (if this file exists, truncates it to 0), O_dsync (so that each write waits for the physical I/O operation to complete, However, if the write operation does not affect the reading of the data just written, there is no need to wait for the file property to be updated, O_rsync (so that each read operation with a file descriptor as a parameter waits until all writes that are pending on the same part of the file are completed)

The FD parameter separates the open and Openat function areas:

The 1.path parameter specifies an absolute pathname, in which case the FD parameter is ignored and the Openat function is equivalent to the Open function

The 2.path parameter specifies a relative pathname, and the FD parameter indicates where the relative path name begins in the file system. The FD parameter is obtained by opening the directory where the relative path name resides

The 3.path parameter specifies the relative pathname, the FD parameter has a special value of AT_FDCW, the pathname is fetched in the current working directory, and the Openat function is similar to the open function on the operation.

There are two points to the existence of the Openat function: 1. Allows a thread to open a file in a directory using a relative pathname, instead of opening the current working directory only; 2. You can avoid time-of-check-to-time-of-use errors.

Iv. function Create and function close

The Open function provides o_creat and O_trunc, so a separate create function is no longer required.

When a process is finished, the kernel automatically closes all of its open files, and many programs take advantage of this feature without explicitly invoking close open files.

V. Function Lseek

Each open file has an expected associated current file offset, which is usually a nonnegative integer that measures the number of bytes computed from the beginning of the file. Typically read and write operations start at the current file offset and increase the number of bytes read and written by the offset. Press the system by default. When you open a file, the offset is set to 0 unless you specify the O_append option. We can call Lseek explicitly to set an offset for an open file.

#include <unistd.h>off_t lseek (intint  whence); // If successful, returns the new file offset; 1 If an error occurs
View Code

The interpretation of the parameter offset is related to the value of the parameter whence:

1.seek_set: The offset of the file is set to the beginning of the file at the start of offset bytes

2.seek_cur: Sets the offset of the file to the current value plus offset (offset can be a negative number)

3.seek_end: Sets the offset of the file to the file length plus offset (offset can be a negative number)

Apue (3)---file I/O

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.