can you please write a program for deadlock that can detect
deadlock and to prevent deadlock.
Answer / 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 |
Performance Algorithm A performs 10n2 basic operations and algorithm B performs 300 lg n basic operations. For what value of n does algorithm B start to show its better performance?
1 Answers ASD Lab, Qatar University, UNV,
Show by induction that 2n > n2, for all n > 4.
2 Answers Karvy, Qatar University,
Seat Reservation prog for the theatre. Write a function for seat allocation for the movie tickets. Total no of seats available are 200. 20 in each row. Each row is referred by the Character, "A" for the first row and 'J' for the last. And each seat in a row is represented by the no. 1-20. So seat in diffrent rows would be represented as A1,A2....;B1,B2.....;........J1,J2... Each cell in the table represent either 0 or 1. 0 rep would seat is available , 1 would represent seat is reserved. Booking should start from the last row (J) to the first row(A). At the max 20 seats can be booked at a time. if seats are available, then print all the seat nos like "B2" i.e (2 row, 3 col) otherwise Print "Seats are not available." and we must book consecutive seats only.
write a proram using exceptional handling create a error & display the message "THERE IS AN ERROR?... PLEASE RECTIFY"?
Given 1 to n random number, find top 10 maximum numbers and explain the time complexity of the algorithm.
what is the diffrence between ++x , x++ pleaaaaase ???
. Remove all the blank spaces between character.Matrix is of 10* 10. eg: INPUT ------------------------------------ | N | A | | V | |T ------------------------------------- | |G | U | |P | -------------------------------------- |T | | | A | | ------------------------------------ OUTPUT: ------------------------------------ | N | A | V | T | | ------------------------------------- |G |U | P | | | -------------------------------------- |T | A | | | | ------------------------------------
How to Split Strings with Regex in Managed C++ Applications?
write a program to convert temperature from fa height into celcius and vise versa,use modular programming
1 Answers Jomo Kenyatta University,
A research student is given a singly-linked list. Each node of the list has a color, which is either “Black” or “White”. He must find if there are more black nodes than white nodes, or vice versa. His advisor gives him 5,000Rs to buy a computer to do the work. He goes to the computer store and finds a slightly defective computer which costs a mere 3,000Rs. This computer has the small problem of not being able to do arithmetic. This means that he cannot use a counter to count the nodes in the list to determine the majority color. The computer is otherwise fully functional. He has the evil idea that he could buy the defective computer and somehow use it to do his work, so that he can use the rest of the money on enjoyment. Show how he can accomplish this amazing task. Write code for an algorithm called ‘findMajorityColor’ which takes a singly-linked list, L, with n nodes and returns the majority color among nodes of L. This algorithm should have the same asymptotic running time as counting the nodes (O(n)). Note: No arithmetic is allowed.
swap prog
write a program that reverses the input number of n.Formulate an equation to come up with the answer.