首页 > 其他 > 详细

用fcntl()设置堵塞函数的堵塞性质

时间:2015-01-24 18:35:36      阅读:322      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define ERR_EXIT(m)     do {         perror(m);        exit(EXIT_FAILURE);    }while(0)

void activate_nonblock(int fd);
void deactivate_nonblock(int fd);

int main(int argc, const char *argv[])
{
    activate_nonblock(STDIN_FILENO);
    char buffer[1024] = {0};
    int ret = read(STDIN_FILENO, buffer, 1024);
    if(ret == -1)
        printf("read");
    return 0;
}

void activate_nonblock(int fd)
{
    int ret;
    int flags = fcntl(fd, F_GETFL);
    if(flags == -1)
        ERR_EXIT("fcntl");
    flags |= O_NONBLOCK;
    ret = fcntl(fd, F_SETFL, flags);
    if(ret == -1)
        ERR_EXIT("fcntl");
}

void deactivate_nonblock(int fd)
{
    int ret;
    int flags = fcntl(fd, F_GETFL);
    if(flags == -1)
        ERR_EXIT("fcntl");
    flags &=~O_NONBLOCK;
    ret = fcntl(fd, F_SETFL, flags);
    if(ret == -1)
        ERR_EXIT("fcntl");
}

用fcntl()设置堵塞函数的堵塞性质

原文:http://www.cnblogs.com/mengfanrong/p/4246169.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!