       character*31 function get_queue(pid)

       IMPLICIT NONE

       INCLUDE		'($QUIDEF)'

       INTEGER*4	SYS$GETQUIW
       INTEGER*4	STATUS_Q
       INTEGER*4	STATUS_J
 
       STRUCTURE	/ITMLST/
          UNION
             MAP
                INTEGER*2	BUFLEN, ITMCOD
                INTEGER*4 BUFADR, RETADR
             END MAP
             MAP
                INTEGER*4	END_LIST
             END MAP
          END UNION
       END STRUCTURE

       STRUCTURE	/IOSBLK/
          INTEGER*4	STS,ZEROED
       END STRUCTURE

       RECORD /ITMLST/	QUEUE_LIST(4)
       RECORD /ITMLST/ JOB_LIST(3)
       RECORD /IOSBLK/	IOSB
	
C      Declare variables used in $GETQUIW item lists
       CHARACTER*31	SEARCH_NAME
       CHARACTER*31	QUEUE_NAME
       INTEGER*2	SEARCH_NAME_LEN
       INTEGER*2        QUEUE_NAME_LEN
       INTEGER*4	SEARCH_FLAGS
       integer*4	pid,job_pid

C      Solicit queue name to search; it may be a wildcard name
       SEARCH_NAME='*'
       SEARCH_NAME_LEN=1
C      Initialize item list for the display operation
       QUEUE_LIST(1).BUFLEN = SEARCH_NAME_LEN
       QUEUE_LIST(1).ITMCOD = QUI$_SEARCH_NAME
       QUEUE_LIST(1).BUFADR = %LOC(SEARCH_NAME)
       QUEUE_LIST(1).RETADR = 0
       QUEUE_LIST(2).BUFLEN = 4
       QUEUE_LIST(2).ITMCOD = QUI$_SEARCH_FLAGS
       QUEUE_LIST(2).BUFADR = %LOC(SEARCH_FLAGS)
       QUEUE_LIST(2).RETADR = 0
       QUEUE_LIST(3).BUFLEN = 31
       QUEUE_LIST(3).ITMCOD = QUI$_QUEUE_NAME
       QUEUE_LIST(3).BUFADR = %LOC(QUEUE_NAME)
       QUEUE_LIST(3).RETADR = %LOC(QUEUE_NAME_LEN)
       QUEUE_LIST(4).END_LIST = 0

C      Initialize item list for the display job operation
       JOB_LIST(1).BUFLEN = 4
       JOB_LIST(1).ITMCOD = QUI$_SEARCH_FLAGS
       JOB_LIST(1).BUFADR = %LOC(SEARCH_FLAGS)
       JOB_LIST(1).RETADR = 0
       JOB_LIST(2).BUFLEN = 4
       JOB_LIST(2).ITMCOD = QUI$_JOB_PID
       JOB_LIST(2).BUFADR = %LOC(JOB_PID)
       JOB_LIST(2).RETADR = 0
       JOB_LIST(3).END_LIST= 0

C      Request search of all jobs present in output queues; also force
C      wildcard mode to maintain the internal search context block after
C      the first call when a non-wild queue name is entered--this preserves
C      queue context for the subsequent display job operation
      SEARCH_FLAGS = (QUI$M_SEARCH_WILDCARD .OR.
     1                QUI$M_SEARCH_executing_jobs .OR.
     2                QUI$M_SEARCH_ALL_JOBS. or.
     3		      qui$M_SEARCH_BATCH)

C     Dissolve any internal search context block for the process
      STATUS_Q = SYS$GETQUIW (,%VAL(QUI$_CANCEL_OPERATION),,,,,)
      IF (.NOT. STATUS_Q) CALL EXIT(STATUS_Q)

C     Locate next output queue; loop until an error status is returned
      DO WHILE (STATUS_Q)
         STATUS_Q = SYS$GETQUIW(,
     1                           %VAL(QUI$_DISPLAY_QUEUE),,
     2                           QUEUE_LIST,
     3                           IOSB,,)
         IF (.NOT. STATUS_Q) CALL EXIT(STATUS_Q)
         IF (STATUS_Q) STATUS_Q = IOSB.STS
         STATUS_J = 1

C     Get information on next job in queue; loop until error return
         DO WHILE (STATUS_Q .AND. STATUS_J)
            STATUS_J = SYS$GETQUIW (,
     1                               %VAL(QUI$_DISPLAY_JOB),,
     2                               JOB_LIST,
     3                               IOSB,,)
            IF (.NOT. STATUS_J) CALL EXIT(STATUS_J)
            IF (STATUS_J) STATUS_J = IOSB.STS
            IF (STATUS_J.and.(job_pid.eq.pid)) THEN

               get_queue = QUEUE_NAME(1:INDEX(QUEUE_NAME,CHAR(0))-1)
	       return
            ENDIF

         ENDDO

      ENDDO
      get_queue=' '	
      return
      END
