search
Log In
2 votes
257 views
In certain countries, when two people meet they bow to each other. The protocol is that one of them bows first and stays down until the other one bows. If they bow at the same time, they will both stay bowed forever. Write a program that does not deadlock.
in Operating System 257 views

1 Answer

0 votes

I think the following program would suffice: 

bowing = [false, false]

bowing[i] = true;

if(bowing[j]==true) bowing[i] = false;

 

0
If both of them execute bowing[i] = true,  then each one will be waiting for the other to finish bowing. I think you should lock it with a mutex.
0
That is the need of the question that two remain bowing if both bowed at the same time..
0

I thought that "If they both bow at the same time, they will both stay bowed forever" was the deadlock condition which had to be avoided. How are you avoiding deadlock through your code and what is the deadlock condition? @GMPrakhar

0
Deadlock condition can be thought of as the situation where we use infinite wait, for example

while(bowing[j]);

bowing[i] = false;

In my solution, I avoided the infinite wait, confirming with the question's language.

Related questions

0 votes
0 answers
1
272 views
Write a program that detects if there is a deadlock in the system by using a resource allocation graph. Your program should read from a file the following inputs: the number of processes and the number of resources. For each process if should read four numbers: the ... is a deadlock in the system. In case there is, the program should print out the identities of all processes that are deadlocked.
asked Oct 30, 2019 in Operating System Lakshman Patel RJIT 272 views
0 votes
0 answers
2
0 votes
0 answers
3
90 views
Write a program to implement the deadlock detection algorithm with multiple resources of each type. Your program should read from a file the following inputs: the number of processes, the number of resource types, the number of resources of each type in existence ... a deadlock in the system. In case there is, the program should print out the identities of all processes that are deadlocked.
asked Oct 30, 2019 in Operating System Lakshman Patel RJIT 90 views
0 votes
0 answers
4
82 views
Repeat the previous problem, but now avoid starvation. When a baboon that wants to cross to the east arrives at the rope and finds baboons crossing to the west, he waits until the rope is empty, but no more westward-moving baboons are allowed to start until at least one baboon has crossed the other way.
asked Oct 30, 2019 in Operating System Lakshman Patel RJIT 82 views
...