C + + Development series under Linux (vi)--File IO related system calls

Source: Internet
Author: User

In Linux operating systems, files are the most basic and most important abstraction, and Linux follows the philosophy of all documents. According to different attributes, the file can be divided into ordinary files and special files. Special files are kernel objects that are represented as files, and Linux supports four types of special files:

1. block device files (e.g. hard disk devices)

2. Character device files (e.g. keyboard devices)

3. Named pipes (mainly inter-process communication use)

4. Unix domain sockets (mainly used for network communication)

Linux system for the operation of the file, provides a series of system calls, mainly divided into the following categories:

1. Create file (creat) 2, open file 3, read file (read) 4, write file (write) 5, synchronous io (fsync)

6, Direct IO 7, close file (Close) 8, find File (Lseek) 9, location file (pread/pwrite) 10, truncated file (truncate)

Here are a few common invocation usages:

1. creat create file, the prototype of system call is:

#include <sys/types.h><sys/stat.h>int creat (constChar * Pathname, mode_t mode);

creat use less, because all the creation of the function of the file can be implemented by the open system call, creat itself can also be implemented by open, why there is such a system call, this is a legacy problem, because UNIX at the beginning of the design, Open system call only two parameters, not enough to complete the function of creating the file, so there is the creat system call, but then the open system call extended three parameters, the function of the creat is implemented, but creat system call or left, creat is not a word, Less the letter E, this is the UNIX developers write code, missing a letter caused by the Big God also have careless, when we write code, be serious.

2. Open and create the file, the system call prototype is

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int open (const char *name, int flags); int open (const char *name, int flags, mode_t mode);

Name represents the file name that is passed in.

Enumeration of Flags: O_rdonly (read-only) o_wronly (write-only) O_rdwr (read-write)

     flags can be performed with any or more of the following enumerations or operations  :     o_append Append mode, each time the file position pointer is placed at the end of the file before the write operation.      o_async   generates a signal when the specified file is readable or writable (default is Sigio). This flag is used only for terminals and sockets and cannot be used for normal files.      o_creat     When the file specified by name does not exist, it is created by the kernel. If the file already exists, this flag is invalid unless given the O_EXCL flag      o_direct   Open File user Direct io     o_directory If name is not a directory, the open () call fails. This flag is used when Opendir () is used internally by     &NBSP;O_EXCL     and o_creat, and if the given file already exists, then the open () call fails. Used to prevent duplicate file creation.      o_largefile   When a given file is opened, a 64-bit offset is used so that the >2g file can also be opened, which is implied in 64-bit schemas by using this parameter to open the file      o _noctty if the given name points to an end device (that is,./dev/tty), it will not be the control terminal for this process, even though the process currently does not have a control terminal. This flag is less common      o_nofollow   If name is a symbolic link, the open () call fails. Typically, the link is parsed and the destination file is opened. If the other part of the given path is a link, the call is still available. For example, if name is/etc/ship/plank.txt, the call fails if plank.txt is a symbolic link. If etc or ship is a symbolic link, the call succeeds as long as plank.txt is not.      o_nonblock If you can, the file will open in non-blocking mode. The open () call does not, and no other action causes the process to block (sleep). This situation may only be used for FIFo     o_sync Open file for synchronizing IO. The write operation is not completed until the data is written to disk, and the normal read operation is synchronized, so this flag has no effect on the read operation. POSIX has additional definitions for O_dsync and O_rsync, and on Linux these flags are synonymous with o_sync.      o_trunc If the file exists and is a normal file, and allows write, truncates the length of the file to 0, for FIFO or interrupt device, this parameter is ignored. There is no definition on other file types. Because truncation of files requires write permission, O_trunc and o_rdonly are used simultaneously, and are not defined.   enumeration of Mode:    &NBSP;S_IRWXU   owner has read and execute rights     &NBSP;S_IRUSR     owner reads rights   &N Bsp &NBSP;S_IWUSR   owner has write permissions     &NBSP;S_IXUSR     owners have execute rights     &NBSP;S_IRWXG   Group has read-write and Execute permissions     &NBSP;S_IRGRP     Group has write access     &NBSP;S_IWGRP   Group has writing rights      s_ IXGRP     Group has execute rights     &NBSP;S_IRWXO   Any user has read-write and execute rights      S_IROTH  any user has access      s_iwoth   Any user has Write permission      s_ixoth    Any user has Execute permission if opened or created successfully, will return an FD, file descriptor,  A file descriptor that is unique within the operating system.   If open fails, the errno is assigned a value of 3, the Read function is prototyped as
#include <unistd.h>ssize_t read (int fd, void *buf, size_t count);

FD represents the file descriptor that the open successfully returns, and also the file to read the data from

BUF storing data read from a file

Count represents the number of readings from a file

Return value: If the read succeeds, returns the amount of data read, if it fails, returns 1, and sets the errno

4. Write prototype

#include <unistd.h>ssize_t write (intconstvoid *buf, size_t count);

FD represents the file descriptor that was successfully returned by open and is also the file to write the data to

BUF storing the data to be written

Count represents the number of writes

Return value: If the write succeeds, returns the amount of data written, if Shibiao, returns 1, and sets the errno

5. Synchronous IO (Fsync)

Fsync is the simplest method of confirming data written to disk, and the prototype of the function is:

#include <unistd.h>int fsync (int fd);

6. Direct IO

When open, the incoming o_direct is the direct Io, at which point the I/O operation ignores the page caching mechanism, initializes the user device directly, and is a synchronous operation that does not return until the operation is complete. 7, close the file, using the close system call can be 8, 9, 10 is to find and truncate files, and so on, do not do a detailed introduction, can be used in the specific, in detail.

C + + Development series under Linux (vi)--File IO related system calls

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.