Program hangs when using processes
<p>I'm trying to communicate my c code to my python code.
I'm making a name pipe in my c code, sending it to python and python prints it out. I want to do the same where I make a named pipe in python and c reads it out, however my program seems to stall (I want to do both of these at once), hence I'm using processes. </p>

<p>My c code:</p>

<pre><code>int main(void) {
    int pid;
    FILE * fp;
    char *calledPython="./a.py"; 
    char *pythonArgs[]={"python",calledPython,"a","b","c",NULL};
    FILE * fp2;
    char str[40];
    pid = fork();

    if(pid == 0) {
        mkfifo("./test",0666);
        fp = fopen("./test","w");
        fprintf(fp,"Hello\n");
        fprintf(fp,"World\n");
    }
    else {
        execvp("python",pythonArgs);
        // if we get here it misfired
        perror("Python execution");
        kill(pid,SIGKILL);
    }

    fp2 = fopen("./test2","r");
    if(fp2 == NULL) {
        printf("NUll\n");
    }

    else {
        fscanf(fp2, "%s", str);
        printf("received from test2 %s\n", str);
   }
   fclose(fp);
    return 0;
}
</code></pre>

<p>My python code:</p>

<pre><code>#!/usr/bin/python


import os, sys

with open("./test") as fp:
    for line in fp:
        print line


path = "./test2"
os.mkfifo(path)

fifo = open(path,"w")
fifo.write("Message from the sender\n")
fifo.close()
</code></pre>

<p>Where ./test is the initial named pipe, python reads in and ./test2 is the second named pipe c is soposed to read in.
However, my program hangs once I write to my second pipe in python:</p>

<pre><code>path = "./test2"
os.mkfifo(path)

fifo = open(path,"w")
fifo.write("Message from the sender\n")
fifo.close()
</code></pre>

<p>The output I get from terminal is:</p>

<pre><code>NUll
Hello

World
</code></pre>

<p>if I run it again, I simply get a empty line (Nothing prints at all).
I tried to open the second named pipe in the parent process, that doesn't seem to change much. I'm not sure where I've gone wrong here, any ideas?</p>

<p>I don't have a unix box on hand to test currently. But the likely problem is that you don't close the fifo in the parent process.</p>

<p>Whenever you have a loop like <code>for line in file</code>, that loop will continue until the file ends. Unlike a normal file, a fifo doesn't "end" until the process on the writing end closes the fifo. If you don't close the fifo, the reading process will block in the read call and won't be able to progress.</p>

<p>There may be other problems with your code that I'm missing.</p>

<p>Some other notes:</p>

<ul>
<li><p>You should mkfifo before you execvp, currently you are doing them in parallel and hoping that mkfifo completes before your python program tries to open the file. Likely, but not certain.</p></li>
<li><p>There is another race condition on the second fifo, this one I think is very unlikely to work as intended.</p></li>
<li><p>You never remove the named pipe, the next time it will try to create  a file which already exists which will not work.</p></li>
<li><p>SIGKILL seldom the correct signal to use. I recommend you read up on the difference between SIGKILL, SIGTERM etc.</p></li>
<li><p>System calls like mkfifo can fail and checking the return value for errors is recommended.</p></li>
</ul>

python c fork