【转载】Non-blocking I/O with regular files

一句话说明:将常规文件的描述符设置为非阻塞是没有任何效果的! 

=========== 我是分割线 ============ 

Every now and then, I hear some programmer complain that a given piece of code uses blocking I/O. The claim is typically that blocking I/O damages the responsiveness of applications, especially if it has a user interface. Hence, solely non-blocking I/O should be used, along with polling (poll() or select()) or an event handling framework (glib, Qt, etc).

I can sympathize with the goal of improving applications responsiveness.But that is not an excuse for mixing up blocking with sleeping. Blocking is just one of several ways to sleep. In other words, non-blocking operations can sleep. Indeed turning non-blocking mode on for a file descriptor will not prevent sleeping in all cases that it could occur, but only one (or two) of them (depending how you count).

Blocking mode refers to one particular and well defined form of sleep: waiting until a file descriptor can be written to or read from. What that really means depends on the type of the underlying file.

  • For sockets, readability means there is some unread data in the input buffers. This is well-known and this is probably the most common use case for non-blocking I/O. Conversely, writeability implies the output buffers are not full as defined by the underlying protocol of the socket. This usually corresponds to congestion control, though the exact mechanisms and policies may vary.
  • For pipes, readability means some unread data remains in the pipe buffer, or one task is blocking in a write to the other end of the pipe. Reciprocally, writeability means the pipe buffer has available room, or one task is blocking in a read operation on the pipe.
  • FIFOs are really exactly like pipes, except that they have a name in the file system hierarchy.
  • Terminals and pseudo-terminals also work much like pipes, with regard to I/O, except for the fact that they support duplex operations like sockets.
  • For devices (other than terminals), polling is implementation-defined. You need to check the device driver documentation.
  • For directories, polling is not defined. In any case, writing to directories is not allowed, and reading is only defined through synchronous APIs.
  • Regular files are always readable and they are also always writeable. This is clearly stated in the relevant POSIX specifications. I cannot stress this enough. Putting a regular file in non-blocking has ABSOLUTELY no effects other than changing one bit in the file flags.

Reading from a regular file might take a long time. For instance, if it is located on a busy disk, the I/O scheduler might take so much time that the user will notice the application is frozen.

Nevertheless, non-blocking mode will not work. It simply will not work. Checking a file for readability or writeability always succeeds immediately. If the system needs time to perform the I/O operation, it will put the task in non-interruptible sleep from the read or write system call.

In other words, if you do know that a file descriptor refers to a regular file, do not waste your time, or worse, other people's time implementing non-blocking I/O. 
The only safe way to read data from or write data to a regular file while not blocking a task... is to not do it - in that particular task. Concretely, you need to create a separate thread (or process), whether you like it or not, even if you think threads suck (which usually really means you are an incompetent programmer who cannot use threads properly).

An alternative, of course, involves reading small chunks of data at once, and handling other events in-between. Then again, even reading a single byte can take a long time, if said byte was not read ahead by the operating system.

原文地址:http://www.remlab.net/op/nonblock.shtml

时间: 2024-09-18 22:18:07

【转载】Non-blocking I/O with regular files的相关文章

正则表达式的高级技巧8个常用的概念

正则表达式高级技巧背后的关键概念 英文原文来自 Smashing Magazine .由 笨活儿 翻译.转载请注明出处. 正则表达式(Regular Expression, abbr. regex) 功能强大,能够用于在一大串字符里找到所需信息.它利用约定俗成的字符结构表达式来发生作用.不幸的是,简单的正则表达式对于一些高级运用,功能远远不够.若要进行筛选的结构比较复杂,你可能就需要用到高级正则表达式. 本文为您介绍正则表达式的高级技巧.我们筛选出了八个常用的概念,并配上实例解析,每个例子都是满

linux下判断文件和目录是否存在[总结]

1.前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在.我结合APUE第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询. 2.stat系列函数 stat函数用来返回与文件有关的结构信息.stat系列函数有三种情况,分别对应文件名称.文件描述符和符号链接文件.stat结构描述了文件的属性,主要包括文件的类型.文件大小等等.详细stat结构如下所示: 1 struct stat { 2 mode_t st_mode; // file type & mode(permi

【原创】shell 操作之 read、cat 和 here document

本文主要学习总结一下三方面问题:  通过 read 进行行读 here document here document 的应用 [read] 在 linux 下执行 man read 能看到如下内容 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

用C语言实现统计一个文件夹中各种文件的比例

原文:用C语言实现统计一个文件夹中各种文件的比例 <UNIX环境高级编程>中的程序清单4-7就介绍了如何实现递归地统计某个目录下面的文件!我刚开始看过它的代码后,觉得照着敲太没意思了,所以就合上书自己写了一遍!为此还写了一篇博文,这是博文地址:在linux下用C语言实现递归查看某个目录中的所有文件[CSDN]! 今天做<Unix环境高级编程>的课后题,看到题目4.11这里提供了一种新的实现这个程序的思路,那就是每回读到一个目录,就通过chdir函数进入到这个目录,然后再通过open

linux下使用 du查看某个文件或目录占用磁盘空间的大小

du -ah --max-depth=1 这个是我想要的结果 a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式,max-depth表示目录的深度. du命令用来查看目录或文件所占用磁盘空间的大小.常用选项组合为:du -sh 一.du的功能:`du` reports the amount of disk space used by the specified files and for each subdirectory (of directory arguments)

正则表达式高级技巧及实例详解 笨活儿_正则表达式

英文原文来自Smashing Magazine.由笨活儿翻译.转载请注明出处. 正则表达式(Regular Expression, abbr. regex) 功能强大,能够用于在一大串字符里找到所需信息.它利用约定俗成的字符结构表达式来发生作用.不幸的是,简单的正则表达式对于一些高级运用,功能远远不够.若要进行筛选的结构比较复杂,你可能就需要用到高级正则表达式. 本文为您介绍正则表达式的高级技巧.我们筛选出了八个常用的概念,并配上实例解析,每个例子都是满足某种复杂要求的简单写法.如果你对正则的基

PHP中fwrite与file_put_contents的区别

相同点:file_put_contents() 函数把一个字符串写入文件中,与依次调用 fopen(),fwrite() 以及 fclose() 功能一样. 不同点:在file_put_contents()函数中使用 FILE_APPEND 可避免删除文件中已有的内容,即实现多次写入同一个文件时的追加功能. 例如: echo file_put_contents("test.txt","Hello World. Testing!",FILE_APPEND); file

《Linux系统编程(第2版)》——1.4 Linux编程的概念

1.4 Linux编程的概念 本节给出了Linux系统提供的服务的简要概述.所有的UNIX系统,包括Linux,提供了共同的抽象和接口集合.实际上,UNIX本身就是由这些共性定义的,比如对文件和进程的抽象.管道和socket的管理接口等等,都构成了UNIX系统的核心. 本概述假定你对Linux环境很熟悉:会使用shell的基础命令.能够编译简单的C程序.它不是关于Linux或其编程环境的,而是关于Linux系统编程的基础. 1.4.1 文件和文件系统文件是Linux系统中最基础最重要的抽象.Li

正则表达式高级技巧及实例详解 笨活儿

英文原文来自Smashing Magazine.由笨活儿翻译.转载请注明出处. 正则表达式(Regular Expression, abbr. regex) 功能强大,能够用于在一大串字符里找到所需信息.它利用约定俗成的字符结构表达式来发生作用.不幸的是,简单的正则表达式对于一些高级运用,功能远远不够.若要进行筛选的结构比较复杂,你可能就需要用到高级正则表达式. 本文为您介绍正则表达式的高级技巧.我们筛选出了八个常用的概念,并配上实例解析,每个例子都是满足某种复杂要求的简单写法.如果你对正则的基