I have a sample program:
int main()
{
const char* fn = "/tmp/tmpfifo";
int i = mkfifo(fn, 0666);
int fd = open(fn, O_RDONLY | O_NONBLOCK);
int flags = fcntl(fd, F_GETFL);
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
char buf[1024];
int rd= read(fd, buf, 100);
cout << rd << endl;
remove(fn);
return 0;
}
It seems that after removing the non-blocking flag from the file descriptor, the read
call should block until something is written into the FIFO, but my program always runs without blocking and rd=0
result. Can you please explain this behaviour? Thanks!
rd = 0
as result, is there any change inerrno
value? – Ballplayercout << rd << endl;
is pure C++ but everything is explain in the previous link #2785000 – Mernaprintf
and is not relevant to the discussion. – Paunchyflags
before/afterflags &= ~O_NONBLOCK;
to insure the bitwise negation is accomplishing what you need? Why not setO_RDONLY
insteadflags
with your 2nd call tofcntl
as a test.? – Paunchy