'programming'에 해당되는 글 387건

  1. 2017.12.20 is_nonblocking / nonblocking_mode_setting

반응형

 

int is_nonblocking (int fd)
{
    int flag;

    flag = fcntl (fd, F_GETFL, 0);

    if (flag < 0) {
        fprintf(stderr, "ERROR_FAIL fcntl get flag[%d] errno[%d][%s]", flag, errno, strerror(errno));
        return (-1);
    }

    return (flag & O_NONBLOCK);

}

int nonblocking_mode_setting(int fd)
{
    int nRet;
    int flag;

    flag = fcntl(fd, F_GETFL, 0);

    if (flag < 0) {
        fprintf(stderr, "ERROR_FAIL fcntl get flag[%d] errno[%d][%s]", flag, errno, strerror(errno));
        return (-1);
    }

    nRet = fcntl(fd, F_SETFL, flag | O_NONBLOCK );

    if( nRet < 0 ) {
        fprintf(stderr, "ERROR_FAIL fcntl set flag[%d] errno[%d][%s]", flag, errno, strerror(errno));
        return (-1);
    }

    return nRet;

}

-----------------------------------------

 


void set_non_blocking_mode(int sock)
{
  int flags = fcntl(sock, F_GETFL, 0);
  fcntl(sock, F_SETFL, flags | O_NONBLOCK);
}

 

 

----------------------------

 


int real_set_block_fd(int fd)
{
    int flags;

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

    flags &= ~O_NONBLOCK;
    flags = fcntl(fd, F_SETFL, flags);
    return flags;
}

int real_set_nonblock_fd(int fd)
{
    int flags;

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

    flags |= O_NONBLOCK;
    flags = fcntl(fd, F_SETFL, flags);
    return flags;
}

 

반응형
Posted by 공간사랑
,