Chapter 10 Deadlock
Chapter 10Deadlock
What is Deadlock?� �������������������� �������������������������������������
������������������
� ������������������� ��������
� ��� ��������������������
� ��������� ������������� �������������������������
�������������������������������������!
� ��������������"�������#������
� ��������������������������� ��������������������������
� ���������� ��������������������
� �����������������������������������������������������
�������
Deadlock Illustration
A set of processes is in a DEADLOCK state when every process is waiting for an event initiated by another
process in the set
Process A
Request X
Request Y
Release X
Release Y
Process B
Request Y
Request X
Release Y
Release X
Deadlock Illustration
� $���%�����&����������'
� (���%�����&����������)
� $���%�����)�� �������
� (���%�����'�� �������
����*+�� ����������,
' )
$
(
����������
� -�����������!�.�/����������
� #���� ���!�������������������
� �����������#���� ������������ 0���
� �������������������������������� -����
�����������1
� 2 ��������������
� 3��������������� ��� ������������� � ��������
��������
����������4��������������������������� -5������6778����
������������ ����� -5������������������������
Three Solutions to Deadlock
#1: Mr./Ms. Conservative (Prevention)
time
Job waitsfor all resources
Job starts Job finishes
R2R1 WAIT
“We had better not allocate if it could ever cause deadlock”
| R1 in use |
| R2 in use |
-������������ ���������� � ��������������9������������������
Three Solutions to Deadlock …
#2: Mr./Ms. Prudent (Avoidance)
time
Job starts Job firstneeds R1
Job finishes
R2R1
Job firstneeds R2
Unsafe Safe
WAIT
“If resource is free and with its allocation we can still guarantee that everyone will finish, use it.”
(�����������������:���-����������������
Three Solutions to Deadlock…
#3: Mr./Ms. Liberal (Detection/Recovery)
time
Job starts Job finishes
R2R1
Job restarts
Deadlock detected
“If it’s free, use it -- why wait?”
��� ��������������:���������������������������5��� �� �����������1!
Names for Three Methods on Last Slide
6;��+�� �����-������
� +������������������������������� �� ������������ � ���������
<;��+�� �����$��� ���
� +���������������������������������%��������� �����
���� ��� ��� �� �����������������%�������������!
� 9�%����������� ��������������%����������������������
���������!
=;��+�� �����+������� �9�������
� $���������� ���� �� ����
� 9��������������
>�.��������� � ���������+�� ����
� ?�������������� .�/������������������
� 4�� �� �@��� $������������������� �������������� ����������������
� .�����/������ 9�������������������� ����������
� ��������@��
96 9<
-6
-<
+�� �����-������
+������������������������������� ����
� -�����*?�������������,
� 5�������������������������
AB��2�����������������������������
+�� �����-�������1
� -�����*4�� �� �@��,
C�; -���������� / ��������������%����� ����
������� �������������%���� �����������������������
�����������
C�; -���������������������������������������� �
����������� ���/��%�������������������%����
����������������
ABD������������
AB ��������E� ��������������E���F����
�� �������������������������������
+�� �����-�������1
� $�����*9��������-�������,
� $����������������������%�����������������������
��������������������������� ���������� �
�������
AB #����������������������������� �������
�������������������C�!�!�������������� �����;
AB ?�����%�����F�����������
+�� �����-�������1
� -����� ��������@��
� "� ��������������
� $�������%������������ �������������������
�� ��
Preventing Circular Wait
Process:
Request:
A B C D A B C D
W X Y Z X Y Z W
A / W
B / X
C / Y
D / Z
Impose an ordering on Resources: 1 W2 X3 Y4 Z
Process D cannot request resource W without voluntarily releasing Z first
After first 4 requests:
Problems with Linear Ordering Approach
C6;� $ ���������������������������� �����
��%����������� ���������������������������
�� ���� G�
C<;� 9��������������������������������
AB $����������������������%��������������������
����������� �����F�������������������%�������
�����������%�������������������� ���
��%����
Deadlock Avoidance
� "#���������������������������������
������� ���� ��� �� ����
AB -�����������������"#����� �����
���������������������������
��%���
Banker’s Algorithm
� (����H��$��������������������
� ������������%��������������/ 2����#���I
� ������������������/ ��2���������������� �������������������� � ����������������������I
� $������������������� �������������������������
��������������������������� �
AB ?���������������������������������� �
�/���������������J�����
Definition: Safe State
� #�������������
� $�������������������������������� �����������������
����������%�������������������
� #�������
� .������������� �� ����� ��� �����������������������
��%����������������%������������� �� ��������� ������!
��������������
� .������������� �� ����� ��� ���������������������
��� ����� �� ����� ����
+�� �����$��� ���
#����#���
�����K��������?���.��
-�������6 6 >
-�������< > L
-�������= M N
$���������A�<
+�� �����$��� ���
5�����#���
�����K���������?���.��
-�������6 N 67
-�������< < M
-�������= 6 =
$���������A�6
#������5������������
�����#����#���
�����K��������?�������.��
-�������6 6 >
-�������< > L
-������= M N �����������
#�������-�������=���%������ ���������������������
�����K��������?�������.��
5���6 6 >
5���< > L
5���= L N �����������
Current state being safe does not necessarily imply future states are safe
(�����0���
� 2������������������������� �� �� �����!
� 2�������������������������� ��������������
�� ����!
� $��� ����� ������������������������������������������!�
#�����5�������+�� �����#���
(����O��$�������
� ?��������������������������!
� �������������������������������������������!
� @���������������%��������������������������������!��
� @���������������������������������������������������������������������!
����������"�������#����� �������L�� ��#���������:��������<77=
+���#���������������(����O��$��������
� $��������� D��������������!�2������������PFQ�A������������� �����������������������9F���������!
� ?�������� �����!��2��?���P��FQ�A���������������-� ������%����������������������������������9F!
� $������������ � �����!��2��$�������P��FQ�A���� -� ������������������ �� ����������9F!
� .�� ����� � �����!�2��.�� P��FQ�A ����� -������� �� ���������������9F����������������!
.�� P��FQA�?��P��FQ�� $������� P��FQ!
Let n = number of processes, and m = number of resources types.
#�����$�������
6! K��@����� �0���������������������� � �������������!��2�����:��
@����A�$��������
0�����P�Q�A ���������� A�6�<�=��1��!
<! 0� ������������������
C�;�0����P�Q�A������
C�;�.�� � ≤ @���2���������������������������>!
=!@��� A�@����R�$��������0����P�Q�A ������������<!
>! 2��0����P�Q�AA�������������������������������������������!
#�����$�������
6! K��@����� �0���������������������� � �������������!��2�����:��
@����A�$��������
0�����P�Q�A ���������� A�6�<�=��1��!i=1;while (i <= n) Do {
if (!Finish[i] && Needi <= Work ) {Finish[i] = True;Work = Work + Allocationi;i = 1;
}else i++;
}
if (0����P�Q�AA���������������) return ( SAFE )else return ( UNSAFE );
9�������/9�%����$������������-�������-�
9�%���A���%����������������������-�!��2��9�%���� PFQ�A�������������-� ����� �����������������������9F!6!2��9�%���� ≤ .�� � ���������<!��"����������������������� ��������������������������� � �����������������!
<!2��9�%���� ≤ $�������������������=!��"��������-� ���������������������������������������!
=!-��� �������������%���� �������������-� ����� ������������������������
$��������A�$���������/ 9�%����S
$��������A�$�������� R�9�%����S
.�� � A .�� � � 9�%����SS�2�������� ������������������������ ���-�!��2��������� -� ���������� ������ ���������/����������������������
�����������(����O��$�������
� M�����������-7��������->S�=���������������$�C67�������;��(�CM�������;��� � �CT�������;!
� #�������������7�
$������� ?�� $��������
$�(� $�(� � $�(�
-7 7�6�7 T�M�=� =�=�<
-6 <�7�7� =�<�<��
-< =�7�<� U�7�<
-= <�6�6� <�<�<
-> 7�7�< >�=�=��
��������C �!;
� �������������������!�.�� ��� ���� ������?�� � $�������!
.��
$�(�
-7 T�>�=�
-6 6�<�<�
-< L�7�7�
-= 7�6�6
-> >�=�6�
� ������������������������������������%�����
V�-6��-=��-7��-<��->B�����������������������!�
��������-6 9�%����C6�7�<;�C �!;� ��������9�%���≤ $��������C�������C6�7�<;�≤C=�=�<;�� ���!
$������� .�� $��������
$�(� $�(� $�(� �
-7 7�6�7� T�>�=� <�=�7
-6 =�7�< 7�<�7�
-< =�7�6� L�7�7�
-= <�6�6� 7�6�6
-> 7�7�<� >�=�6�
� ����������������������������������%�����V-6��-=��-7��-<��->B������������������%������!�
� ����%��������C=�=�7;����-> ������� I
� ����%��������C7�<�7;����-7�������� I
Banker's Algorithm: Summary
CR; -9"H��
� +�� ����������������!
� ?�������������&����������������� �� ������������!��C@��I;
C/;� ".H��
� ?������������������������������������F�������!
AB�.������� ��������������
� -���������������������������� �� ��������� �����������
Deadlock Detection
Allow deadlock to occur, then recognize that it exists
� 9�� �� ����� ����������������������������
��������������%����
� ��� ��������� �����������������
Resource Graphs
������������ ������ �� ����
.� ���
� ����
6;��-���������������������� <;��9��������
Pi Rj
Pi Rj6;��9�%���
<;��$������ Pi Rj
Resource Graphs: Example
P1R1
P2 R2
P1 holds 2 units of R1P1 holds 1 unit of R2R1 has a total inventory of 4 unitsP2 holds 1 unit of R1P2 requests 1 unit of R2 (and is blocked)
Operations on Resource Graphs:An Overview
6;�-���������%�����������������$ ����C�;
<;�-���������%������������������9����������C�;
=; -����������������������������+��������C�;
Graph Reductions
� $������������ ��� ����������������������<�� �=�
C��������� ��������;
� $���������������������� ������������������������%�����
����� ����������� ����������������������������� �
� ��
� $���������-����� �� ����� ����� ���������������������
��%���������� ���������������-��������
� 2������������������������� ������������������������
���������������� �� �����
Operations on Resource Graphs:Details
6;�-���%���������������C$ ����;-���� ����
� -���������������� �����%����
� -������%�����������������������������������
"�������
� $ ����� ���C�-� 9F ;�����������������������9F ��%����
<;�-���%����������������C9����������;-���� ����
� ?�������������������������������%����
� -���%������������%���� ����������
"�������
� 9������������%����� ���� ����� ������-����� ����������
Operations on Resource Graphs: Details …
=; -��������������������C+��������;
-���� ����
� -���������������� �����%����
� -������������������������������������������ �
"�������
� +������������ ����� ������������������������������������ ���������
Resource Graphs
P1 R1
P2R2
1) P1 acquires 1 unit of R12) P1 releases all resources (finishes)3) P2 acquires 2 units of R24) P2 releases all resources (finishes)
NO….One sequence of reductions:
DEADLOCKED?
Resource Graphs …
P1 R1
P2R2
DEADLOCKED?
NO…. One sequence of Reductions:
1) P2 acquires 2 units of R22) P2 releases all resources (finishes)3) P1 acquires 2 units of R14) P1 releases all resources (finishes)
Resource Graphs…
@�������������������<�����������������9<�I
�� �� ���������������������������������F��������������I
I
Recovering from Deadlock
"��� �� ������������� ���� ������������������������ ������/ �� ����� ����
6;�W������������������������
/ ?�������� �����������������������!��� ��������� ��������������
<;�-���������������
/ -������ �����������������������
/ ?������������������������������������������