
#include <stdio.h>
#include <sys/time.h>
#include "mpi.h"

int main( int argc, char *argv[] )
{
    int rank, value, size;
    MPI_Status status;
    FILE *fp;

    MPI_Init( &argc, &argv );

    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_WORLD, &size );

    if (rank == 0)
	{
		fp = fopen("data.dat","r");
		if (fp == NULL)
		{
			printf("Cannot open file");
			exit(0);
		}
	}


    do
	{
    	if (rank == 0)
		{
			while (!feof(fp))
			{
				fscanf(fp, "%d", &value );
				MPI_Send( &value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD );
			}

			value = -1;
			MPI_Send( &value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD );
		 }
		 else
		 {
				MPI_Recv( &value, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD,&status );
				if ( rank<size-1)
					MPI_Send( &value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD );
				printf( "Process %d got %d\n", rank, value );
	      }
	 }while ( value>=0);


    MPI_Finalize( );
    return 0;
}
