20.16.1 Each process opens the FIFO pathnames

One way is to generate unique file pathnames for 2 named pipes and, somehow, make them known to 2 processes; then have them open the named pipes to exchange characters. Let’s say a parent process executes a child process and we use environment variables to make the pathnames known to the child.

The parent can look like this:

mbfl_file_enable_remove
mbfl_process_enable

declare -r CHILD=/path/to/child.bash

# Export these variables so the child sees them.
declare -rx FIFO_PARENT_TO_CHILD=/path/to/fifo-parent-to-child.$RANDOM
declare -rx FIFO_CHILD_TO_PARENT=/path/to/fifo-child-to-parent.$RANDOM

declare -ri FD_PARENT_TO_CHILD=4
declare -ri FD_CHILD_TO_PARENT=5

mbfl_location_enter
{
    if mbfl_exec_mkfifo --mode=0600 -- "$FIFO_PARENT_TO_CHILD"
    then mbfl_location_handler "mbfl_file_remove '$FIFO_PARENT_TO_CHILD'"
    else
        mbfl_location_leave
        exit_failure
    fi

    if mbfl_exec_mkfifo --mode=0600 -- "$FIFO_CHILD_TO_PARENT"
    then mbfl_location_handler "mbfl_file_remove '$FIFO_CHILD_TO_PARENT'"
    else
        mbfl_location_leave
        exit_failure
    fi

    if mbfl_fd_open_input_output $FD_PARENT_TO_CHILD "$FIFO_PARENT_TO_CHILD"
    then mbfl_location_handler "mbfl_fd_close $FD_PARENT_TO_CHILD"
    else
        mbfl_location_leave
        exit_failure
    fi

    if mbfl_fd_open_input_output $FD_CHILD_TO_PARENT "$FIFO_CHILD_TO_PARENT"
    then mbfl_location_handler "mbfl_fd_close $FD_CHILD_TO_PARENT"
    else
        mbfl_location_leave
        exit_failure
    fi

    if mbfl_program_execbg 0 1 "$mbfl_PROGRAM_BASH" "$FIFO_SCRIPT"
    then mbfl_location_handler "mbfl_process_wait $mbfl_program_BGPID"
    else
        mbfl_location_leave
        exit_failure
    fi

    printf 'ciao child\n' >&$FD_PARENT_TO_CHILD
    read -u $FD_CHILD_TO_PARENT
}
mbfl_location_leave

The child can look like this:

declare -r FIFO_PARENT_TO_CHILD=$FIFO_PARENT_TO_CHILD
declare -r FIFO_CHILD_TO_PARENT=$FIFO_CHILD_TO_PARENT

declare -ri FD_PARENT_TO_CHILD=4
declare -ri FD_CHILD_TO_PARENT=5

mbfl_location_enter
{
    if mbfl_fd_open_input_output $FD_PARENT_TO_CHILD "$FIFO_PARENT_TO_CHILD"
    then mbfl_location_handler "mbfl_fd_close $FD_PARENT_TO_CHILD"
    else
        mbfl_location_leave
        exit_failure
    fi

    if mbfl_fd_open_input_output $FD_CHILD_TO_PARENT "$FIFO_CHILD_TO_PARENT"
    then mbfl_location_handler "mbfl_fd_close $FD_CHILD_TO_PARENT"
    else
        mbfl_location_leave
        exit_failure
    fi

    read -u $FD_PARENT_TO_CHILD
    printf 'ciao parent\n' >&$FD_CHILD_TO_PARENT
}
mbfl_location_leave

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.