Write a MPI program to demonstration of deadlock using point to point communication
and avoidance of deadlock by altering the call sequence
STEP1: Open terminal
Type
nano deadlock.c
type code as in below
// deadlock.c
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank, size;
int send_data, recv_data;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
send_data = 100;
// blocking send&recieved
MPI_Send(&send_data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&recv_data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 0 received %d\n", recv_data);
} else if (rank == 1) {
send_data = 200;
MPI_Send(&send_data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); // blocking send
MPI_Recv(&recv_data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 1 received %d\n", recv_data);
}
MPI_Finalize();
return 0;
}
Now For Compilation
mpicc deadlock.c -o deadlock
For execution
mpirun --np 2 ./deadlock