"Unix environment advanced programming" chapter 3rd-file I/O, unix environment Advanced Programming

Source: Internet
Author: User

"Unix environment advanced programming" chapter 3rd-file I/O, unix environment Advanced Programming
1. Introduction

  • For most Unix files, I/O only requires five functions: open, read, write, lseek, and close.
  • The functions described in this chapter are often calledI/O without buffering. TermsWithout BufferingIt means that the user's process will not automatically buffer it. Each read and write calls a system call in the kernel. However, all disk I/O must pass through the kernel block cache (also known as the kernel buffer cache ). The only exception is the I/O of the original disk device.
2. file descriptor
  • For the kernel, all opened files are referenced through the file descriptor. The file descriptor is a non-negative integer and its variation range is 0 ~ OPEN_MAX-1
  • Convention: phantom numbers 0, 1, and 2 are symbolic constants STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.
3. open and openat functions: Create or open a file
 
  • Oflag Parameters

  • The openat function is one of the new functions added in POSIX.1 in the latest version. We hope to solve two problems:

  • The basic idea of TOCTTOU errors is:

    It refers to the software vulnerability generated when the information and permissions of the computer system are checked and used because the specific status has changed during this period.

  • File Name and path name Truncation

4. Function creat: Create a new file
 
  • It is equivalent to open (path, O_WRONLY | O_CREAT | O_TRUNC, mode );
  • One disadvantage of creat is that it opens the created file in write-only mode.
5. function close
 
  • When a file is closed, all record locks of the Process added to the file will be released.
  • When a process is terminated, the kernel automatically closes all open files of the process.
6. Function lseek: sets an offset value for an open file.
 
  • Each open file (through open) has an associated "current file offset ". It is usually a non-negative integer (which may be negative) used to measure the number of bytes calculated from the beginning of the file.
  • The description of the offse parameter is related to the whence parameter values: SEEK_SET, SEEK_CUR, and SEEK_END.
  • If lseek is successfully executed, a new file offset is returned. Therefore, you can use lseek (fd, 0, SEEK_CUR) to determine the current offset.
  • Can be used to determine whether the involved file can be set Offset, pipeline, FIFO, or network socket is not allowed, lseek returns-1, errno is set to ESPIPE
  • The value of l in the name is long before the off_t type is introduced.
7. Function read
 
  • The read operation starts from the current offset of the file. Before successful return, the offset will increase the number of bytes actually read.
  • In many cases, the number of bytes to be read is smaller than the number of bytes to be read.
8. Function write
 
  • The returned value is usually the same as the value of the parameter nbytes; otherwise, it indicates an error.
  • For normal files, the write operation starts from the current offset of the file. If the O_APPEND option is specified when the file is opened, the file offset is set at the current end of the file before each write operation.
9. I/O efficiency
  • Most file systems use some pre-read technology to improve performance.
10. File Sharing
  • The kernel uses three data structures to open files. The relationship between them determines the possible impact of one process on another process in file sharing.

    Left: Process-level file descriptor table

  • Intermediate: system-level open file table: each call to open a file adds a new file table item (different processes can open the same file, resulting in multiple file table items)

  • Right: file system-level I-node table

  • Others

11. Atomic operations
  • First lseek and then write. It is not equivalent to specifying the write of O_APPEND.
  • Specify O_CREAT and O_EXCL for the open function

  • Functions: pread, pwrite

 
  • Calling pread is equivalent to calling lseek and then calling read, but there are differences:

  • Calling pwrite is equivalent to calling lseek and then calling write. There are similar differences

12. Functions dup and dup2
 
  • The new file descriptor returned by dup must be the minimum value of the currently available file descriptor.
  • Dup2 can use the fd2 parameter to specify the value of the new descriptor.

  • Dup (fd); equivalent to fcntl (fd, F_DUPFD, 0 );

  • Dup2 (fd, fd2); is not exactly equivalent to close (fd2); fcntl (fd, F_DUPFD, fd2); because of its non-atomic operation, dup2 and fcntl have some different errno
13. sync, fsync, and fdatasync
  • The traditional Unix system has a buffer or a page cache in the kernel. Most disk I/O operations are performed through the buffer zone. When we write data to a file, the kernel usually copies the data to the buffer first, then enters the queue, and then writes the data to the disk later. This method is calledDelayed write.
14. Function fcntl: Change the attribute of an opened file 
 
  • The fcntl function provides the following 5 functions:

  • GET the val through GET first, and then val | = flags; or val & = ~ Flags; finally, SET val to fd through SET

15. Function ioctl
 
  • There is usually another parameter, which is usually a pointer to a variable or structure.
  • In the above prototype, only the header files required by the ioctl function are included. In general, you need other specialized device header files. For example, the ioctl commands of terminal I/O require the header files termios. h.
16./dev/fd
  • Opening a file/dev/fd/n is equivalent to copying descriptor n (assuming descriptor n is open)
  • In Linux,/dev/fd is an exception. It maps the file descriptor to a symbolic link pointing to the underlying physical file. For example, when/dev/fd/0 is enabled, actually opening the file associated with the standard input

[Wulin @ localhost ~] $ Ls/dev/fd/-l
Lrwx ------. 1 wulin 64 July 29 10:28 0->/dev/pts/0
Lrwx ------. 1 wulin 64 July 29 10:28 1->/dev/pts/0
Lrwx ------. 1 wulin 64 July 29 10:28 2->/dev/pts/0
Lr-x ------. 1 wulin 64 July 29 10:28 3->/proc/5341/fd

[Wulin @ localhost ~] $ Ls/dev/std *-l
Lrwxrwxrwx. 1 root 15 September July 29 2014/dev/stderr->/proc/self/fd/2
Lrwxrwxrwx. 1 root 15 January 15, July 29 2014/dev/stdin->/proc/self/fd/0
Lrwxrwxrwx. 1 root 15 August 15, July 29 2014/dev/stdout->/proc/self/fd/1

  • The/dev/fd file is mainly used by shell. It allows programs that call Parameters Using pathnames to process standard input and output in the same way as other pathnames.
 
 
  • Filter file2 | cat file1-file3 | lpr first cat reads file1, then reads its standard input (output of the filter file2 command), and then reads file3
  • If/dev/fd is supported, you can delete cat's special "-" processing. Therefore, you can: filter file2 | cat file1/dev/fd/0 file3 | lpr
  • As a post-command parameter, "-" refers to standard input or standard output, which has been used by many programs. However, this may cause some problems. For example, if "-" is used to specify the first file, it seems like a command line option is specified.

In linux, how does one use the "ourhdrh" file in "Advanced Programming for UNIX environments?

Undefined ....
Blog.csdn.net/xuorui/article/details/5447555
It is actually the compilation and include of source code.
Check whether the above website can solve the problem.
If not
G o g l e
Apue source code compilation
What is the header file written by the author...

Who's better about linux learning websites?

Verycd has many articles on the linuxsir tutorial.

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.