我正在编写一个小程序,用于轮询命名管道的缓冲区。为了测试它,我将登录到“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;
}
最佳答案
This function also works when I take out O_NONBLOCK
当您将文件描述符标记为非阻塞时,所有通常可以阻塞的操作(例如 read(2)
和 write(2)
)都会返回 -1
并设置 errno = EAGAIN
。
因此在您的情况下,read
立即返回 -1 信号“我现在还没准备好,稍后再试”。
关于c - 命名管道终止 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15457630/