当前位置:网站首页>UNIX Environment Advanced Programming Chapter 3
UNIX Environment Advanced Programming Chapter 3
2022-07-29 18:06:00 【51CTO】
UNIX环境高级编程》Chapter 3 of the documentI/O的学习,This chapter covers opening files、读文件、write files, etc.The following is a systematic introduction to these functions.
1.open函数
The third parameter is usually used when a new file is created.
pathnameThe argument is the name of the file to open or to create,oflagThe options for the parameters are detailed in the options below.
O_RDONLY 只读打开.
O_WRONLY 只写打开.
O_RDWR 读写打开.
One and only one of these three parameters is specified.The following parameters are optional.
O_APPEND Appends to the end of the file each time it is written.详见3.11
O_CREAT Create a file if the file does not exist,This option requires a third oneopen函数的第三个参数----mode,Specifies the allow bits that new files get.详见4.5
O_EXCL 如果O_CREATis specified and the file already exists generates an error.详见3.11
O_TRUNC If the file exists and has been opened for read-only or read-write,The file is truncated to 0.
O_NOCTTY 如果pathnameA terminal device is specified,Do not assign a controlling terminal for this device to this process.详见9.6
O_NONBLOCK 如果parhname指定的是一个FIFO,一个块设备,Or a special byte device,This option is non-blocking for opening the file this time and subsequent onesI/O操作.
The following three parameters are also optional,They are options for synchronizing output and input.
D_DSYNC 使每次write等待物理I/O操作完成,But if the write operation does not affect the reading of the data just written,Do not wait for file attributes to be updated.
O_RSYNC Make each one take a file descriptor as an argumentread操作等待,Until any unwritten operations on the same part of the file are complete
O_SYNC 使每次write都等待物理I/O操作完成,包括write操作引起的文件属性更新所需的I/O
2.creat函数
也可调用creat函数创建一个新文件.
这个函数等价于
creatThe function can only create a write-only file,If you want to create a temporary file,Read then write and then callcreat,close,最后在open,最好的方式是调用open函数如下所示
3.close 函数
文件调用close函数结束
Usually the current offset of the file is a non-negative integer,But some devices allow negative offsets,But for normal files,The offset must be non-negative.
odcommand to see the actual contents of the file,-cis to print the content as characters.
5.read 函数
读普通文件时,The end of the file was reached before the requested byte was read.例如,If on duty at the end of the file, there is still30个文件,而要求读100个字节,则read返回30,下一次再调用read时,它将返回0(文件尾部).
当从终端设备读时,通常一次最多读一行(18chapter describes how to change this).
当从网络读时,网络中的缓冲机制可能造成返回值小于所要求读的字节数.
When from the pipeline orFIFO读时,If the slave contains fewer bytes than required,那么readwill only return the number of bytes available to the world.
When reading from some record-oriented devices,一次最多返回一个记录.
When an interrupt is caused from a certain signal,When only part of the amount of data has been read.The read operation starts at the current offset of the file,在成功返回之前,This offset will be incremented by the number of bytes actually read.
POSIX标准read函数如下.
int read(int filedes, char *buf, unsigned nbytes);
6.write函数
3.9 I/O效率
经过测试BUFSIZE为4096The system time used is minimal.
3.10 文件共享
内核使用三种数据结构表示打开的文件,它们之间的关系决定了在文件共享方面一个进程对另一个进程可能产生的影响.
(1)每个进程在进程表中都有一个记录项,记录项中包含有一张打开文件描述符表,可将其视为一个矢量,每个描述符占用一项,与每个文件描述符相关联的是:
(a)文件描述符标志
(b)指向一个文件表项的指针
(2)内核为所有打开文件维持一张文件表.每个文件表项包含:
(a)文件状态标志
(b)当前文件偏移量
(c)指向该文件v节点表项的指针
(3)每个打开文件都有一个v节点结构.v节点包含了文件类型和对此文件进行各种操作的函数的指针.对于大多数文件.b节点还包含了该文件的i节点(i-node,索引节点).This information is read into memory from disk when the file is opened,All information about the file is quickly available.例如,i节点包含了文件的所有者、文件长度、文件所在的设备、A pointer to where the actual data blocks of the file reside on disk, and so on.
如果两个独立进程各自打开了同一个文件,则有图3-2arrangement shown in .We assume the file descriptor of the first process3On vigorously this document,And let a process be on the file descriptor4上打开该文件.Every process that opens the file gets a file table entry,But only one file for a given pointv节点表项.每个进程都有自己的文件表项的一个理由是:This arrangement allows each base to have its own current offset to the file.
在完成每个write后,The current file offset in the file table entry is incremented by the number of bytes of the abbreviation.If this makes the current file offset more than the current file length,则在iThe current file length in the node table entry is set to the current file offset.
如果用O_APPEND标志打开一个文件,Then the corresponding flag is also set to the file status flag of the file entry.Every time a write operation is performed on such a file with fill flags.The current file offset in the file table entry is first set to i节点表项中的文件长度.这就使得每次写的数据都添加到文件的当前尾端处.
若一个文件用lseekNavigate to the current end of the file,则文件表项中的当前文件偏移量被设置为i节点表项中的当前文件长度.
lseek函数只修改文件表项中的当前文件偏移量,没有进行任何I/O操作.
3.11 原子操作
假定有两个独立的进程A和BBoth add operations to the same file.Every process has opened the file,但未使用O_APPEND标志.此时,The relationship between the data structures is shown in the figure3-2中所示.每个进程都有它自己的文件表项,但是共享一个v节点表项.假定进程A调用了lseek,它将进程A的该文件当前偏移量设置为1500字节.Then the kernel switches the process to make the processB允许.进程B执行lseek,will also processA的该文件当前偏移量设置为1500字节.然后B调用write,它将B执行lseek,也将其对该文件的当前偏移量设置为1500字节.然后B调用write,它将BThe current offset of this file to this file has been to1600,.Because the length of the file has increased,所以内核对vThe current file length offset in the node is updated to 1600.Then perform process switching to make the processA恢复运行.当A调用write时,Just the offset from the file(1500字节)The data is written to the file.This also replaces the processBJust wrote to the file.
3.12 dup和dup2函数
dupThe new file descriptor returned must be the smallest number of file descriptors currently available,用dup2then availablefiledes2参数指定新描述符的数值.如果filedes2已经打开,则先将其关闭.如若filedes等于filedes2,则dup2返回filedes2,而不是关闭它.
Another way to copy a descriptor is to use fcntl函数,3.14Sections describe the functions.
dup(filedes);等效于
fcntl(filedes, F_DUPFD, 0);
而调用dup2(filedes, filrdes2);等效于
close(filedes2);
fcntl(filedes, F_DUPFD,filedes2);
在后一种情况下,dup2并不完全等同于close以及fcntl.它们之间的区别是:
(1)dup2is a yard operation,而close及fcntlthen includes two function calls.有可能在close和fcntl之间插入执行信号捕获函数,它可能修改文件描述符.
(2)dup2和fcntl有某些不同的errno.
3.13sync、fsync、fdatasync函数
sync函数只是将所有修改过的块缓冲区排入写队列,然后就返回,It does not mean the end of the actual write to disk operation.
通常称为updateThe system daemon is called periodicallysync函数.这就保证了定期冲洗内核的块缓冲区.
fsync函数只对由文件描述符filedes指定的单一文件起作用,并且等待写磁盘操作结束,然后返回.fsyncApplications that can be used with databases,This is used by programs to ensure that modified blocks are written to disk immediately.
fdatasync函数类似于fsync,但它只影响文件的数据部分.而除数据外,fsync还会同步更新文件的属性.
3.14 fcntl函数
fcntl函数可以改变已打开的文件的性质.
#include <fcntl.h>
int fcntl(int filedes, int cmd,../*int arg*/);
第三个参数则是指向一个结构的指针
fcntl函数有5种功能:
(1)复制一个现有的描述符(cmd=F_DUPFD).
(2)获得/设置文件描述符标记(cmd=F_GETFD或F_SETFD).
(3)获得/设置文件状态标志(cmd=F_GETFL或F_SETFL).
(4)获得/设置异步I/O所有权(cmd=F_GETOWN或F_SETOWN).
(5)获得/设置记录锁(cmd=F_GETLK、F_SETLK或F_SETLKW).
边栏推荐
猜你喜欢
随机推荐
解析正则表达式的语法(二)
澜舟孟子轻量化预训练模型技术实践
The two armies clash
Domino服务器SSL证书安装指南
浅析无人机发展趋势以及如何实现EasyDSS+无人机视频推流?
LinkedList 5-141. The circular linked list
多智能体协同控制研究中光学动作捕捉与UWB定位技术比较
清道夫受体-A靶向脂肪酸修饰白蛋白纳米粒/银耳多糖修饰白蛋白微球的制备
【高并发】我用多线程进一步优化了亿级流量电商业务下的海量数据校对系统,性能再次提升了200%!!(全程干货,建议收藏)
flink cdc source支持redis 和es吗
(notes) Build the was configured to -- Settings repositories over project repositories but solutions
How CRM Helps Enterprise Marketing Acquire Customers
sorting and searching 二分查找法
Six basic experiments of STC8h1k28
Groeb - "gramm, explicit and complete n -" gramm mask language model, implements the explicit n - "gramm semantic unit modeling knowledge.
一文搞定代码中的命名
lua-调试技巧
面试官:MySQL如何根据执行计划调优SQL语句?
【微信小程序】零基础学 | 小程序语法
[C language brush questions] Explanation of the use of linked lists