0% found this document useful (0 votes)
16 views2 pages

MPI Deadlock Demonstration Code

The document provides a C program using MPI to demonstrate deadlock through point-to-point communication between two processes. It includes code for sending and receiving data, illustrating a scenario where both processes wait indefinitely for each other. Additionally, it outlines the steps for compiling and executing the program to observe the deadlock behavior.

Uploaded by

shivakumar31855
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

MPI Deadlock Demonstration Code

The document provides a C program using MPI to demonstrate deadlock through point-to-point communication between two processes. It includes code for sending and receiving data, illustrating a scenario where both processes wait indefinitely for each other. Additionally, it outlines the steps for compiling and executing the program to observe the deadlock behavior.

Uploaded by

shivakumar31855
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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, send_data, recv_data;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

send_data = (rank == 0) ? 100 : 200;

if (rank == 0) {
printf("Process 0: Before Send\n");
fflush(stdout);

MPI_Send(&send_data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // blocking send

printf("Process 0: After Send, Before Recv\n");


fflush(stdout);

MPI_Recv(&recv_data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

printf("Process 0: Received %d\n", recv_data);


fflush(stdout);
} else if (rank == 1) {
printf("Process 1: Before Send\n");
fflush(stdout);

MPI_Send(&send_data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); // blocking send

printf("Process 1: After Send, Before Recv\n");


fflush(stdout);

MPI_Recv(&recv_data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);


printf("Process 1: Received %d\n", recv_data);
fflush(stdout);
}

MPI_Finalize();
return 0;
}
Now For Compilation
mpicc deadlock.c -o deadlock
For execution
mpirun --np 2 ./deadlock

You might also like