0% found this document useful (0 votes)
2 views1 page

Program 6

The document provides a simple MPI program to demonstrate deadlock using point-to-point communication between two processes. It includes the code for a program named 'deadlock.c' that illustrates how blocking sends and receives can lead to deadlock. Additionally, it outlines the steps for compiling and executing the program using MPI commands.

Uploaded by

Sheela Vyasa
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)
2 views1 page

Program 6

The document provides a simple MPI program to demonstrate deadlock using point-to-point communication between two processes. It includes the code for a program named 'deadlock.c' that illustrates how blocking sends and receives can lead to deadlock. Additionally, it outlines the steps for compiling and executing the program using MPI commands.

Uploaded by

Sheela Vyasa
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, 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

You might also like