关于fifo的open阻塞问题

进程间通信之fifo,在阻塞模式下,只有当读和写模式都打开时才返回,否则一直阻塞;

非阻塞模式下,当读端没打开,则打开写端无效,返回错误。

建议你读一读UNP卷2。

a. 不使用O_NONBLOCK标志时,只读open要阻塞到某个其它进程为写而打开它为止

b. 不使用O_NONBLOCK标志时,只写open要阻塞到某个其它进程为读而打开它为止
c. 如果在open的时候指定O_NONBLOCK标志,当只读open时,没有进程为写而打开FIFO的话,会返回-1,只写open时,没有进程为读而打开FIFO的话也会返回-1表示失败。

以上的情况使FIFO的使用带来了一些不便,但有一个常用的技巧是,只要用O_RDWR(读写)来打开一个管道,则不会返回失败,而open也不会阻塞。两个进程使用FIFO来进行通信时,通常会使用两个FIFO,一个用于发送数据(即进行写操作),一个用于接收数据(即进行读操作),而这两个FIFO需要均使用O_RDWR来打开。使用系统调用open, read, write来操作这两个FIFO没有什么问题,程序工作得很好。


读数据端:

16 #include<sys/stat.h>
 17 #include <stdio.h>
 18 #include <iostream>
 19 #include <string.h>
 20 #include <unistd.h>
 21 #include <fcntl.h>
 22 #include <errno.h>
 23 using namespace std;
 24 
 25 #define FIFO_NAME "SPINFO_PIPE"
 26 
 27 int main(int argc, char *argv[])
 28 {
 29         int fifo_fd = 0;
 30         int num = 0;
 31         char buf[1024] = {0};
 32         if(access(FIFO_NAME, F_OK) == -1)
 33         {
 34                 fifo_fd = mkfifo(FIFO_NAME, 0777);
 35         }
 36         cout<<"Trying to opening named pipe for reading.\n";
 37 
 38         fifo_fd = open(FIFO_NAME, O_RDWR);
 39         cout<<"Opened named pipe for writing.\n";
 40 
 41         int i = 0;
 42         while(1)
 43         {
 44                 num = read(fifo_fd, buf, 10);
 45                 if(-1 == num)
 46                 {
 47                         cout<<"Error["<<errno<<"]when reading data into named pipe\n";
 48                 }
 49                 else// if(0 != num)
 50                 {
 51                         buf[num] = '\0';
 52                         cout<<"Readed"<<num<<"chars into named pipe:"<<buf<<endl;
 53                         buf[4] = '\0';
 54                         if(strcasecmp(buf,"exit")==0)
 55                         {
 56                                 break;
 57                         }
 58                 }
 59         }
 60         close(fifo_fd);
 61         return 0;
 62 }

写数据段:

16 #include<sys/stat.h>
 17 #include <stdio.h>
 18 #include <iostream>
 19 #include <string.h>
 20 #include <unistd.h>
 21 #include <fcntl.h>
 22 #include <errno.h>
 23 using namespace std;
 24 
 25 #define FIFO_NAME "SPINFO_PIPE"
 26 
 27 int main(int argc, char *argv[])
 28 {
 29         int fifo_fd = 0;
 30         int num = 0;
 31         char buf[1024] = {0};
 32         //char* buf = "1";
 33         if(access(FIFO_NAME, F_OK) == -1)
 34         {
 35                 cout<<"file is not exits.\n";
 36                 fifo_fd = mkfifo(FIFO_NAME, 0777);
 37         }
 38         cout<<"Trying to opening named pipe for writing.\n";
             fifo_fd = open(FIFO_NAME, O_RDWR);
60         cout<<"Opened named pipe for writing.\n";
 61 
 62         int i = 0;
 63         for(i = 0; i < 10; ++i)
 64         {
 65                 fgets(buf, 100, stdin);
 66                 num = strlen(buf);
 67                 cout<<"num = "<<num<<endl;
 68                 num=write(fifo_fd, buf, num);
 69                 if(-1 == num)
 70                 {
 71                         cout<<"error = "<<strerror(errno);
 72                         cout<<"Error[ "<<errno<<" ]when writing data into named pipe\n";
 73                 }
 74                 else
 75                 {
 76                         cout<<"Writed[ "<<num<<" ]chars into named pipe:"<<buf<<endl;
 77                 }
 78         }
 79         close(fifo_fd);
 80         return 0;
 81 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值