Reading and Wrtiting Fifo Files between C and Python
<p>In general, I am creating two fifo queues to be read and written to by my .c and .py programs. To enable my .c program to interact with python, I have included the <code>&lt;python2.7/Python.h&gt;</code> library. </p>

<p>Firstly, my .c program creates a file called CFifo and writes text to it using fprintf. No problem there.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;python2.7/Python.h&gt;


int main() {
    FILE *CFifo, *pythonFifo, *pythonFile;
    char buffer[1024];

    // declare python
    Py_SetProgramName("writer.py");

    // init python
    Py_Initialize();

    // open python
    pythonFile = fopen("writer.py", "r");

    // C writes to file, python reads from this file
    CFifo = fopen("./CFifo", "w");  

    // Print strings to file using fprintf
    fprintf(CFifo, "This is a test");

    // close file python reads
    fclose(CFifo);
</code></pre>

<p>The second part of my C program should read the information written (in the second fifo queue) by my python program but instead it just hangs in the terminal when opening <code>./pythonFifo</code>.</p>

<pre><code>    // make second fifo queue
    // python writes to this file, C reads from it
    mkfifo("./pythonFifo", 0777);

    // run python program
    PyRun_SimpleFile(pythonFile, "writer.py");
    pythonFifo = fopen("./pythonFifo", "r");

    while(fgets (buffer, sizeof(buffer), pythonFifo)) {
        printf("%s", buffer);
    }

    // close python
    Py_Finalize();
    fclose(pythonFifo);
    remove("./pythonFifo");

    return 0;
}
</code></pre>

<p>This is the python section responsible for writing to the fifo queue.</p>

<pre><code># open fifo file that python writes to
filename_write = "./pythonFifo"
pipe = os.open(filename_write, os.O_WRONLY)
for output in finalStringList:
   os.write(pipe, output)
os.close(pipe)
</code></pre>

<p>The purpose of the second file is to write modified information read from the first file.</p>

<p>You can't get there from here. From the <code>mkfifo</code> man page...</p>

<blockquote>
  <p>Once  you have created a FIFO special file in this way, any process
  can open it for reading or writing, in the same way as  an  ordinary 
  file. However,  it  has to be open at both ends simultaneously before
  you can proceed to do any input or output operations on it.  Opening a
  FIFO for reading  normally  blocks  until some other process opens the
  same FIFO for writing, and vice versa.</p>
</blockquote>

<p>Both sides need to open the file before they can continue. But since <code>PyRun_SimpleFile</code> runs a python script sychronously, the latter C code that opens the file is never reached. If you try to open it in the C code first, it would hang before running the python code. You have a classic deadlock.</p>

<p>I added several prints to your sample and was able to see the program advance as I did <code>cat pythonFifo</code> and <code>echo foo &gt; pythonFifo</code> in a separate console. It returned garbage of course, but proved the problem.</p>

<p>Actually, you can get there from here (again, the man page)</p>

<blockquote>
  <p>See fifo(7) for nonblocking  handling  of FIFO special files.</p>
</blockquote>

<p>but you open yourself to more deadlocking if your python code writes more than fits in the pipe. You may be better off having your python program write to some variable and have your C code read it from there.</p>

python c file-io fifo