zoukankan      html  css  js  c++  java
  • select应用于read函数 超时非阻塞方式

    /*
     * "Timed" read - timout specifies the # of seconds to wait before
     * giving up (5th argument to select controls how long to wait for
     * data to be readable).  Returns # of bytes read or -1 on error.
     *
     * LOCKING: none.
     */
    ssize_t
    tread(int fd, void *buf, size_t nbytes, unsigned int timout)
    {
        int                nfds;
        fd_set            readfds;
        struct timeval    tv;
    
        tv.tv_sec = timout;
        tv.tv_usec = 0;
        FD_ZERO(&readfds);
        FD_SET(fd, &readfds);
        nfds = select(fd+1, &readfds, NULL, NULL, &tv);
        if (nfds <= 0) {
            if (nfds == 0)
                errno = ETIME;
            return(-1);
        }
        return(read(fd, buf, nbytes));
    }
    
    /*
     * "Timed" read - timout specifies the number of seconds to wait
     * per read call before giving up, but read exactly nbytes bytes.
     * Returns number of bytes read or -1 on error.
     *
     * LOCKING: none.
     */
    ssize_t
    treadn(int fd, void *buf, size_t nbytes, unsigned int timout)
    {
        size_t    nleft;
        ssize_t    nread;
    
        nleft = nbytes;
        while (nleft > 0) {
            if ((nread = tread(fd, buf, nleft, timout)) < 0) {
                if (nleft == nbytes)
                    return(-1); /* error, return -1 */
                else
                    break;      /* error, return amount read so far */
            } else if (nread == 0) {
                break;          /* EOF */
            }
            nleft -= nread;
            buf += nread;
        }
        return(nbytes - nleft);      /* return >= 0 */
    }
  • 相关阅读:
    Python---面向对象---案例
    Python---面向对象---龟鱼游戏
    man lspci
    Python---面向对象---修学校
    Python---面向对象编程---自定义列表和集合操作类
    Python---面向对象编程
    Python---常用的内置模块
    man hdparm
    man lsof
    linux中文man手册安装
  • 原文地址:https://www.cnblogs.com/sherlockhomles/p/4819611.html
Copyright © 2011-2022 走看看