Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2:...

45
Chapter 10 Deadlock

Transcript of Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2:...

Page 1: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Chapter 10Deadlock

Page 2: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

What is Deadlock?� �������������������� �������������������������������������

������������������

� ������������������� ��������

� ��� ��������������������

� ��������� ������������� �������������������������

�������������������������������������!

� ��������������"�������#������

� ��������������������������� ��������������������������

� ���������� ��������������������

� �����������������������������������������������������

�������

Page 3: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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

Page 4: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Deadlock Illustration

� $���%�����&����������'

� (���%�����&����������)

� $���%�����)�� �������

� (���%�����'�� �������

����*+�� ����������,

' )

$

(

Page 5: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

����������

� -�����������!�.�/����������

� #���� ���!�������������������

� �����������#���� ������������ 0���

� �������������������������������� -����

Page 6: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

�����������1

� 2 ��������������

� 3��������������� ��� ������������� � ��������

��������

����������4��������������������������� -5������6778����

������������ ����� -5������������������������

Page 7: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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������������������

Page 8: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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.”

(�����������������:���-����������������

Page 9: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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!

Page 10: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Names for Three Methods on Last Slide

6;��+�� �����-������

� +������������������������������� �� ������������ � ���������

<;��+�� �����$��� ���

� +���������������������������������%��������� �����

���� ��� ��� �� �����������������%�������������!

� 9�%����������� ��������������%����������������������

���������!

=;��+�� �����+������� �9�������

� $���������� ���� �� ����

� 9��������������

Page 11: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

>�.��������� � ���������+�� ����

� ?�������������� .�/������������������

� 4�� �� �@��� $������������������� �������������� ����������������

� .�����/������ 9�������������������� ����������

� ��������@��

96 9<

-6

-<

Page 12: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+�� �����-������

+������������������������������� ����

� -�����*?�������������,

� 5�������������������������

AB��2�����������������������������

Page 13: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+�� �����-�������1

� -�����*4�� �� �@��,

C�; -���������� / ��������������%����� ����

������� �������������%���� �����������������������

�����������

C�; -���������������������������������������� �

����������� ���/��%�������������������%����

����������������

ABD������������

AB ��������E� ��������������E���F����

�� �������������������������������

Page 14: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+�� �����-�������1

� $�����*9��������-�������,

� $����������������������%�����������������������

��������������������������� ���������� �

�������

AB #����������������������������� �������

�������������������C�!�!�������������� �����;

AB ?�����%�����F�����������

Page 15: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+�� �����-�������1

� -����� ��������@��

� "� ��������������

� $�������%������������ �������������������

�� ��

Page 16: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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:

Page 17: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Problems with Linear Ordering Approach

C6;� $ ���������������������������� �����

��%����������� ���������������������������

�� ���� G�

C<;� 9��������������������������������

AB $����������������������%��������������������

����������� �����F�������������������%�������

�����������%�������������������� ���

��%����

Page 18: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Deadlock Avoidance

� "#���������������������������������

������� ���� ��� �� ����

AB -�����������������"#����� �����

���������������������������

��%���

Page 19: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Banker’s Algorithm

� (����H��$��������������������

� ������������%��������������/ 2����#���I

� ������������������/ ��2���������������� �������������������� � ����������������������I

� $������������������� �������������������������

��������������������������� �

AB ?���������������������������������� �

�/���������������J�����

Page 20: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Definition: Safe State

� #�������������

� $�������������������������������� �����������������

����������%�������������������

� #�������

� .������������� �� ����� ��� �����������������������

��%����������������%������������� �� ��������� ������!

��������������

� .������������� �� ����� ��� ���������������������

��� ����� �� ����� ����

Page 21: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+�� �����$��� ���

#����#���

�����K��������?���.��

-�������6 6 >

-�������< > L

-�������= M N

$���������A�<

Page 22: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+�� �����$��� ���

5�����#���

�����K���������?���.��

-�������6 N 67

-�������< < M

-�������= 6 =

$���������A�6

Page 23: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

#������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

Page 24: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

(�����0���

� 2������������������������� �� �� �����!

� 2�������������������������� ��������������

�� ����!

� $��� ����� ������������������������������������������!�

Page 25: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

#�����5�������+�� �����#���

Page 26: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

(����O��$�������

� ?��������������������������!

� �������������������������������������������!

� @���������������%��������������������������������!��

� @���������������������������������������������������������������������!

����������"�������#����� �������L�� ��#���������:��������<77=

Page 27: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

+���#���������������(����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.

Page 28: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

#�����$�������

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�������������������������������������������!

Page 29: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

#�����$�������

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 );

Page 30: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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��������� -� ���������� ������ ���������/����������������������

Page 31: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

�����������(����O��$�������

� M�����������-7��������->S�=���������������$�C67�������;��(�CM�������;��� � �CT�������;!

� #�������������7�

$������� ?�� $��������

$�(� $�(� � $�(�

-7 7�6�7 T�M�=� =�=�<

-6 <�7�7� =�<�<��

-< =�7�<� U�7�<

-= <�6�6� <�<�<

-> 7�7�< >�=�=��

Page 32: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

��������C �!;

� �������������������!�.�� ��� ���� ������?�� � $�������!

.��

$�(�

-7 T�>�=�

-6 6�<�<�

-< L�7�7�

-= 7�6�6

-> >�=�6�

� ������������������������������������%�����

V�-6��-=��-7��-<��->B�����������������������!�

Page 33: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

��������-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

Page 34: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Banker's Algorithm: Summary

CR; -9"H��

� +�� ����������������!

� ?�������������&����������������� �� ������������!��C@��I;

C/;� ".H��

� ?������������������������������������F�������!

AB�.������� ��������������

� -���������������������������� �� ��������� �����������

Page 35: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Deadlock Detection

Allow deadlock to occur, then recognize that it exists

� 9�� �� ����� ����������������������������

��������������%����

� ��� ��������� �����������������

Page 36: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Resource Graphs

������������ ������ �� ����

.� ���

� ����

6;��-���������������������� <;��9��������

Pi Rj

Pi Rj6;��9�%���

<;��$������ Pi Rj

Page 37: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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)

Page 38: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Operations on Resource Graphs:An Overview

6;�-���������%�����������������$ ����C�;

<;�-���������%������������������9����������C�;

=; -����������������������������+��������C�;

Page 39: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Graph Reductions

� $������������ ��� ����������������������<�� �=�

C��������� ��������;

� $���������������������� ������������������������%�����

����� ����������� ����������������������������� �

� ��

� $���������-����� �� ����� ����� ���������������������

��%���������� ���������������-��������

� 2������������������������� ������������������������

���������������� �� �����

Page 40: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Operations on Resource Graphs:Details

6;�-���%���������������C$ ����;-���� ����

� -���������������� �����%����

� -������%�����������������������������������

"�������

� $ ����� ���C�-� 9F ;�����������������������9F ��%����

<;�-���%����������������C9����������;-���� ����

� ?�������������������������������%����

� -���%������������%���� ����������

"�������

� 9������������%����� ���� ����� ������-����� ����������

Page 41: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Operations on Resource Graphs: Details …

=; -��������������������C+��������;

-���� ����

� -���������������� �����%����

� -������������������������������������������ �

"�������

� +������������ ����� ������������������������������������ ���������

Page 42: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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?

Page 43: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

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)

Page 44: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Resource Graphs…

@�������������������<�����������������9<�I

�� �� ���������������������������������F��������������I

I

Page 45: Chapter 10 Deadlock - Virginia Tech · 2002. 10. 31. · Three Solutions to Deadlock … #2: Mr./Ms. Prudent (Avoidance) time Job starts Job first needs R1 Job finishes R2 R1 Job

Recovering from Deadlock

"��� �� ������������� ���� ������������������������ ������/ �� ����� ����

6;�W������������������������

/ ?�������� �����������������������!��� ��������� ��������������

<;�-���������������

/ -������ �����������������������

/ ?������������������������������������������