Reputation: 4337
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;
}
Upvotes: 1
Views: 101
Reputation: 182619
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".
Upvotes: 1