当我使用pipe时,我在select()
处被封锁. 当我们在stdin上获取数据时,前select()
将解除阻止
然后我将数据写入管道的写端,但select()
无法识别管道的读FD上的任何数据. 所以它无限期地阻塞.
int fd_pipe[2];
char buf[20];
fd_set set;
pipe(fd_pipe);
FD_ZERO(&set);
FD_SET(fd_pipe[0], &set); // Pipe read fd
FD_SET(0 , &set); // stdin fd
select(fd_pipe[0]+1, &set, NULL, NULL, NULL); // I type 1 char + ENTER to get past this point
if (FD_ISSET(0, &set))
{
read(0, buf, 20); // Can confirm we do get here
write(fd_pipe[1], buf, 1); // Lets put just one character in the pipe
}
select(fd_pipe[0]+1, &set, NULL, NULL, NULL); // <-- We get stuck here
if (FD_ISSET(fd_pipe[0], &set))
{
char d;
read(0, &d, 1);
assert(d == buf[0]);
}
如果我在没有select()
的情况下这样做,事情就会很好:
int fd_pipe[2];
pipe(fd_pipe);
char c = 'x';
write( fd_pipe[1], &c, 1);
c = 'a';
read( fd_pipe[0], &c, 1);
assert(c == 'x');
从更大的Angular 来看,我有一个多线程程序,它使用select()
和pipe()
来模拟取消read()
操作. 我用一个字符写入管道,目的是迫使select()
返回,这样我就可以关闭操作,而不是try 发送信号来取消主FD上的阻塞read()
. 但select()
不会回来.