我正在尝试使用命名管道在进程之间进行通信。它没有按预期运行,它一次又一次地读取相同的消息。
进程1 :(创建一个管道,开始读取直到它读完或读取100条消息)
char* myfifo = "/tmp/omgfifo";
if ( feature_head == NULL ) {
vty_out(vty,"%s%s", ERR_STR ,VTY_NEWLINE);
return CMD_WARNING ;
}
vtysh_diag_list_features(feature_head,vty);
/* Create UDS connection for ovs-appctl. */
rc = mkfifo(myfifo,0777);
if(rc == -1)
{
vty_out(vty,"mkfifo errorno : %d %s",errno,VTY_NEWLINE);
}
fd = open(myfifo, O_RDONLY);
if(fcntl(fd, F_GETFL ) & O_NONBLOCK)
{
vty_out(vty,"non block is enabled %s",VTY_NEWLINE);
}
if(fd == -1)
{
vty_out(vty,"fd errorno : %d %s",errno,VTY_NEWLINE);
}
else
{
while(flag)
{
retval ++;
buf[0] = "\0"
rc = read(fd,buf,MAX_BUF);
if(retval > 100)
{
flag = 0;
break;
}
if(rc == -1)
{
flag = 0;
vty_out(vty,"read errorno : %d %s",errno,VTY_NEWLINE);
}
else
{
if(strlen(buf) > 3 && strcmp(buf,"over"))
{
vty_out(vty,"gone case %s",VTY_NEWLINE);
flag = 0;
}
vty_out(vty,"%3d:%s %s",retval,buf,VTY_NEWLINE);
}
}
close(fd);
}
unlink(myfifo);
vty_out(vty,"SIGN : done");
return CMD_SUCCESS;
进程2(写入同一个管道)
int fd;
char * myfifo = "/tmp/omgfifo";
fd = open(myfifo, O_WRONLY);
if(fd == -1)
{
vty_out(vty,"fd errorno : %d %s",errno,VTY_NEWLINE);
}
else
{
if(fcntl(fd, F_GETFL ) & O_NONBLOCK)
{
vty_out(vty,"non block is enabled %s",VTY_NEWLINE);
}
if(write(fd, "Hi", sizeof("Hi"))== -1)
{
vty_out(vty,"write h errorno : %d %s",errno,VTY_NEWLINE);
}
if(write(fd, "Hi1", sizeof("Hi1"))== -1)
{
vty_out(vty,"write h1 errorno : %d %s",errno,VTY_NEWLINE);
}
if(write(fd, "over", sizeof("over")) == -1)
{
vty_out(vty,"write o errorno : %d %s",errno,VTY_NEWLINE);
}
if(write(fd, "Hi2", sizeof("Hi2")) == -1)
{
vty_out(vty,"write h2 errorno : %d %s",errno,VTY_NEWLINE);
}
if(write(fd, "Hi3", sizeof("Hi3")) == -1)
{
vty_out(vty,"write h3 errorno : %d %s",errno,VTY_NEWLINE);
}
if(close(fd)!=0)
{
vty_out(vty,"close errorno : %d %s",errno,VTY_NEWLINE);
}
}
return 0;
过程1的输出(有时它是Hi1,有时它已经结束了
1:Hi
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
SIGN : done
这两个进程的fd都是阻塞的。 有人可以解释为什么prcoess 1会一次又一次地阅读某些消息
答案 0 :(得分:2)
当你关闭写入部分的fd时,read接收到一个文件结尾并返回0(并且缓冲区没有改变,因此它保持包含" Hi")。你至少应该检查这个条件(通常你应该使用read' s返回值作为实际读取的字节数)。 通常,不能保证读写之间存在完美匹配。 "写"只是将字符排入队列,然后阅读"最多接收您传递的数字作为第三个参数(MAX_BUF)。所以发生的事情是你在一次读取操作中收到整个字符集,然后在进一步读取时得到EOF。您可以拆分缓冲区并查找部件。