默认文件描述符是阻塞的,即文件IO是阻塞的
设置为非阻塞:
int setNonBlock(int fd)
{
int flags = fcntl(fd, F_GETFL);
if (flags == -1)
{
return flags;
}
flags |= O_NONBLOCK;
int ret = fcntl(fd, F_SETFL, flags);
return ret;
}
从非阻塞还原到阻塞:
int setBlock(int fd)
{
int ret = 0;
int flags = fcntl(fd, F_GETFL);
if (flags == -1)
{
return flags;
}
flags &= ~O_NONBLOCK;
ret = fcntl(fd, F_SETFL, flags);
return ret;
}