I want to generate four endless processes with fork which run simultaneously until I stop the program.
All four processes should communicate with os.pipe()
, where process A continuously generate a random number and writes it into a pipe for process B (B writes the numbers into a file) and C to read from it. Process C then saves all the generated files in an Array and writes the sum of all of them into another pipe for process D which reads from the pipe and prints it out.
The processes should be generated with os.fork
and run until stopped with a Sigint
(no problems with the Sigint)
My main problem is generating the four endless processes and let them communicate with pipes. To be precise:
1)I'm not sure how to generate the four processes and let them run endlessly. Especially to let them write the new generated numbers into the pipe again, so the other running processes can read the ne numbers and save them (in the array)
2)When I tried to read from a Pipe and write into another pipe in a process, the program stopped before it. Here is the part I mean ( Pipes were generated with r2,w2 and r3,w3 before and also some pipe with r,w were used before but this worked fine):
if pid2 > 0:
os.close(w2)
r2 = os.fdopen(r2)
verarbeiten = r2.read()
print(int(verarbeiten)-1000)
os.close(r3)
os.write(w3, verarbeiten)
else:
os.close(w3)
os.fdopen(r3)
text = os.read(r3)
print(text)
I really hope somebody can help me please because I stuck here. Thanks in advance!