I am trying to send information back and forth between two processes using a fifo. It works up to a point, but then a read blocks. I suspect Process2 is where the bug is.
Process1:
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
{
char oprtr;
int fd1,fd0;
float oprnd1,oprnd2,result;
mkfifo("fifo1",0777);
fd1=open("fifo1",O_RDWR);
printf("fd1:%d\n",fd1);
printf("Add(+)\n");
printf("subtract(-)\n");
printf("multiply(*)\n");
printf("division(/)\n");
printf("Enter operator\n");
scanf("%c",&oprtr);
getchar();
write(fd1,&oprtr,sizeof(oprtr));
printf("Enter oprnd1\n");
scanf("%f",&oprnd1);
getchar();
write(fd1,&oprnd1,sizeof(oprnd1));
fd0=dup(fd1);
printf("Enter oprnd2\n");
scanf("%f",&oprnd2);
getchar();
if(write(fd0,&oprnd2,sizeof(oprnd2))==0)
perror("write : oprnd2:");
else
printf("writing oprnd2 done\n");
read(fd1,&result,sizeof(result));
printf("Result:%f\n",result);
}
Process2:
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
{
int fd2,fd3;
char oprtr;
float oprnd1,oprnd2,result;
fd2=open("fifo1",O_RDWR);
printf("fd2:%d\n",fd2);
read(fd2,&oprtr,sizeof(oprtr));
printf("oprtr:%c\n",oprtr);
read(fd2,&oprnd1,sizeof(oprnd1));
printf("oprnd1:%f\n",oprnd1);
fd3=dup(fd2);
This is where the read function is getting blocked
The above two read() calls appear to be working fine, but the following read() call is getting blocked. Why?
if(read(fd3,&oprnd2,sizeof(oprnd2))==0) ////This is the problem
perror("read : oprnd2:");
else
printf("oprnd2:%f\n",oprnd2);
switch(oprtr)
{
case '+':result=oprnd1+oprnd2;
write(fd2,&result,sizeof(result));break;
case '-':result=oprnd1-oprnd2;
write(fd2,&result,sizeof(result));break;
case '*':result=oprnd1*oprnd2;
write(fd2,&result,sizeof(result));break;
case '/':result=oprnd1/oprnd2;
write(fd2,&result,sizeof(result));break;
default: printf("Wrong Choice\n");break;
}
}
Terminal 1:
Add(+)
subtract(-)
multiply(*)
division(/)
Enter operator
+
Enter oprnd1
14.56
Enter oprnd2
16.44
writing oprnd2 done
Result:16.440089
Terminal 2:
fd2:3
oprtr:+
oprnd1:14.560000
Then it just gets blocked
The short and simple answer is that you shouldn’t use a single fifo for bi-directional communication without some external synchronization mechanism.
To elaborate: a Unix pipe, or fifo, is not best visualized as a plumbing-type pipe.
It’s more like a storage tank, with pipes leading to and from it. The standard usage is that one process writes, thus filling the tank, and another process reads, thus draining the tank. In your program, everything that gets written to the fifo sits in the tank until somebody comes along and reads it, first-come-first-served. And so your Process1 program reads the 16.44 value, that it wrote from
oprnd2, back intoresult. This leaves the tank empty, so there is nothing for Process2 to read. It comes down to a race condition. I suspect that, if you ran these two commands a few hundred times, they would work the way you want a few times.If you just want proof-of-concept, add a
sleepto Process1 before thereadfrom the fifo. A better solution would be to use two fifos, one for each direction. Or you could devise some way for Process2 to let Process1 know that it (Process2) has read the operator and both operands and has written the result –– e.g., a signal or a semaphore –– but that would probably be more trouble than it’s worth.On another matter, I strongly recommend against the use of
scanfin anything but a toy, demonstration, or throw-away prototype type of program. If the user hits just Enter, the program will just sit there forever. I recommend reading a line (being sure to address buffer-overflow) and then callsscanfon it.Oh, and also, please indent your code.