Advanced Programming in UNIX Environment Episode 70
发布日期:2021-10-07 23:47:44 浏览次数:1 分类:技术文章

本文共 3485 字,大约阅读时间需要 11 分钟。

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 
#include
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

转载地址:https://blog.csdn.net/myfather103/article/details/80020637 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Advanced Programming in UNIX Environment Episode 71
下一篇:Advanced Programming in UNIX Environment Episode 69

发表评论

最新留言

很好
[***.229.124.182]2024年04月25日 19时55分39秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章