#include <stdio.h>
#include "mpi.h"

main(int argc, char* argv[]) {
  int x, y, np, me;
  int tag = 42;
  MPI_Status  status;

  MPI_Init(&argc, &argv);               /* Initialize MPI */
  MPI_Comm_size(MPI_COMM_WORLD, &np);   /* Get number of processes */
  MPI_Comm_rank(MPI_COMM_WORLD, &me);   /* Get own identifier */
  
  x = me;
  if (me == 0) {    /* Process 0 does this */
    
    printf("Sending to process 1\n");
    MPI_Ssend(&x, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);  /* Synchronous send */
    printf("Receiving from process 1\n");
    MPI_Recv (&y, 1, MPI_INT, 1, tag, MPI_COMM_WORLD, &status);
    printf("Process %d received a message containing value %d\n", me, y);
    
  } else {         /* Process 1 does this */

    /* Since we use synchronous send, we have to do the receive-operation */
    /* first, otherwise we will get a deadlock */
    MPI_Recv (&y, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
    MPI_Ssend (&x, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);  /* Synchronous send */

  }

  MPI_Finalize();
}

