博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
APUE学习笔记之文件I/O(下)(4)
阅读量:5097 次
发布时间:2019-06-13

本文共 8397 字,大约阅读时间需要 27 分钟。

  3.9 I/O Efficiency (I/O 效率)

  All normal UNIX System shells provide a way to open a file for reading on standard input and to create (or rewrite) a file on standard output. (所有正常的Unix系统shells都提供拉一种方法来从标准输入打开一个文件和从标准输出创建或重写一个文件。)

  3.10 File Sharing (文件共享)

  The UNIX System supports the sharing of open files among different processes. (Unix 系统支持不同进程间文件共享。)

  Every process has an entry in the process table. Associated with each file descriptor are: The file descriptor flags(close-on-exec); A pointer to a file table entry. (每个进程有一个process table entry。关联着文件描述符的标志(close-on-exec);一个指向file table entry的指针。)

  The kernel maintains a file table for all open files. Each file table entry contains: file status flags(read, write, append, sync, nonblocking...); the current file offset; A pointer to the v-node table entry for the file. (kernel为每个打开的文件维持一个file table entry。每个file table entry包含:文件状态标准(read, write, append, sync, nonblocking...); 当前的偏移量;指向v-node table entry的指针。)

  Each open file has a v-node structure that contains: the type of file and pointers to functions that operate on the file; alse contains the i-node for the file. (每个打开的文件有一个v-node结构体,包含:文件的类型,指向操作函数的指针,和一个文件的i-node。)

  The i-node contains the owner of file, the size of the file, pointers to where the actual data blocks for the file, and so on. (i-node 包含文件的拥有者,文件的大小,数据块的指针,等等。)

  Each process that opens the file gets its own file table entry, but only a single v-node table entry is required for a given file while two independent processes have the same file. (当两个独立的进程打开相同的文件时, 每个进程都有一个自己的file table entry, 但是只有一个v-node table entry。)

  After each write is complete, the current file offset in the file table entry is incremented by the number of bytes written. If this causes the current file offset to exceed the current file size, the current file size in the i-node table entry is set to the current file offset. (写操作完成后,file table entry 中的当前文件偏移量根据实际写入的字节递增。如果偏移量超出拉文件的大小,i-node中的文件大小被设置为当前的文件偏移量。)

   If a file is opened with the O_APPEND flag, a corresponding flag is set in the file status flag of the file table entry. Each time a write is performed for a file with this append flag set, the current file offset int the file table entry is first set to the current file size form the i-node table entry. (如果打开文件时使用了O_APPEND标志,在file table entry 中会设置一个相应的文件状态标志。每次执行带append标志的写操作,当前的文件偏移量首先会被设置为i-node table entry中的文件大小。)

  If a file is positioned to its current end of the file using lseek, all that happens is the current file offset in the file table entry is set to the current file size from the i-node table entry(Note that this is not the same as if the file was opend with the O_APPEND flag). (如果一个文件使用lseek函数指向文件的结尾,实际上是把当前的偏移量设置为文件的大小。(注意这和打开文件时使用O_APPEND标志不一样。))

  The lseek function modifies only the current file offset in the file table entry. NO I/O tabkes place. (lseek函数只改变file table entry中的文件偏移量,没有I/O发生。)

  3.11 Atomic Operations (原子操作)

  Any operation that requires more than one function call cannot be atomic, as there is always possibility that the kernel can temporarily suspend the process betweent the two function calls. (任何需要调用两个函数的操作都不是原子操作。因为kernel在两个函数调用期间挂起进程。)

  pread and pwrite Functions (pread 和 pwrite 函数)

  #include <unistd.h>

  ssize_t pread(int filedes, void *buf, size_t nbytes, off_t offset);

              Returns: number of bytes read, 0 if end of file, -1 on error

  ssize_t pwrite(int filedes, const void *buf, size_t nbytes, off_t offset);

              Returns: number of bytes written if Ok, -1 on error

  Calling pread is equivalent to calling lseek followed by a call to read. (调用pread等同调用lseek后调read。)

  Calling pwrite is equivalent to calling lseek followed by a call to write. (调用pwrite等同调用lseek后调write。)

  Creating a File (创建文件)

  We said that the check for the existence of the file and the creation of the file was performed as an atomic operation. (我们说检查文件是否存在和创建文件也是原子操作。)

  If the operation is performed atomically, either all the steps are performed, or none are performed. (原子性:要么都执行,要么一个也不执行。)

  3.12 dup and dup2 Functions (dupdup2 函数)

  #include <unistd.h>

  int dup(int filedes);

  int dup2(int filedes, int filedes2);

            Both returns: new file descriptor if OK, -1 on error.

  With dup2, we specify the value of the new descriptor with the fileses2 argument. if filedes2 is already open, it is first close. If filedes equals filedes2, the dup2 returns filedes2 without closing it. (对于dup2函数,我们用filedes2参数指定了一个新的文件描述符值,如果filedes2已经打开,先关闭它。如果filedes和filedes2相同,返回filedes2而不关闭它。)

  The new file descriptor that is returned as the value of the functions shares the same file table entry as the filedes argument. (dup函数返回的新的文件描述符和filedes参数共享相同的file table entry。)

  3.13 sync, fsync, and fdatasync Functions (sync, fsync, 和 fdatasync 函数)

  #include <unistd.h>

  int fsync(int filedes);

  int fdatasync(int filedes);

            Returns: 0 if OK, -1 on error

  void sync(void);

  When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. This is called ”delay write“. (当我们向文件写入数据时, 数据通常会被kernel复制进它的缓存和队列中,等之后一段时间再写入硬盘。这称为“延迟写入”。)  

  The sync function simply queues all the modified block buffers for writing and returns; it does not  wait for the disk writes to take place. (sync函数只是排列所有更改过的将写入或返回的块缓存。它不等硬盘写操作发生。)

  The functio sync is normally called periodically(usually every 30 seconds) from a system daemon, often called update. The command sync(1) also calls the sync function. (sync函数通常会被系统守护进程定期(每30秒)调用,称为update。sync(1)命令也调用sync函数。)

       The funciton fsync refers only to a single file. specified by the file descriptor filedes, and wait for the disk writes to complete before returning. (fsync函数指向由参数filedes指定的单个文件,并且在等硬盘完全写完才返回。)

  The fdatasync function is similar to fsync, but it affects only the data portions of a file. With fsync, the file's attributes are also updated synchronously. (fdatasync函数和fsync函数相似,但它只影响文件的数据部分。fsync函数,文件的属性也会被同步更新。)

  3.14 fcntl Function (fcntl 函数)

  #include <fcntl.h>

  int fcntl(int filedes, int cmd, ... /* int arg */ );

            Returns: depends on cmd if OK, -1 on error

  The fcntl function can change the properties of a file that is already open. (fcntl函数可以改变一个打开文件的属性。)

  The fcntl function is used for five different purposes. (fcntl函数用于五种目的,如下)。

  1. Duplicate an existing descriptor(cmd = F_DUPFD) (复制一个存在的文件描述符)

  2. Get/set file descriptor flags(cmd = F_GETFT or F_SETFD) (获取/设置文件描述符标志)

  3. Get/set file status flags(cmd = F_GETFL or F_SETFL) (获取/设置文件状态符标志)

  4. Get/set asynchornous I/O ownership(cmd = F_GETOWN or F_SETOWN) (获取/设置异步I/O所有者)

  5. Get/set record locks(cmd = F_GETLK, F_SETLK, or F_SETKW) (获取/设置记录锁)

  cmd:

    F_DUPFD: Duplicate the file descriptor filedes. (复制文件描述符)

    F_GETFD: Return the file descriptor flags. Currently, only one flag is defined: the FD_CLOEXEC (返回文件描述符标志)

    F_SETFD: Set the file descriptor flag. The new flag value is set from the third argument.(设置文件描述符标志为第三个参数)

    F_GETFL: Return the file status flag. (返回文件状态符标志)

    F_SETFL: Set the file status flag to the value of the third argument.(设置文件状态符标志为第三个参数)

    F_GETOWN: Get the process ID or process group ID currently receiving the SIGIO and SIGURG signals. (获取进程ID或者进程组ID,目前接收SIGIO和SIGURG信号)

    F_SETOWN: Set the process ID or process group ID to receive the SIGIO and SIGURG signals. (设置进程ID或者进程组ID为接收SIGIO和SIGURG信号)

  3.15 ioctl Function

  #include <unistd.h> /* System V */

  #include <sys/ioctl.h> /* BSD and Linux */

  #include <strops.h> /* XSI STREAMS */

  int ioctl(int filedes, int request, ...);

            Returns: -1 on error, something else if OK

  Anything that couldn't be expressed using one of the other functions in this chapter usually ended up being specified with an ioctl. (在本章中凡是使用其他函数不能表达的,最终都指定了一个ioctl。)

  3.16 /dev/fd

  Newer systems provides a directory named /dev/fd whose entries are files named 0, 1, 2 and so on. (新的系统提供了一个叫做“/dev/fd”的目录,包含一些0,1,2,...为文件名的实体。)

  Opening the file /dev/fd/n is equivalent to duplicating descriptor n, assuming that descriptor n is open. (打开文件“/dev/fd/n”相当于复制文件描述符n(假设n是打开的)。)

  The main use of the /dev/fd files is from the shell. (/dev/fd 文件的主要用途就是通过shell使用。)

  Using /dev/fd is a step towards uniformity and cleanliness. (使用/dev/fd是一个保持均匀性和洁净度的方法。)

  3.17 Summary (总结)

  (完)

 

转载于:https://www.cnblogs.com/yanjf/p/3329774.html

你可能感兴趣的文章
生活大爆炸之何为光速
查看>>
[Typescript] Specify Exact Values with TypeScript’s Literal Types
查看>>
Illustrated C#学习笔记(一)
查看>>
理解oracle中连接和会话
查看>>
Scrapy实战篇(三)之爬取豆瓣电影短评
查看>>
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
在centos上开关tomcat
查看>>
无人值守安装linux系统
查看>>
黑马程序员——2 注释
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
查询消除重复行
查看>>