Advanced Programming in UNIX Environment Episode 70

Advanced I/O

Introduction

This chapter covers numerous topics and functions that we lump under the term advanced I/O: nonblocking I/O, record locking, I/O multiplexing (the select and poll functions), asynchronous I/O, the readv and writev functions, and memory-mapped I/O (mmap).

Nonblocking I/O

The slow system calls are those that can block forever. They include

  • Reads that can block the caller forever if data isn’t present with certain file types (pipes, terminal devices, and network devices)
  • Writes that can block the caller forever if the data can’t be accepted immediately by these same file types (e.g., no room in the pipe, network flow control)
  • Opens that block until some condition occurs on certain file types (such as an open of a terminal device that waits until an attached modem answers the phone, or an open of a FIFO for writing only, when no other process has the FIFO open for reading)
  • Reads and writes of files that have mandatory record locking enabled
  • Certain ioctl operations
  • Some of the interprocess communication functions (Chapter 15)

There are two ways to specify nonblocking I/O for a given descriptor.

1.If we call open to get the descriptor, we can specify the O_NONBLOCK flag (Section 3.3).
2.For a descriptor that is already open, we call fcntl to turn on the O_NONBLOCK file status flag (Section 3.14). Figure 3.12 shows a function that we can call to turn on any of the file status flags for a descriptor.

Earlier versions of System V used the flag O_NDELAY to specify nonblocking mode. These versions of System V returned a value of 0 from the read function if there wasn’t any data to be read. Since this use of a return value of 0 overlapped with the normal UNIX System convention of 0 meaning the end of file, POSIX.1 chose to provide a nonblocking flag with a different name and different semantics.
We’ll see that POSIX.1 requires that read return −1 with errno set to EAGAIN if there is no data to read from a nonblocking descriptor. Some platforms derived from System V support both the older O_NDELAY and the POSIX.1 O_NONBLOCK, but in this text we’ll use only the POSIX.1 feature. The older O_NDELAY is intended for backward compatibility and should not be used in new applications.
4.3BSD provided the FNDELAY flag for fcntl, and its semantics were slightly different. Instead of affecting only the file status flags for the descriptor, the flags for either the terminal device or the socket were also changed to be nonblocking, thereby affecting all users of the terminal or socket, not just the users sharing the same file table entry (4.3BSD nonblocking I/O worked only on terminals and sockets). Also, 4.3BSD returned EWOULDBLOCK if an operation on a nonblocking descriptor could not complete without blocking. Today, BSD-based systems provide the POSIX.1 O_NONBLOCK flag and define EWOULDBLOCK to be the same as EAGAIN.

#include "apue.h"
#include <errno.h>
#include <fctnl.h>

char buf[500000];

int main(void)
{
    int ntowrite, nwrite;
    char *ptr;

    ntowrite=read(STDIN_FILENO,buf, sizeof(buf));
    fprintf(stderr, "read %d bytes\n", ntowrite);
    
    set_fl(STDOUT_FILENO,O_NONBLOCK);

    ptr=buf;
    while(ntowrite>0)
    {
        errno=0;
        nwrite=write(STDOUT_FILENO,ptr,notowrite);
        fprintf(stderr, "nwrite=%d, errno=%d\n", nwrite, errno);

        if(nwrite>0)
        {
            ptr+=nwrite;
            ntowrite-=nwrite;
        }
    }

    clr_fl(STDOUT_FILENO, O_NONBLOCK);
    
    return 0;
}

Large nonblocking write

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值