adspace


can you please write a program for deadlock that can detect
deadlock and to prevent deadlock.

Answer Posted / Ram Pravesh Sahni

Here's a simple C++ code to detect and prevent deadlock using the wait-detection algorithm. However, it's essential to note that this is a simplified example and real-world applications may require more complex solutions:nn```cppn#include <iostream>nusing namespace std;nnconst int N = 2; // number of processesnconst int M = 3; // number of resourcesnnint allocated[N][M]; // allocation matrixnint needed[N][M]; // need matrixnint available[M]; // resource vectornnvoid allocate(int pid, int r) {n if (available[r] > 0 && allocated[pid][r] == -1)n {n available[r]--;n allocated[pid][r] = 1;n }n}nnvoid release(int pid, int r) {n if (allocated[pid][r] == 1)n {n allocated[pid][r] = -1;n available[r]++;n }n}nnbool safeState(int w[], bool finish[]) {n for (int i = 0; i < N; i++) {n if (!finish[i]) {n int need[M];n for (int j = 0; j < M; j++)n need[j] = needed[i][j] - allocated[i][j];nn bool safe = true;n for (int j = 0; j < M && safe; j++) {n if (need[j] > available[j]) {n safe = false;n break;n }n if (allocated[w[i]][j] > 0 && need[j] > 0)n {n if (need[j] > available[j])n {n safe = false;n break;n }n else if (available[j] < needed[w[i]][j])n {n available[j] += allocated[w[i]][j];n allocated[w[i]][j] = -1;n finish[w[i]] = true;nn for (int k = 0; k < N; k++) {n if (!finish[k]) {n safe = safeState(w, finish);n if (!safe) break;n }n}n if (safe) {n release(w[i], i);n return true;n }n allocated[w[i]][j] = allocated[i][j];n available[j] -= need[j];n }n }n }n return safe;n}nbool checkDeadlock() {n bool finish[N] = {false};n for (int i = 0; i < N; i++) {n if (!finish[i]) {n if (!safeState({i}, finish)) return true;n }n }n return false;n}nint main() {n int allocation[][M] = {{0, 2, 1}, {1, 0, 3}};n int max[][M] = {{3, 1, 2}, {2, 3, 1}};nn for (int i = 0; i < N; i++) {n for (int j = 0; j < M; j++)n {n allocated[i][j] = allocation[i][j];n needed[i][j] = max[i][j] - allocation[i][j];n }n }nn int available[] = {4, 2, 3};nn while (true) {n for (int i = 0; i < N; i++) {n if (!finish[i]) {n cout << "Process " << i + 1 << " wants to run.n";n int choice;n cin >> choice;n for (int j = 0; j < M && choice >= 0 && choice < M; j++)n {n allocate(i, j);n }n if (!checkDeadlock()) break;n release(i, choice);n }n }n }n return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Question 1: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date. *This Should Be Done IN C++

1271