這很奇怪!我試了一個代碼,它打開沒有O_NONBLOCK的文件,然後分3個階段進行。第三階段沒有正確地處理O_NONBLOCK標誌結果重置!
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
char buf[1024];
int rd;
const char* fn = "prova.txt";
int i = mkfifo(fn, 0666);
int fd = open(fn, O_RDONLY); // | O_NONBLOCK);
int flags = fcntl(fd, F_GETFL);
//flags &= ~O_NONBLOCK;
printf("1) Waits!\t\tflags=0%08o\n",flags);
rd= read(fd, buf, 100);
printf("%d %o\n",rd,flags);
flags |= O_NONBLOCK;
printf("2) Doesn't wait!\tflags=0%08o\n",flags);
fcntl(fd, F_SETFL, flags);
rd= read(fd, buf, 100);
printf("%d %o\n",rd,flags);
//This doen't act the flag ????
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
flags=fcntl(fd, F_GETFL);
printf("3) Waits!\t\tflags=0%08o\n",flags);
rd= read(fd, buf, 100);
printf("%d %o\n",rd,flags);
puts("End!");
return 0;
}
這裏是命令序列和輸出:
[email protected]:~$ ./a.out &
[2] 6555
[email protected]:~$ echo xxx >> prova.txt
1) Waits! flags=000100000
4 100000
2) Doesn't wait! flags=000104000
0 104000
3) Waits! flags=000100000
0 100000
End!
[email protected]:~$
看起來像C++而不是C.請參閱http://stackoverflow.com/questions/2784500/how-to-send-a-simple-string-between-two-programs-using-pipes –
是C++, c區別重要嗎? – hovo
當你從fifo讀入'rd = 0'的結果時,errno的值是否有變化? –