【发布时间】:2015-04-28 04:31:52
【问题描述】:
如何在 unix 中为非阻塞代码提供输入? 代码如下:
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
void set_fl(int fd, int flags);
char buf[10];
int main()
{
int noread;
set_fl(STDIN_FILENO, O_NONBLOCK); //code for nonblocking
set_fl(0, O_NONBLOCK); //code for nonblocking
noread = read(0, buf, sizeof(buf));
printf("INPUT: %s\n", buf);
return 0;
}
void set_fl(int fd, int flags)
{
int val;
if ((val = fcntl(fd, F_GETFL, 0)) < 0)
printf("ERROR: %s\n", "fcntl F_GETFL error");
val |= flags; //changes the bit
if (fcntl(fd, F_SETFL, val) < 0)
printf("ERROR: %s\n", "fcntl F_SETFL error");
}
【问题讨论】:
-
你的问题是?