INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

ksh Inter-process comm's

using TWO-WAY PIPE to allow child and parent processes to have control over each other. by mi294r3
Posted: 17 Dec 02

  This is an example of how to use a TWO-WAY PIPE |& to allow a parent process to set the values of variables in a child process and vice-versa (This was executed on AIX)

------------------------    
    Parent process wants to determine the values of the variables CHILD_NUMBER and CHILD_COLOR. These variables are
    set in the child process.  When the parent has both of these values it tells
    the child set the value of CHILD_NUMBER to the value of PAPA_NUMBER.


    the parent sends the request through the pipe print -p

------------------------

    the child reads the request from its stdin (which is the pipe), it then determines
    what type of request..

        For example
        the first request sent from the parent is formated as INT#CHILD_NUMBER.
 
        The child uses '#' as a field seperator.

        The first field INT tells the child to send back ONLY the
    
        value of the second field CHILD_NUMBER

        The child replys through stdout (which is the pipe) INFO#PURPLE.

        The parent then processes the reply simalar to the childs method by using the
            first and second field.

        What the values are of the first and second field and how are they are processed
        can be set to anything.


-------------------------------------------
PARENT SCRIPT
-------------------------------------------
#/bin/ksh                                                                      
PAPA_NUMBER=$RANDOM                                                            
  echo 'Initial Values From Parent Script'                                     
  echo 'PAPA_NUMBER='$PAPA_NUMBER                                              
  echo 'CHILD_NUMBER='${CHILD_NUMBER}                                          
  echo 'CHILD_COLOR='${CHILD_COLOR}                                            
                                                                               
#Start child process with two-way pipe                                         
     child |&                                                                  
  while [[ ${CHILD_NUMBER} != ${PAPA_NUMBER} || -z ${CHILD_COLOR} ]] do        
          if [[ -z ${CHILD_NUMBER} ]] then                                     
              QUESTION='CHILD_NUMBER'                                          
              echo '-----------------------------------'                       
              echo "Ask Child value of ${QUESTION}"                            
              print -p "INT#${QUESTION}"                                       
        elif [[ -z ${CHILD_COLOR} ]] then                                      
              QUESTION='CHILD_COLOR'                                           
              echo '-----------------------------------'                       
              echo "Ask Child to set Parent value of ${QUESTION}"              
              print -p "SET#${QUESTION}"                                       
        elif [[ ${CHILD_NUMBER} != ${PAPA_NUMBER} && -n ${CHILD_NUMBER} ]] then
              QUESTION='CHILD_NUMBER'                                          
              echo '-----------------------------------'                       
              echo "Tell Child to change its value of ${QUESTION}"
              echo 'PAPA_NUMBER='$PAPA_NUMBER                     
              echo 'CHILD_NUMBER='${CHILD_NUMBER}                 
              echo 'CHILD_COLOR='${CHILD_COLOR}                   
              print -p CMD#CHILD_NUMBER=${PAPA_NUMBER}            
              echo '-----------------------------------'          
              echo "Ask Child value of ${QUESTION}"               
              print -p "INT#${QUESTION}"                          
          fi                                                      
              read -p CHILD_MSG                                   
              REPLY_TYPE=$(echo $CHILD_MSG|cut -d# -f1)           
              CHILD_REPLY=$(echo $CHILD_MSG|cut -d# -f2)          
           case $REPLY_TYPE in                                    
               INFO*) eval "${QUESTION}"'='${CHILD_REPLY};;       
               CMD*)  eval ${CHILD_REPLY};;                       
           esac                                                   
              echo 'PAPA_NUMBER='$PAPA_NUMBER                     
              echo 'CHILD_NUMBER='${CHILD_NUMBER}                 
              echo 'CHILD_COLOR='${CHILD_COLOR}                   
  done                                                            
      print -p 'END'                                              
wait                                                              
  echo '-----------------------------------'                      
  echo "Final Values From Parent "       
  echo 'PAPA_NUMBER='$PAPA_NUMBER        
  echo 'CHILD_NUMBER='$CHILD_NUMBER      
  echo 'CHILD_COLOR='$CHILD_COLOR

-------------------------------------------
CHILD SCRIPT
-------------------------------------------        
#!/bin/ksh                                                       
  CHILD_COLOR='PURPLE'                                           
  CHILD_NUMBER=$RANDOM                                           
                                                                 
while [[ $PAPA_MSG != END ]]  do                                 
    read  PAPA_MSG                                               
    REPLY_TYPE=$(echo $PAPA_MSG|cut -d# -f1)                     
    PAPA_REQ=$(echo $PAPA_MSG|cut -d# -f2)                       
 case $REPLY_TYPE in                                             
     INT*)  echo 'INFO#'$(eval echo \$${PAPA_REQ});;             
     SET*)  echo 'CMD#'${PAPA_REQ}'='$(eval echo \$${PAPA_REQ});;
     CMD*)  eval ${PAPA_REQ};;                                   
 esac                                                            
done
                                                                               

        JRjr

Back to UNIX Scripting FAQ Index
Back to UNIX Scripting Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close