简体   繁体   English

命名管道终止会话

[英]named pipe terminating session

I'm writing a short program that's polling the buffer of a named pipe. 我正在编写一个轮询命名管道缓冲区的简短程序。 To test it out, I'll log into 'nobody' and echo into the pipe. 为了进行测试,我将登录“ nobody”并回显到管道中。 while it's hanging, I log in from a different user and run the program that reads the buffer. 挂起时,我从其他用户登录并运行读取缓冲区的程序。 When it runs, the program returns nothing and the other user is logged out of the system. 运行该程序时,该程序不返回任何内容,并且另一个用户退出系统。 Here's the read function: 这是读取功能:

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 当我取出O_NONBLOCK时,此功能也有效

When you mark a file descriptor as non blocking, all the operations that normally can block (for example read(2) , and write(2) ) return -1 instead and set errno = EAGAIN . 当您将文件描述符标记为非阻塞时,通常可以阻塞的所有操作(例如read(2)write(2) )都将返回-1并设置errno = EAGAIN

So in your case read immediately returns -1 signaling " I'm not ready right now, try again later ". 因此,在您的情况下, read立即返回-1,表示“ 我现在尚未准备好,请稍后再试 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM