6. Write a MPI program to demonstration of deadlock using point to point communication and avoidance of deadlock by altering the call sequence.
Note: This program gives two outputs — one shows deadlock, and the other shows deadlock avoidance by changing a number in the code (check the comments carefully).
COMMAND:
- Create:
gedit prg6.c
- Compile:
mpicc prg6.c -o prg6
- Run:
mpirun -np 2 ./prg6
PROGRAM:
#include <mpi.h>
#include <stdio.h>
// Change this to 1 for deadlock, 2 for deadlock avoidance
#define DEADLOCK_PART 2
int main(int argc, char *argv[]) {
int rank, size, num = 123;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size < 2) {
if (rank == 0)
printf("Run with at least 2 processes.\n");
MPI_Finalize();
return 0;
}
#if DEADLOCK_PART == 1
// ----------- Part A: Deadlock -----------
if (rank == 0) {
printf("Process 0 waiting to receive from Process 1...\n");
MPI_Recv(&num, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(&num, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (rank == 1) {
printf("Process 1 waiting to receive from Process 0...\n");
MPI_Recv(&num, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(&num, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
#elif DEADLOCK_PART == 2
// ----------- Part B: Deadlock Avoidance -----------
if (rank == 0) {
MPI_Send(&num, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&num, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 0 received back number: %d\n", num);
} else if (rank == 1) {
MPI_Recv(&num, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(&num, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
printf("Process 1 received and sent back number: %d\n", num);
}
#endif
MPI_Finalize();
return 0;
}
OUTPUT 1:

OUTPUT 2:
