How to terminate processes in a specific order?

43 Views Asked by At

I have three tasks. 1: get_input (P0) 2: clean_input (C0) 3: find_missing (P1)

First task is the parent process for the second one and third one is its own process. They should terminate in the following order: After P1 finds the missing alphabets and prints the result, P1 should terminate. When P1 terminates, C0 should delete the shared memory segment and terminate. P0 should terminate after C0 terminates.

I'm not sure how to implement this correctly since I'm not so used to python or multiprocessing.

This is what I have so far:

import os
import multiprocessing as mp
import multiprocessing.shared_memory
import string
import sys

# Function for Task 1 (Get_Input)
def get_input(pipe, queue):
    print(f"P0 ({os.getpid()}): Task 1 (Get_Input) started.")

    sys.stdin = open(0)
    test_string = input("Enter the test string: ")

    # Send test string to child process
    pipe.send(test_string)
    
    queue.put("Task 1 (Get_Input) completed.")
    print(f"P0 ({os.getpid()}): Task 1 (Get_Input) completed.")


# Function for Task 2 (Clean_Input)
def clean_input(pipe, shm, queue):
    print(f"C0 ({os.getpid()}): Task 2 (Clean_Input) started.")

    # Receive test string from parent process
    test_string = pipe.recv()

    # Remove numbers and special characters from the test string
    cleaned_string = ''.join(filter(str.isalpha, test_string))
    print(cleaned_string)

    # Write the cleaned string into the shared memory segment
    shm.buf[:len(cleaned_string)] = cleaned_string.encode('utf-8')

    queue.put("Task 2 (Clean_Input) completed.")
    print(f"C0 ({os.getpid()}): Task 2 (Clean_Input) completed.")


# Function for Task 3 (Find_Missing)
def find_missing(shm, queue):
    print(f"P1 ({os.getpid()}): Task 3 (Find_Missing) started.")
    queue.get()  # Wait for Task 2 to complete
    
    # Read the cleaned string from the shared memory
    cleaned_string = shm.buf.tobytes().decode('utf-8')

    # Find the missing alphabets
    missing_alphabets = [ch for ch in string.ascii_lowercase if ch not in cleaned_string]

    # Print the result
    if missing_alphabets:
        print(f"P1 ({os.getpid()}): Missing alphabets: {''.join(missing_alphabets)}")
    else:
        print(f"P1 ({os.getpid()}): No missing alphabets found.")
    
    print(f"P1 ({os.getpid()}): Task 3 (Find_Missing) completed.")

if __name__ == '__main__':
    # Create a pipe for communication between P0 and C0
    parent_pipe, child_pipe = mp.Pipe()

    # Create a shared memory segment
    shm = mp.shared_memory.SharedMemory(create=True, size=1024)

    # Create a queue to control the task execution order
    task_queue = mp.Queue()

    # Create P0 (Parent Process)
    p0 = mp.Process(target=get_input, args=(parent_pipe, task_queue))
    
    # Create C0 (Child Process)
    c0 = mp.Process(target=clean_input, args=(child_pipe, shm, task_queue))
    
    # Create P1 (Another Process)
    p1 = mp.Process(target=find_missing, args=(shm, task_queue))

    # Start the processes in the desired order
    p0.start()
    c0.start()
    p1.start()
    p1.join()
    c0.join()
    p0.join()


    # Delete the shared memory segment
    shm.close()
    shm.unlink()

0

There are 0 best solutions below