      program batonpass
      implicit none
      include 'mpif.h'
      integer i, tag
      integer baton
      integer myProc, nProc
      integer ierror(2), istatus(2)

      call MPI_Init(ierror)
      call MPI_Comm_Rank(MPI_COMM_WORLD, myProc, ierror)
      call MPI_Comm_Size(MPI_COMM_WORLD, nProc, ierror)

      baton = 0
      do i = 0, nProc-2
        tag = i
        if ( myProc .eq. i ) then
          write(6,10) myProc, baton
          baton = baton + 1
          call MPI_Send(baton, 1, MPI_INTEGER, i+1, tag,  
     +      MPI_COMM_WORLD, ierror)
        elseif (myProc .eq. i+1) then
          call MPI_Recv(baton, 1, MPI_INTEGER, i, tag, 
     +      MPI_COMM_WORLD, istatus, ierror)
        endif
      enddo

      if ( myProc .eq. nProc-1 ) then
        write(6,10) myProc, baton
10      format('hello from process ',I5,' with baton value ',I5)
      endif  

      call MPI_Finalize(ierror)
      end

