Search code examples
named-pipesfifo

What happenes if i open a FIFO for read and write in the same program?


I started to learn pipes and FIFO and i don t understand exactly what happens in the background in this situation.Why nothing is printed in the console? No other process opens the "abc" FIFO

`int r,w,n=7;
r=open("abc",O_RDONLY);
n--;
w=open("abc",O_WRONLY);
n--;
printf("%d",n);`

Solution

  • As https://stackoverflow.com/a/23435538/139985 explains, when you open a FIFO, the open call will block until the system gets a corresponding open on the the other side of same FIFO.

    In your example, you have a single-threaded C program that attempts to open both sides one after the other. That won't work. The program will just lock up.

    If you redesign your program to use two threads and open the read and write ends of the FIFO in different threads, that should work. One thread will block in open until the other thread calls open.

    However, using a FIFO to communicate between two threads in the same application is (to say the least) inefficient.