有名管道的读写有阻塞和非阻塞两种,可以在open()时指定,下面我们对各种情况进行一些讨论。
//写进程#include #include #include #include #include #include #define FIFO_NAME '/tmp/myfifo'main(){ int fd; char w_buf[50]; int w_num; // 若fifo已存在,则直接使用,否则创建它 if((mkfifo(FIFO_NAME,0777)<0)&&(errno!=eexist)) {="" printf('cannot="" create="" fifo...\n');="" exit(1);="" }="" 以阻塞型只写方式打开fifo="" fd="open(FIFO_NAME,O_WRONLY);" w_num="write(fd,'abcdg\0',6);">0)&&(errno!=eexist))>
//读进程#include #include #include #include #define FIFO_NAME '/tmp/myfifo'main(){ char r_buf[50]; int fd; int r_num;// 若fifo已存在,则直接使用,否则创建它 if((mkfifo(FIFO_NAME,0777)<0)&&(errno!=eexist)) {="" printf('cannot="" create="" fifo...\n');="" exit(1);="" }="" 以阻塞型只读方式打开fifo="" fd="open(FIFO_NAME,O_RDONLY);" if(fd="=-1)" {="" printf('open="" %s="" for="" read="" error\n');="" exit(1);="" }="" 通过键盘输入字符串,再将其写入fifo,直到输入'exit'为止="" r_num="read(fd,r_buf,6);" printf('="" %d="" bytes="" read:%s\n',r_num,r_buf);="">0)&&(errno!=eexist))>
1.
写进程阻塞,读进程阻塞。
先运行写进程(被阻塞),再运行读进程,一切正常。
先运行读进程(被阻塞),再运行写进程,一切正常。
2.
写进程阻塞,读进程非阻塞。
就改一句代码 fd=open(FIFO_NAME,O_RDONLY | O_NONBLOCK),下面类似。
先运行写进程(被阻塞),再运行读进程,一切正常。
先运行读进程,程序直接崩掉(Segmentation fault (core dumped)),想想也挺自然的,没东西你还要读,而且不愿等。。。
3.
写进程非阻塞,读进程阻塞。
先运行写进程,open调用将返回-1,打开失败。
先运行读进程(被阻塞),再运行写进程,一切正常。
4.
写进程非阻塞,读进程非阻塞。
其实就是上面2,3类各取一半不正常的情况。。
还有我们在/tmp目录下通过ls -la命令可以看到管道文件myfifo的大小总是0,这是因为虽然FIFO文件存在于文件系统中,但FIFO中的内容都存放在内存中,所以文件大小始终为0。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。