program driver

  implicit none
  include 'mpif.h'
  
  integer ierr, pid,nprocs,intercomm,tag
  integer errc(4)
!  character*1  args(2)
  logical probe_result
  character(len=2) :: args(2)


  call MPI_INIT(ierr)
  call MPI_COMM_RANK( MPI_COMM_WORLD, pid, ierr )
  call MPI_COMM_SIZE( MPI_COMM_WORLD, nprocs, ierr )

  print *,"Starting, task",pid

  write (args(1), "(I2)") pid
  args(2) = ""
  tag = 21

  print *,"ARGS(1):",args(1)

!  call sleep(2)  
!  print *,"driver: task:",pid,"INTERCOMM:",intercomm
  call MPI_BARRIER(MPI_COMM_WORLD,ierr)
  call MPI_COMM_SPAWN('./hello.x', args,2,MPI_INFO_NULL,0, &
        MPI_COMM_SELF,intercomm,MPI_ERRCODES_IGNORE, ierr) 

  print *,"driver: task:",pid,"INTERCOMM:",intercomm
!  call MPI_COMM_DISCONNECT(intercomm, ierr)
  
  call  MPI_IPROBE(0, tag, intercomm, probe_result, MPI_STATUS_IGNORE, ierr)
  do while ( probe_result .eqv. .false.)
     print *," child not ready, task:",pid
     call sleep(2)
     call  MPI_IPROBE(0, tag, intercomm, probe_result, MPI_STATUS_IGNORE, ierr)
  enddo

  print *,"driver: probe successfull, task:",pid
  call MPI_COMM_DISCONNECT(intercomm, ierr)
!  call sleep(270)  
  call MPI_FINALIZE(ierr)
  
  stop
end program driver

