0

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!

Crownen
  • 13
  • 3
  • Is there any reason why you try to set this up manually. Using `subprocess` and connecting the processes via `stdin`/`stdout`, or `multiprocessing` with `Queue` or `Pipe` is a standard task. – MisterMiyagi Jun 12 '22 at 11:20
  • @MisterMiyagi It is a task I have to do and I'm only allowed to use system calls or standard-library functions **and** I have to use Pipes. So I'm not quite sure if either `subprocess` or `multiprocessing` would fulfill this. – Crownen Jun 12 '22 at 11:45
  • Both are part of the standard library and both use pipes. Is this an exercise, i.e. do you have to satisfy some *formal* requirements? Can you perhaps provide the *exact* requirements? – MisterMiyagi Jun 12 '22 at 12:31
  • @MisterMiyagi Alright, so the program should consist out of parallel running endless processes (A,B,C,D) which have to communicate with pipes. They should only close with the Signal handler SIGINT. All parallel running processes should also be seen with the commands top, ps and pstree. Process A first has to generate and write a number before B and C can read it and also C has to write into the pipe before D reads it. This should repeat, so there are multiple numbers generated in A, transfered through the pipes and saved in the other processes (Array and file) – Crownen Jun 12 '22 at 12:43

0 Answers0