! ----------------------------------------------------------------- !
      subroutine x_cque_c(msgtype,ipsz,nproc,tids)
! ----------------------------------------------------------------- !
! Purpose :  This subroutine acts as a shell containing message 
!            passing code to communicated with the subroutines 
!            found in the spawned slaves
! Glossary:
! ipsz       -- Total number of evaluations
! snd_c      -- Number of real numbers sent to ea. child process 
! rcv_c      -- Number of real numbers received from ea. child process 
! paraminlin -- Array storing data to be sent to ea. child process
! fitarrya   -- Array storing data returned from ea. child process
! ----------------------------------------------------------------- !
      use x_gdat

      implicit none
      include 'fpvm3.h'
      
      integer :: i,j,k,kkk
      integer :: istat
      integer :: job, jobindex
      integer :: ipsz
      integer :: info
      integer, dimension(100)  ::  jobline
      integer :: msgtype
      integer :: nproc 
      integer, dimension(0:32) :: tids
      integer :: who

      istat = 1
      jobline(:) = -1
      jobindex = 0

! ---- Send a job to each slave process

      do i=0,(nproc-1)
         jobindex=jobindex+1
         j=snd_c*jobindex-(snd_c-1)
         call pvmfinitsend( PVMDEFAULT, info )
         call pvmfpack( INTEGER4, istat, 1, 1, info )
         call pvmfpack( INTEGER4, snd_c, 1, 1, info )
         call pvmfpack( REAL8, r8_frm_p(j), snd_c, 1, info )
         call pvmfsend( tids(i), msgtype, info )
         jobline(jobindex)=i
      enddo

      if(jobindex.eq.ipsz) goto 20   !#jobs = # slaves

! ---- Retrieve results and submit new job until queue is empty 

      do while(jobindex .lt. ipsz)
         call pvmfrecv( -1, msgtype, info )
         call pvmfunpack( INTEGER4, who, 1, 1, info )
         do k=1,ipsz+1
            if(jobline(k).eq.who)then
               job=k
               jobline(job)=-2 ! -2-> complete 
               goto 10
            endif
            if(k.eq.ipsz+1) then
               print*,'ERROR TRAP1: x_cdat_c k=',k 
               stop
            endif 
         enddo
10       continue
         call pvmfunpack( INTEGER4, rcv_c, 1, 1, info )
         j=rcv_c*job-(rcv_c-1)
         call pvmfunpack( REAL8, r8_frm_c(j), rcv_c, 1, info )

         print*,'rcv_c',rcv_c,' who=', who
         do kkk=0,rcv_c-1
             print*,'r8_frm_c(',j+kkk,')=',r8_frm_c(j+kkk)
         enddo
         print*,' '

         jobindex = jobindex +1
         j=snd_c*jobindex -(snd_c-1)
         call pvmfinitsend( PVMDEFAULT, info )
         call pvmfpack( INTEGER4, istat, 1, 1, info )
         call pvmfpack( INTEGER4, snd_c, 1, 1, info )
         call pvmfpack( REAL8, r8_frm_p(j), snd_c, 1, info )
         call pvmfsend( tids(who), msgtype, info ) 
         jobline(jobindex)=who
      enddo

20    continue

! ----- Retrieve all remaining results from slaves

      do i=0,(nproc-1)
         call pvmfrecv( -1, msgtype, info )
         call pvmfunpack( INTEGER4, who, 1, 1, info )
         do k=1,ipsz+1
            if(jobline(k).eq.who)then
               job=k
               jobline(job)=-2 ! -2-> complete
               goto 30
            endif
            if(k.eq.ipsz+1) then
               print*,'ERROR TRAP2: x_cdat_c who=',who
               stop
            endif
         enddo
30       continue
         call pvmfunpack( INTEGER4, rcv_c, 1, 1, info )
         j=rcv_c*job-(rcv_c-1)
         call pvmfunpack( REAL8, r8_frm_c(j), rcv_c, 1, info )

         print*,'rcv_c',rcv_c,' who=', who
         do kkk=0,rcv_c-1
            print*,'r8_frm_c(',j+kkk,')=',r8_frm_c(j+kkk)
         enddo
         print*,' '

      enddo

      end
