#include <stdio.h>
/*#include "/net/appl/mpich-1.1/src/include/mpi.h"*/
#include <mpi.h>

int numprocs,myid;




void master()
{

       
	MPI_Status status;
        int buck;
        buck=1;
	
	

        printf("nuprocs is %d \n",numprocs);
	printf("b4 send %d",buck);
        MPI_Send(&buck,1,MPI_INT,1,0,MPI_COMM_WORLD);
        printf("b4 recv %d",buck);
        MPI_Recv(&buck,1,MPI_INT,(numprocs-1),0,MPI_COMM_WORLD,&status);

        printf("The number is %d \n",buck);

}




void slave()
{


      
	int buck;
        MPI_Status status;
	
	


        printf("In slave %d b4 recv \n",myid);
	MPI_Recv(&buck,1,MPI_INT,(myid-1),0,MPI_COMM_WORLD,&status);

        buck=buck+1;

        printf("In slave %d b4 send \n",myid);
	
	if (myid >= (numprocs-1))
	{
        
                MPI_Send(&buck,1,MPI_INT,0,0,MPI_COMM_WORLD);
	}


        else
	{
                MPI_Send(&buck,1,MPI_INT,(myid+1),0,MPI_COMM_WORLD);
	}
		
	printf("In slave %d after send \n",myid);

}







int main(int argc,char *argv[])
{

       

        MPI_Init(&argc,&argv);
	MPI_Comm_rank(MPI_COMM_WORLD,&myid);
        MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
        


        if (myid == 0)
        
                master();
        else
        
                slave();
                
        MPI_Finalize();



}



















