[文件I/O]非阻塞 I/O | O_NONBLOCK

21 篇文章 0 订阅

[文件I/O]非阻塞 I/O | O_NONBLOCK

转自:http://www.groad.net/bbs/read.php?tid-950.html

系统也可以分为
低速系统 和 其他。

低速系统调用是可能会使进程永远阻塞的一类系统调用:

  • 如果数据并不存在,则读文件可能会使调用者永远阻塞( 例如读管道,终端设备,网络设备 )
  • 如果数据不能被立即接受,则写这些文件同样也会使调用者永远阻塞。
  • 在某些条件发生之前,打开文件会被阻塞( 例如打开一个终端设备可能需要等到与之连接的调制解调器应答 );又如,如果以只写的方式打开 FIFO,那么在没有其他进程已用读方式打开该 FIFO 时也要等待(因为,FIFO一直写会被写满,没有其他进程去读出数据,则 FIFO 不能再继续写)。
  • 对已经加上强制性锁的文件进行读、写。
  • 某些 iotcl 操作。
  • 某些进程间通信函数。

虽然读、写磁盘文件会使调用在短暂时间内阻塞,但并不能将她们视为 “低速”。

 

非阻塞 I/O 使我们可以调用不会永远阻塞的 I/O 操作,例如 open, read 和 write 。如果这种操作不能完成,则立即出错返回 ---表示该操作如继续执行将继续阻塞下去。

对于一个给定的描述符有两种方法对其指定非阻塞 I/O
(1) 如果是调用 open() 函数获得该描述符,则可调用 fcntl() 打开 O_NONBLOCK 文件状态标志。
(2) 对于已经打开的一个描述符,则可调用 fcntl() 打开 O_NONBLOCK 文件状态标志。

下面测试代码,将说明一个非阻塞 I/O 操作实例,它的作用是:
从标准输入读入 100 000 个字节,并试图将它们写到标准输出上。该程序先将标准输出设置为非阻塞的,然后用 for 循环进行输出,每次写的结果都在标准出错上打印。

 

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. char buf[100000];
  8. void set_fl(int fd, int flags);
  9. void clr_fl(int fd, int flags);
  10. int main(void)
  11. {
  12.     int ntowrite, nwrite;
  13.     char *ptr;
  14.     /*STDIN_FILENO 宏定义为 0,为标准输入*/
  15.     ntowrite = read(STDIN_FILENO, buf, sizeof(buf));
  16.     fprintf(stderr, "read %d bytes\n", ntowrite);
  17.     set_fl(STDOUT_FILENO, O_NONBLOCK);    /*设置非阻塞*/
  18.    
  19.     for(ptr = buf; ntowrite > 0; ) {
  20.         errno = 0;
  21. nwrite = write(STDOUT_FILENO, ptr, ntowrite);
  22.         fprintf(stderr, "nwrite = %d, errno = %d\n", nwrite, errno);
  23.         if (nwrite > 0) {
  24.             ptr += nwrite;
  25.             ntowrite -= nwrite;
  26.         }
  27.     }
  28.     clr_fl(STDOUT_FILENO, O_NONBLOCK);    /*清除非阻塞*/
  29.     exit(0);
  30. }
  31. /*设置 open 标志*/
  32. void set_fl(int fd, int flags)
  33. {
  34.     int     val;
  35.     if ( (val = fcntl(fd, F_GETFL, 0)) < 0)
  36.         perror("fcntli get");
  37.    
  38.     val |= flags;
  39.    
  40.     if (fcntl(fd, F_SETFL, val) < 0)
  41.         perror("fcntl set");
  42. }
  43. /*清除 open 标志*/
  44. void clr_fl(int fd, int flags)
  45. {
  46.     int     val;
  47.     if ( (val = fcntl(fd, F_GETFL, 0)) < 0)
  48.         perror("fcntl get");
  49.    
  50.     val &= ~flags;
  51.    
  52.     if (fcntl(fd, F_SETFL, val) < 0)
  53.         perror("fcntl set");
  54. }


第 17 行,STDIN_FILENO 是标准输入描述符的宏定义,定义在 unistd.h 头文件中。意思是,从标准输入读入 sizeof(buf) 个字节数据到 buf  缓冲区中。
第 18 行,把一共读到的字节数打印到标准错误。
第 20 行,设置标准输出为非阻塞方式。
第 24 行,把 ptr 所指的内容送往标准输出。
第 22,26,27,28 行,在 for 循环中,ntowrite 为读取到的字节数,一般情况下会读到 100 000 个。由于终端的标准输出一次性可以接受的最大字节一般不可能这么大( 根据系统的不同而不同,而这个数字在某些系统上每次还可能不是固定的,如我的 opensuse11.1 就是如此 ),所以需要一个 for 循环分开几次把读到的内容输出。 nwrite 是一次写到标准输出的字节数。ptr += nwrite 是调整内容指针,ntowrite -= nwrite 表示剩下多少个字节数还没输出。


先看一个普通的文件

beyes@linux-beyes:~/C> ll /etc/termcap
lrwxrwxrwx 1 root root 23 04-18 12:51 /etc/termcap -> /usr/share/misc/termcap
beyes@linux-beyes:~/C> ll /usr/share/misc/termcap
-rw-r--r-- 1 root root 969976 2008-12-03 /usr/share/misc/termcap


由上面看到,termcap 文件的大小可以满足我们一次读取 100 000 个字节。

下面,把读取到的 100 000 个字节内容一次性写入到一个 temp.file 的文件中

beyes@linux-beyes:~/C> ./noblock.exe < /usr/share/misc/termcap > termp.file
read 100000 bytes
nwrite = 100000, errno = 0
beyes@linux-beyes:~/C> ll termp.file
-rw-r--r-- 1 beyes users 100000 07-14 19:19 termp.file


上面标准输出是 termp.file 文件,这时 write 只执行了一次!

假如,标准输出是终端,那么 write 的情形会怎么样呢?

beyes@linux-beyes:~/C> ./noblock.exe < /usr/share/misc/termcap 2>stderr.out


执行上面的命令,把读取的内容输出到标准终端。2>stderr.out 是把产生的错误导入到 stderr.out 文件中。

现在用 cat 命令来看一下 stderr.out 中的内容,非常多,部分返回内容为:

beyes@linux-beyes:~/C> cat stderr.out | more
read 100000 bytes
nwrite = 12007, errno = 0
nwrite = -1, errno = 11
nwrite = -1, errno = 11
nwrite = -1, errno = 11
... ...


在该系统上,errno 11 是 EAGAIN 。
查看所有 errno = 0 (无错) 的输出

beyes@linux-beyes:~/C> cat stderr.out | grep "errno = 0"
nwrite = 12007, errno = 0
nwrite = 8023, errno = 0
nwrite = 8016, errno = 0
nwrite = 4007, errno = 0
nwrite = 8010, errno = 0
nwrite = 4009, errno = 0
nwrite = 8011, errno = 0
nwrite = 6059, errno = 0
nwrite = 6058, errno = 0
nwrite = 8019, errno = 0
nwrite = 8024, errno = 0
nwrite = 4012, errno = 0
nwrite = 6060, errno = 0
nwrite = 6065, errno = 0
nwrite = 3620, errno = 0


可见,每次 write 的字节数都有不同,把这些字节数加起来,刚好就是 100, 000 。

再看一下共有多少个输出

beyes@linux-beyes:~/C> wc -l stderr.out
48423 stderr.out


errno 号为 11 的输出总共 4 万多个!
也就是说,write 的调用次数一共有 4 万次之多,而真正有用的,真正输出数据的才有 15 个!

究其原因,就是使用了非阻塞的输出方式造成的!因为不阻塞了,所以就造成 write 的肆无忌惮的调用!这种形式的循环称为 “轮询”,在多用户系统上,它浪费了大量的 CPU 时间。

 

使用poll函数,使用非阻塞输出效率提高的方法:

下面通过 poll() 的测试程序来测试一下性能的提升,测试代码如下:

引用
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
                                                                                                                             

#define BUFFER_SIZE     1000000
#define LINE_LEN        256   

typedef enum {B_FALSE, B_TRUE} boolean_t;               /*定义布尔变量*/

static char buf [BUFFER_SIZE];
int daemon_proc = 0;         

int set_fsflag (int fd, int new_flags)
{                                    
        int flags;                   

        if ((flags = fcntl (fd, F_GETFL)) == -1)        /*得到 fd 上的状态*/
                return (-1);                                               

        flags |= new_flags;                             /*对 fd 添加新的属性*/

        if ((flags = fcntl (fd, F_SETFL, flags)) == -1) /*设置新属性*/
                return (-1);                                         

        return (0);
}                 

int clear_fsflag (int fd, int new_flags)
{                                      
        int flags;                     

        if ((flags = fcntl (fd, F_SETFL, flags)) == -1)
                return (-1);                          

        return (0);
}                 

static void err_common (boolean_t flag, int level, const char *text, va_list args)
{                                                                                
        int old_errno;                                                           
        int n;                                                                   
        char buf [LINE_LEN];                                                     

        old_errno = errno;      /*获得错误号*/
#ifdef NEED_SNPRINTF                         
        n = vsprintf (buf, text, args);                 /*n 为写入到 buf 中的字节数,不包括'\0'*/
#else                                                                                          
        n = vsnprintf (buf, sizeof (buf), text, args);                                         
#endif                                                                                         
        if (flag)                                                                              
                snprintf (buf + n, sizeof (buf) - n, ": %s", strerror (old_errno));     /*附加出错具体提示*/
        strcat (buf, "\n");                                                                                

        if (daemon_proc)
                syslog (level, buf);    /*产生日志消息*/
        else {                                         
                fflush (stdout);                       
                fprintf (stderr, "%s", buf);           
                fflush (stderr);                       
        }                                              
}                                                      

void log_msg (const char *text, ...)
{                                  
        va_list arg;               
        va_start (arg, text);      
        err_common (B_FALSE, LOG_INFO, text, arg);
        va_end (arg);                            
}                                                

void err_msg (const char *text, ...)
{                                  
        va_list arg;               

        va_start (arg, text);
        err_common (B_TRUE, LOG_ERR, text, arg);
        va_end (arg);                          

        exit (1);
}               

void err_set (const char *text, ...)
{                                  
        va_list arg;               

        va_start (arg, text);
        err_common (B_TRUE, LOG_INFO, text, arg);
        va_end (arg);                           
}



int main (void)
{
        ssize_t n;
        ssize_t res;
        char *ptr;
        int errs;
        struct pollfd fds;

        errs = 0;
        n = read (STDIN_FILENO, buf, BUFFER_SIZE);
        log_msg ("Read %d bytes", n);

        set_fsflag (STDOUT_FILENO, O_NONBLOCK);

        fds.fd = STDOUT_FILENO;
        fds.events = POLLOUT;
        fds.revents = 0;

        ptr = buf;
        while (n > 0) {
                if (poll (&fds, 1, -1) == -1)
                        err_msg ("Can't poll");

                while ((n > 0) && ((res = write (STDOUT_FILENO, ptr, n)) > 0)) {
                        if (errs > 0) {
                                err_set ("write failed %d times\n", errs);
                                errs = 0;
                        }

                        log_msg ("Wrote %d bytes", res);
                        ptr += res;
                        n -= res;
                }
        }
        clear_fsflag (STDOUT_FILENO, O_NONBLOCK);

        return (0);
}



执行程序:

引用
[beyes@localhost poll]$ ./poll.exe < zypper.log 2> errors



不出错的话,输出读取到的 100,000 个字节内容。

查看生成的 errors 文件:

引用
[beyes@localhost poll]$ cat errors
Read 1000000 bytes               
Wrote 4059 bytes                 
Wrote 4061 bytes                 
Wrote 4061 bytes                 
Wrote 4062 bytes                 
Wrote 4064 bytes                 
Wrote 4071 bytes            
... ...



统计一下 errors 文件:

引用
[beyes@localhost poll]$ wc -l errors
247 errors

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值