我正在写一个简短的程序来轮询命名管道的缓冲区。为了测试它,我将登录'nobody'并回显到管道。当它挂起时,我从另一个用户登录并运行读取缓冲区的程序。运行时,程序不返回任何内容,另一个用户退出系统。这是读取功能:
void ReadOut( char * buf )
{
ZERO_MEM( buffer, BUF_SIZE );
int pipe = open( buf, O_RDONLY | O_NONBLOCK );
if( pipe < 0 )
{
printf( "Error %d has occured.\n" , pipe );
return;
}
while( read( pipe, buffer, 2 ) > 0 ) printf( "%s \n" , buffer );
close( pipe );
return;
}
答案 0 :(得分:1)
当我取出O_NONBLOCK
时,此功能也有效
当您将文件描述符标记为非阻止时,通常可以阻止的所有操作(例如read(2)
和write(2)
)都会返回-1
并设置errno = EAGAIN
所以在你的情况下read
立即返回-1信号“我现在还没有准备好,稍后再试一次”。