
      PROGRAM simple_deadlock_avoided

      INCLUDE 'mpif.h'
      INTEGER myrank, ierr, status(MPI_STATUS_SIZE)
      INTEGER request
      REAL a(100), b(100)

C     Initialize MPI:

      call MPI_INIT(ierr)

C     Get my rank:

      call MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)

C     Process 0 posts a receive, then sends; same for process 1   

      if( myrank.eq.0 )then
c
         write(*,*)myrank,' posting a receive...'
         call MPI_IRECV( b, 100, MPI_REAL, 1, 19, MPI_COMM_WORLD,
     $        request, ierr )
         write(*,*)myrank,' posted receive ',request
         write(*,*)myrank,' sending data to 1'
         call MPI_SEND( a, 100, MPI_REAL, 1, 17, MPI_COMM_WORLD, ierr)
         write(*,*)myrank,' waiting for completion of ',request
         call MPI_WAIT( request, status, ierr )
         write(*,*)myrank,' done.'
c
      else if ( myrank.eq.1 )then
c
         write(*,*)myrank,' posting a receive...'
         call MPI_IRECV( b, 100, MPI_REAL, 0, 17, MPI_COMM_WORLD,
     $        request, ierr )
         write(*,*)myrank,' posted receive ',request
         write(*,*)myrank,' sending data to 1'
         call MPI_SEND( a, 100, MPI_REAL, 0, 19, MPI_COMM_WORLD, ierr)
         write(*,*)myrank,' waiting for completion of ',request
         call MPI_WAIT( request, status, ierr )
         write(*,*)myrank,' done.'
c
      endif


C     Terminate MPI:

      call MPI_FINALIZE(ierr)

      END
