0% found this document useful (0 votes)
1 views

Operating Systems Deadlock

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Operating Systems Deadlock

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Mustansiriayah University

Collage of Education
Computers Science Department

Chapter Six Fourth Class


Deadlock
Part 1

Dr. Ahmed Hashim


2021-
6.1.Deadlock
• In a multiprogramming environment, several processes may compete for a
finite number of resources. A process requests resources; if the resources are
not available at that time, the process enters a waiting state.
‫ على ع"دد مح"دود من‬processes ‫ ق"د تتن"افس ع"دة‬، ‫في بيئ"ة البرمج"ة المتع"ددة‬
‫ تطلب الم"وارد؛ إذا ك"انت الم"وارد غ"ير مت"وفرة في ذل"ك‬processes ‫ الـ‬.‫الم"وارد‬
.‫ تدخل حالة انتظار‬process ‫ فإن ال‬، ‫الوقت‬

• Sometimes, a waiting process is never again able to change state, because the
resources it has requested are held by other waiting processes. This situation
is called a deadlock.
‫ ألن الم"وارد ال"تي‬، ‫ ال"تي تنتظ"ر ان تغ"ير حالته"ا‬process ‫ ال يمكن ل"ل‬، ‫• في بعض األحي"ان‬
‫ تس""مى ه""ذه الحال""ة‬.‫ أخ""رى‬waiting processes ‫طلبته""ا يتم االحتف""اظ به""ا بواس""طة‬
Deadlock
• operating systems typically do not provide deadlock-prevention facilities, and it
remains the responsibility of programmers to ensure that they design deadlock-
free programs.
‫ ويبقى من مس""ؤولية‬، Deadlock ‫• ال ت""وفر أنظم""ة التش""غيل ع""ادًة تس""هيالت من""ع ال‬
.Deadlock ‫المبرمجين التأكد من تصميم برامج خالية من ال‬
6.1.1. System Model
• System consists of a finite number of resources
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has Wi instances.

 A process must request a resource before using it and must


release the resource after using it.
‫ الموارد قبل استخدامها ويجب ان يحررها بعد‬process ‫ يجب ان يطلب الـ‬
‫استخدامها‬
• A process may request as many resources as it requires to carry
out its designated task. The number of resources requested may
not exceed the total number of resources available in the
system.
.‫ ما تحتاج إليه من موارد لتنفيذ المهمة المحددة لها‬process‫• قد تطلب ال‬
.‫بحيث ال يتجاوز عدد الموارد المطلوبة عن عدد الموارد المتاحة في النظام‬
System Model Cont.
• Under the normal mode of operation, a process may utilize a resource in only
the following sequence:
1. Request: The process requests the resource. If the request cannot be granted
immediately (for example, if the resource is being used by another process), then
the requesting process must wait until it can acquire the resource.
‫ إذا ك""ان ال يمكن منح الطلب على الف""ور (على س""بيل‬.‫ الم""ورد‬process ‫• تطلب الـ‬
‫ فيجب أن تنتظ"ر‬، )‫ أخ"رى‬process ‫ إذا ك"ان الم"ورد قي"د االس"تخدام من قب"ل‬، ‫المث"ال‬
.‫ التي قامت بالطلب حتى تتمكن من الحصول على المورد‬process

2. Use: The process can operate on the resource (for example, if the resource is
a printer, the process can print on the printer).
, printer ‫ اذا كان المورد هو‬:‫ ( كمثال‬.‫ أستخدام المورد‬process ‫• تستطيع الـ‬
. Printer ‫ الطباعة على هذه الـ‬process ‫فتستطيع الـ‬

3. Release: The process releases the resource.

‫ يحرر المورد‬process ‫الـ‬ •


To illustrate a deadlocked state:
• consider a system with three CD R/W drives. Suppose each of three
processes holds one of these CD R/W drives.
 If each process is now requesting another drive, the three processes will
be in a deadlocked state.
 Each is waiting for the event “CD RW is released,” which can be
caused only by one of the other waiting processes. This example
illustrates a deadlock involving the same resource type.

:Deadlock ‫• لتوضيح حالة ال‬


.CD R / W ‫ النظام به مورد واحد من الـ محركات أقراص‬-
.processes ‫به ثالثة من هذا النوع مع ثالثة‬
‫ من الثالث يستخدم او يمسك أحد‬process ‫ افترض أن كل‬-
. CD R / W ‫محركات أقراص‬
، ‫ تطلب اآلن محرك أقراص آخر‬process ‫ إذا كانت كل‬-
.Deadlock ‫ الثالث في حالة‬processes ‫فستكون ال‬
‫ " والذي يمكن أن‬CD RW ‫ كل منها ينتظر الحدث "تم تحرير‬-
‫ يوضح‬.‫ األخرى‬waiting processes ‫يحدث فقط بسبب إحدى‬
.‫ لنظام يتضمن نفس نوع المورد‬Deadlock ‫هذا المثال حالة‬
• Deadlocks may also involve different resource types.
 For example, consider a system with one printer, and
one DVD drive.
 Suppose that process Pi is holding the DVD and process
Pj is holding the printer.
 If Pi requests the printer and Pj requests the DVD drive,
a deadlock occurs.

.‫ أيضا أنواع مختلفة من الموارد‬Deadlock ‫• قد تشمل ال‬


‫ النظام به طابعة واحدة ومحرك أقراص‬، ‫على سبيل المثال‬
.‫ واحد‬DVD
DVD ‫ يستخدم او يمسك قرص‬process Pi ‫افترض أن‬
.‫ ت يستخدم او يمسك الطابعة‬process Pj ‫وأن‬
، DVD ‫ محرك أقراص‬Pj ‫ الطابعة وطلبت‬Pi ‫إذا طلبت‬
.Deadlock ‫يحدث‬
6.2. Deadlock Characterization
In a deadlock, processes never finish executing, and system resources are tied
up (‫)مقيد‬, preventing other jobs from starting
6.2.1. Necessary Conditions
Deadlock can arise if four conditions hold simultaneously (‫)في نفس الوقت‬.
1. Mutual exclusion: At least one resource must be held in a
non-sharable mode; that is, only one process at a time can use
the resource. If another process requests that resource, the
requesting process must be delayed until the resource has
been released.
‫ يجب االحتفاظ بمورد واحد على األقل في وضع غير قابل‬:‫االستبعاد المتبادل‬
‫ واحدة فقط في كل مرة يمكن ان يستخدام‬process ، ‫للمشاركة ؛ بمعنى‬
process ‫ فيجب تأجيل الـ‬، ‫ أخرى ذلك المورد‬process ‫ إذا طلبت‬.‫المورد‬
.‫الطالب حتى يتم تحرير المورد‬
2. Hold and wait: a process holding at least one resource
and waiting to acquire additional resources held by other
processes
‫ يمسك مورًدا واحًدا على األقل وتنتظر‬process ‫ الـ‬:‫الالمساك واالنتظار‬
‫ األخرى‬processes ‫الحصول على موارد إضافية تمسك بها الـ‬
Deadlock Characterization Cont.
3. No preemption: Resources cannot be preempted; that is,
a resource can be released only by the process holding it, after
that process has completed its task.

‫ ال يمكن استباق (استقطاع) الموارد ؛ أي أنه ال يمكن تحرير‬:‫ال يوجد استباق‬


‫ بعد أن تكمل هذه الـ‬، ‫ التي تحتفظ بها‬process ‫المورد إال من خالل الـ‬
.‫ مهمتها‬process

4. Circular wait: There exists a set {P0, P1, ..., Pn} of


waiting processes, such that P0 is waiting for a resource held
by P1, P1 is waiting for a resource held by P2, …., Pn-1 is
waiting for a resource held by Pn, and Pn is waiting for a
resource held by P0.
,waiting processes {P0, P1, ..., Pn} ‫يوجد على االقل مجموعة من الـ‬
‫ ينتظر مورد‬P2 , Pn-1‫ ينتظر مورد يمسك به‬P1 , P1 ‫ ينتظر مورد يمسك به‬P0
. P0 ‫ ينتظر مورد يمسك به‬Pn , Pn ‫يمسك به‬
6.2.2. Resource-Allocation
Graph
Deadlocks can be described more precisely in terms of a directed graph called a
system resource-allocation graph consists of:
 This graph consists of a set of vertices V and a set of edges E.

• The set of vertices V is partitioned into two different types of nodes:


P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system

R = {R1, R2, …, Rm}, the set consisting of all resource types in


the system

• Request edge: A directed edge from process Pi to resource


type Rj is denoted by Pi → Rj; it signifies that process Pi, has
requested an instance of resource type Rj, and is currently
waiting for that resource.
• Assignment edge : edge from resource type Rj to process
Pi is denoted by Rj → Pi; it signifies that an instance of
resource type Rj has been allocated to process Pi
•We represent each process Pi, as a circle
•We represent each resource type Rj as a rectangle. Since resource
type Rj may have more than one instance, we represent each such
instance as a dot within the rectangle.

•Note that a request edge points to only the rectangle Rj,

•An assignment edge must also designate one of the dots in the rectangle (instance).

 When process Pi, requests an instance of resource type Rj, a request edge is inserted in
the resource-allocation graph. When this request can be fulfilled, the request edge is
instantaneously transformed to an assignment edge.
 When the process no longer needs access to the resource, it releases the resource; as a
result, the assignment edge is deleted.
RESOURCE ALLOCATION GRAPH EXAMPLE
The resource-allocation graph shown in the figure 6.1 depicts the following
• situation
The sets P, R, and E:
- P = {P1, P2, P3}
- R = {R1, R2 ,R3, R4}
- E = {P1 →R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1,
R3 →P3 }
• Resource instances:
- One instance of resource type R1
- Two instances of resource type R2
- One instance of resource type R3
Figure 6.1
- Three instances of resource type R4
• Process states:
- Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type
R1.
- Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.
- Process P3 is holding an instance of R3.
Given the definition of a resource-allocation graph, it can
be shown that, if the graph contains no cycles, then no
process in the system is deadlocked. If the graph does
contain a cycle, then a deadlock may exist.
If each resource type has exactly one instance, then a
cycle implies that a deadlock has occurred. Each process
involved in the cycle is deadlocked.
If each resource type has several instances, then a cycle
does not necessarily imply that a deadlock has occurred.

:‫من الشكل اعاله يمكن مالحظة‬


‫ في النظام في حالة الـ‬process ‫ فلن تكون هناك‬، cycles ‫ إذا لم يحتوي الرسم على‬-
. Deadlock
.Deadlock ‫ فقد يكون هناك‬، cycles ‫إذا كان الرسم يحتوي على‬
‫ تشير إلى‬cycle ‫ فإن ال‬، one instance ‫ إذا كان لكل نوع من الموارد حالة واحدة بالضبط‬-
.Deadlock ‫ وصلت إلى‬cycle ‫ تشارك في ال‬process ‫ كل‬.Deadlocked ‫حدوث‬
‫ ال تعني‬cycle ‫ فإن ال‬، several instances ‫ إذا كان لكل نوع من الموارد عدة حاالت‬-
Deadlock ‫بالضرورة حدوث‬
EXAMPLE 1: To illustrate this concept, we return to
the resource-allocation graph depicted in Figure 6.1.
• Suppose that process P3 requests an instance of resource
type R2. Since no resource instance is currently available, a
request edge P3 → R2 is added to the graph (Figure 6.2)
‫ وبما‬. R2 ‫ تطلب عدد واحد من المورد‬process P3 ‫نفرض ان الـ‬
‫ان ال يتوفر في هذه اللحظة هذا المورد (محجوز من‬
‫ كما‬request edge P3 → R2 ‫ فيتم اضافة‬,‫) اخرى‬PROCESSES Figure 6.1
6.2 ‫في الى الشكل‬
• At this point, two minimal cycles exist in the system:
1- P1 →R1→ P2 →R3→ P3 →R2→ P1
2. P2 →R3→ P3 →R2→ P2

Processes P1, P2, and P3 are Deadlocked.


 Process P2 is waiting for the resource R3, which is
held by process P3.
 Process P3 is waiting for either process P1 or process
P2 to release resource R2.
 In addition, process P1 is waiting for process P2 to
release resource R1.
EXAMPLE 2
• Now consider the resource-allocation graph
in Figure 6.3. In this example, we also have
a cycle: P1 →R1→ P3 →R2→ P1
• However, there is no deadlock. Observe
that process P4 may release its instance
of resource type R2. That resource can
then be allocated to P3, breaking the
cycle.
process P4 ‫ الحظ ان الـ‬.Deadlock ‫هنا ال يوجد‬
‫ وهذا المورد ممكن ان‬, R2 ‫ممكن ان تحرر المورد‬
.cycle ‫ فيتم كسر الـ‬P3 ‫يحجز الى‬

BASIC FACTS
• If graph contains no cycles  no deadlock.
• If graph contains a cycle 
- if only one instance per resource type, then deadlock.
- if several instances per resource type, possibility of
deadlock
End of Part1

You might also like