I am trying to use named pipe to communicated between process. It is not behaving as expected , it is reading the same message again and again .
process 1 : ( creates a pipe , and start reading till it reads over or it reads 100 message )
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;
process 2 (write to the same pipe)
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;
output of process 1( at times it is Hi1 and at times it is over
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 of both the process is blocking. Can someone shed some light about why prcoess 1 is reading some message again and again and again