`
kanwoerzi
  • 浏览: 1643711 次
文章分类
社区版块
存档分类
最新评论

unix环境高级编程-文件读取,同步机制

 
阅读更多

3.12 dup和dup2函数

下面的两个函数用来赋值一个现存的文件描述符

成功则返回新的文件描述符,若出错则返回-1;

查看了gun c手册
int dup (int old) [Function]
This function copies descriptor old to the first available descriptor number (the first
number not currently open). It is equivalent to fcntl (old, F_DUPFD, 0).
int dup2 (int old, int new) [Function]
This function copies the descriptor old to descriptor number new.
If old is an invalid descriptor, then dup2 does nothing; it does not close new. Otherwise,
the new duplicate of old replaces any previous meaning of descriptor new, as if
new were closed first.
If old and new are different numbers, and old is a valid descriptor number, then dup2
is equivalent to:
close (new);
fcntl (old, F_DUPFD, new)
However, dup2 does this atomically; there is no instant in the middle of calling dup2
at which new is closed and not yet a duplicate of old.

dup返回的描述符一定是当前可用文件描述符忠最小的数值,用dup2则可以返回filedes2参数指定新描述符的数值。若果filedes2已经打开,则先关闭。如果相同,则不关闭他。

如下图

这些函数返回的新文件描述符与参数filedes共享同一个文件表项。他们都共享同一个文件状态标志(读写等)以及同一当前文件偏移量。

复制一个描述符的另一种方法是使用fcntl函数,调用

dup(filedes)

等效于

fcntl(filedes,F_DUPFD,0);

而调用

dup2(filedes,filedes2);

等效于

close(filedes2);

fcntl(filedes,F_DUPFD,filedes2);

3.13 sync、fsync、和fdatasync函数

传统的UNIX实现在内核忠设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓存进行。当数据文件写入文件中,内核通常先将该数据复制到其中一个缓冲区中,如果改缓冲区尚未写满,则并不将其排入输出队列,二师等待期写满或者内核需要改缓冲区。再将该缓冲区的数据输出。然后待其达到队首时候,才进行实际的I/O操作。这叫延迟写(delayed write)

优点:减少了写的次数。缺点:降低了文件内容更新速度。一旦出现故障,可能一些数据未写进文件。

所以这时候unix提供了三个函数 sync,fsync,fdatesync 三个函数

下面查阅一下GUN C手册
int sync (void) [Function]
A call to this function will not return as long as there is data which has not been
written to the device. All dirty buffers in the kernel will be written and so an overall
consistent system can be achieved (if no other process in parallel writes data).

int fsync (int fildes) [Function]
The fsync function can be used to make sure all data associated with the open file
fildes is written to the device associated with the descriptor. The function call does
not return unless all actions have finished.
A prototype for fsync can be found in ‘unistd.h’.
This function is a cancellation point in multi-threaded programs. This is a problem
if the thread allocates some resources (like memory, file descriptors, semaphores or
whatever) at the time fsync is called. If the thread gets canceled these resources stay
allocated until the program ends. To avoid this, calls to fsync should be protected
using cancellation handlers.
The return value of the function is zero if no error occurred. Otherwise it is

分享到:
评论

相关推荐

    高级UNIX编程 pdf 电子书

    本书以当前UNIX规范为基础,详细介绍了UNIX系统函数的用法,并用大量的代码和示例程序进行演示,对实际编程具有指导意义。全书共9章,内容包括:基本概念、基本文件I/O、高级文件I/0、终端I/O、进程与线程、基本...

    linux网络编程-宋敬彬-part3

    3.2.4 读取文件read()函数 77 3.2.5 写文件write()函数 79 3.2.6 文件偏移lseek()函数 80 3.2.7 获得文件状态fstat()函数 83 3.2.8 文件空间映射mmap()函数 85 3.2.9 文件属性fcntl()函数 88 3.2.10 ...

    linux网络编程-宋敬彬-part2

    3.2.4 读取文件read()函数 77 3.2.5 写文件write()函数 79 3.2.6 文件偏移lseek()函数 80 3.2.7 获得文件状态fstat()函数 83 3.2.8 文件空间映射mmap()函数 85 3.2.9 文件属性fcntl()函数 88 3.2.10 ...

    linux网络编程-宋敬彬-part1

    第2章 Linux编程环境 14 2.1 Linux环境下的编辑器 14 2.1.1 vim使用简介 14 2.1.2 使用vim建立文件 15 2.1.3 使用vim编辑文本 16 2.1.4 vim的格式设置 18 2.1.5 vim配置文件.vimrc 19 2.1.6 使用其他...

    《计算机操作系统》期末复习指导

    高级通信原语,用于一组信息发送(Send)与读取(Read)。 5、死锁 (1)死锁的概念 死锁是两个或两个以上的进程中的每一个,都在等待其中另一个进程释放资源而被封锁,它们都无法向前推进,称这种...

    Linux程序设计 第4版.haozip01

    9.2.9 高级主题:makefile文件和子目录 329 9.2.10 gnu make和gcc 329 9.3 源代码控制 330 9.3.1 rcs 331 9.3.2 sccs 336 9.3.3 rcs和sccs的比较 336 9.3.4 cvs 337 9.3.5 cvs的前端程序 340 9.3.6 ...

    Linux程序设计 第4版.haozip02

    9.2.9 高级主题:makefile文件和子目录 329 9.2.10 gnu make和gcc 329 9.3 源代码控制 330 9.3.1 rcs 331 9.3.2 sccs 336 9.3.3 rcs和sccs的比较 336 9.3.4 cvs 337 9.3.5 cvs的前端程序 340 9.3.6 ...

    Python Cookbook

    16.12 在UNIX中将主脚本和模块绑成一个可执行文件 587 第17章 扩展和嵌入 590 引言 590 17.1 实现一个简单的扩展类型 592 17.2 用Pyrex实现一个简单的扩展类型 597 17.3 在Python中使用C++库 598 17.4 调用...

    linux网路编程 中文 23M 版

    第2 章Linux编程环境....................................................................................................14 2.1 Linux环境下的编辑器................................................. 14 ...

    Foxpro 开发答疑160问

    69. 如何进行DOS/Windows文本文件与UNIX/Linux文本文件的相互转换 257 70. 如何遍历磁盘中的所有目录及文件 258 71. 如何使用低级函数读写文件 263 72. 如何为Visual FoxPro应用程序增加文件压缩功能 271 73. ...

    dataset:数据集是命令行工具,Go包,共享库和Python包,用于将JSON对象作为集合使用

    它的一些增强功能包括生成数据的能力以及与CSV文件之间导入,导出和同步JSON对象的功能。 数据集是用编程语言编写的。 它可以被其他基于Go的软件用作Go软件包。 Go支持生成C共享库。 通过编译Go源代码,您可以创建...

    JAVA面试题最全集

    描述一下JVM加载class文件的原理机制? 41.试举例说明一个典型的垃圾回收算法? 42.请用java写二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来. 43.请写一个java程序实现线程连接池功能? 44...

    精通websphere MQ

    安装环境................................................................................................... 32 2.1.1 硬件................................................................................

Global site tag (gtag.js) - Google Analytics