Problem using multiple pipes in a multiprocess program in python

297 Views Asked by At

I'm currently working on a project for university in which have to generate numbers and transfer them to other processes using Pipes in Python. I'm pretty new to Python and especially to interprocess comunication, so I don't know where my problem is exactly.

import os
import random


r, w = os.pipe()
r2, w2 = os.pipe()
r3, w3 = os.pipe()


pid = os.fork()

#Parentsprocess
if pid > 0:
    
    os.close(r)
    os.close(r2)
    
    random_num = []
    n = random.sample(range(1, 1000), 5)
    random_num.append(n)
    print(random_num)
    
    zahlenstring = "".join(str(x) for x in n)
    
    zahlen = bytes(zahlenstring,"utf-8" )
    print("bytes:", zahlen)
    print("Parent process writes:")
    
    os.write(w,zahlen)
    os.write(w2,zahlen)
    print("Written text:", zahlen.decode())


#Childprocess
else:
    os.close(w)
    print("\nChild process reads:")
    r = os.fdopen(r)
    zahlen = r.read()
    print("Zahlen:", zahlen)       
    file = open("file.txt", "w")            
    file.write(zahlen)                      

    
    pid2 = os.fork()
    #Parentsprocess
    if pid2 > 0:
        os.close(w2)
        r2 = os.fdopen(r2)
        verarbeiten = r2.read()
        print("Partents number 2")
        print(int(verarbeiten)-1000)
        os.close(r3)
        os.write(verarbeiten)



    #Childprocess
    else:
        os.close(w3)
        print("Child number 2")
        os.fdopen(r3)
        print(os.read(r3))

First I know my code is messy and definitely not perfect, because I'm pretty new to Python. So I'm sorry for that.

Now to my problem(s):

1.Normally a file would be created and the numbers from "zahlen" written into it. It also worked fine until I added the third pipe and wrote into it in the second parentsprocess, so the child can read from it. When I remove the third pipe and the code for it after the second fork, it also writes into the file again.

2.for some reason the number is written twice in a row into the file, although the code is only executed once and the number is also written and read individually in the pipe.

Could someone help me please.

1

There are 1 best solutions below

0
VPfB On

The program runs with errors, but I can explain why is the number written twice

The number is written to a buffer (file.write). The file remains open (forgotten close?) and then a fork happens. It creates a duplicate process (just the PID is different). The result are two copies of the buffer, one in child, one in parent. When those two processes exit, the automatic flush and close writes the buffer to the file two times.

To fix it, close the file, or at least flush the buffers before forking. In general, pay attention what child processes inherit.