The Gateway to Computer Science Excellence
+1 vote
75 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 by Veteran (54.8k points) | 75 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;

 

by (23 points)
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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,508 answers
195,524 comments
100,960 users