---
title: "UNIX编程(14)-高级IO"
author: "brxonline"
date: "2011-08-03T21:36:40Z"
tags: []
categories: ["操作系统"]
slug: "unix-14-io"
draft: false
lastmod: "2011-08-03T22:27:15Z"
id: 1139524
url: "https://brxonline.iteye.com/blog/1139524"
---

1.非阻塞IO

对于一个给定的描述符有两种方法对其指定非阻塞IO

1）如果调用open获得描述符，则可指定O\_NONBLOCK标志

2）对于一个已经打开的描述符,则可调用fcntl由该函数打开O\_NONBLOCK文件状态标志

 

```
#include "apue.h"
#include <errno.h>
#include <fcntl.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); /* set nonblocking */

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

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

    clr_fl(STDOUT_FILENO, O_NONBLOCK); /* clear nonblocking */

    exit(0);
}
```

 

2\. 记录锁

 

记录锁锁定文件中的一个区域

 

```
#include <fcntl.h>

int fcntl(int filedes, int cmd, ... /* struct
 flock *flockptr */ );
```

 

Returns: depends on cmd if OK (see following), 1 on error

 

 

cmd is `F_GETLK`, `F_SETLK`, or `F_SETLKW`

 

struct flock {  
     short l\_type;   /* F\_RDLCK, F\_WRLCK, or F\_UNLCK \*/  
     off\_t l\_start;  /* offset in bytes, relative to l\_whence \*/  
     short l\_whence; /* SEEK\_SET, SEEK\_CUR, or SEEK\_END \*/  
     off\_t l\_len;    /* length, in bytes; 0 means lock to EOF \*/  
     pid\_t l\_pid;    /* returned with F\_GETLK \*/  
   };

 

例：加锁和解锁一个文件区域的函数

```
#include "apue.h"
#include <fcntl.h>

int
lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
    struct flock lock;

    lock.l_type = type;     /* F_RDLCK, F_WRLCK, F_UNLCK */
    lock.l_start = offset;  /* byte offset, relative to l_whence */
    lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
    lock.l_len = len;       /* #bytes (0 means to EOF) */

    return(fcntl(fd, cmd, &lock));
}
```

 

 

锁的隐含继承和释放

1）

2）

3）

4）

建议性锁和强制性锁

 

3.STREAMS

1）putmsg和putpmsg函数

2)ioctl

3)写至流

4）写模式

5）getmsg和getpmsg函数

6）读模式

 4.IO多路转接

 

[]()

#include &lt;sys/select.h&gt;

int select(int maxfdp1, fd\_set \*restrict readfds,  
           fd\_set \*restrict writefds, fd\_set  
![](/admin/blogs/1139524/images/ccc.gif) \*restrict exceptfds,  
           struct timeval \*restrict tvptr);

 

Returns: count of ready descriptors, 0 on timeout, 1 on error

 

 

[]()

[\[View full width\]](/admin/blogs/1139524/ch14lev1sec5.html#PLID11)

```
#include <sys/select.h>

int pselect(int maxfdp1, fd_set *restrict readfds,
            fd_set *restrict writefds, fd_set
 *restrict exceptfds,
            const struct timespec *restrict tsptr,
            const sigset_t *restrict sigmask);
```

 

Returns: count of ready descriptors, 0 on timeout, 1 on error

 

[]()

[\[View full width\]](/admin/blogs/1139524/ch14lev1sec5.html#PLID12)

```
#include <poll.h>

int poll(struct pollfd fdarray[], nfds_t nfds, int
timeout);
```

 

Returns: count of ready descriptors, 0 on timeout, 1 on error

 

 

5.异步IO

6.readv和writev函数

7.readn和writen函数

 

8存储映射IO

 

 

[]()

[\[View full width\]](/admin/blogs/1139524/ch14lev1sec9.html#PLID0)

```
#include <sys/mman.h>

void *mmap(void *addr, size_t len, int prot, int
flag, int filedes,
           off_t off );
```

 

Returns: starting address of mapped region if OK, `MAP_FAILED` on error

 

 

例：用存储映射IO复制文件

```
#include "apue.h"
#include <fcntl.h>
#include <sys/mman.h>

int
main(int argc, char *argv[])
{
    int         fdin, fdout;
    void        *src, *dst;
    struct stat statbuf;

    if (argc != 3)
        err_quit("usage: %s <fromfile> <tofile>", argv[0]);

    if ((fdin = open(argv[1], O_RDONLY)) < 0)
        err_sys("can't open %s for reading", argv[1]);

    if ((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC,
      FILE_MODE)) < 0)
        err_sys("can't creat %s for writing", argv[2]);

    if (fstat(fdin, &statbuf) < 0)   /* need size of input file */
        err_sys("fstat error");

    /* set size of output file */
    if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
        err_sys("lseek error");
    if (write(fdout, "", 1) != 1)
        err_sys("write error");

    if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED,
      fdin, 0)) == MAP_FAILED)
        err_sys("mmap error for input");

    if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
      MAP_SHARED, fdout, 0)) == MAP_FAILED)
        err_sys("mmap error for output");

    memcpy(dst, src, statbuf.st_size); /* does the file copy */
    exit(0);
}
```