简体   繁体   中英

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. 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

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 .

So in your case read immediately returns -1 signaling " I'm not ready right now, try again later ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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