You are on page 1of 8

Methods of Handling Deadlock

We can deal with the deadlock problem in one of three ways:


1. We can ignore the problem altogether and pretend that deadlocks never occur in the
system.
2. We can use a protocol to prevent or avoid deadlocks, ensuring that the system will
never enter a deadlocked state.
3. We can allow the system to enter a deadlocked state, detect it, and recover
‫طرق التعامل مع الجمود‬
:‫يمكننا التعامل مع مشكلة المأزق بإحدى الطرق الثالث‬
.‫يمكننا تجاهل المشكلة تما ًما والتظاهر بأن الجمود ال يحدث أبدًا في النظام‬-1
.‫ مما يضمن أن النظام لن يدخل في حالة توقف تام‬، ‫يمكننا استخدام بروتوكول لمنع أو تجنب المآزق‬-2
‫يمكننا السماح للنظام بالدخول إلى حالة الجمود واكتشافه واستعادته‬-3

A. The first solution is the one used by most operating systems, including Linux and
Windows. It is cheaper than the other approaches because in many systems,
deadlocks occur infrequently.
B. To ensure that deadlocks never occur, the system can use either a deadlock
prevention or a deadlock-avoidance scheme.
‫إنها أرخص من الطرق األخرى‬Windows. ‫ و‬Linux ‫ بما في ذلك‬، ‫الحل األول هو الذي تستخدمه معظم أنظمة التشغيل‬
.‫ تحدث المآزق بشكل غير منتظم‬، ‫ألنه في العديد من األنظمة‬
‫ يمكن للنظام استخدام نظام منع حدوث طريق مسدود أو نظام تجنب طريق‬، ‫لضمان عدم حدوث حاالت الجمود مطلقًا‬
.‫مسدود‬

1. Deadlock prevention provides a set of methods to ensure that at least one of the
necessary conditions cannot hold.
2. Deadlock avoidance requires that the operating system be given additional
information in advance concerning which resources a thread will request and use
during its lifetime.
ً ‫يوفر منع الجمود مجموعة من الطرق للتأكد من أن شر‬-1
.‫طا واحدًا على األقل من الشروط الضرورية ال يمكن أن يصمد‬
‫ يتطلب تجنب حالة توقف تام أن يتم إعطاء نظام التشغيل معلومات إضافية مسبقًا بخصوص الموارد التي سيطلبها مؤشر‬-2
.‫الترابط ويستخدمها أثناء عمره‬
C.If a system does not employ either a deadlock-prevention or a deadlock avoidance
algorithm, then a deadlock situation may arise The system can provide an algorithm to
detect the deadlock and an algorithm to recover from it.
‫ يمكن للنظام توفير خوارزمية‬.‫ فقد تنشأ حالة توقف تام‬، ‫إذا كان النظام ال يستخدم خوارزمية منع الجمود أو تجنب الجمود‬
.‫الكتشاف حالة الجمود وخوارزمية للتعافي منه‬

Deadlock Prevention
A. By ensuring that at least one of the four necessary conditions cannot hold, we can
prevent the occurrence of a deadlock.
B. Mutual Exclusion: Sharable resources do not require mutually exclusive access and
thus cannot be involved in a deadlock. Read-only files are a good example of a
sharable resource.
C. Hold and Wait: we must guarantee that, whenever a thread requests a resource, it
does not hold any other resources. Two protocols can be used.
1. Each thread to request and be allocated all its resources before it begins
execution.
2. Allows a thread to request resources only when it has none.
D. Disadvantages
1. Resource utilization may be low.
2. starvation is possible.
E .No Preemption: To ensure that this condition does not hold, we can use the
following protocol.
1. If a thread is holding some resources and requests another resource that cannot be
immediately allocated to it (that is, the thread must wait), then all resources the thread
is currently holding are preempted.
OR
2. If a thread requests some resources, then check if
a. Available: allocate them.
b. Not available:
1. Allocated to some other thread that is waiting for additional resources : preempt the
desired resources from the waiting thread and allocate them to the requesting thread.
2. If the resources are neither available nor held by a waiting thread, the requesting
thread must wait.
F. Circular Wait: One way to ensure that this condition never holds is to impose a total
ordering of all resource types and to require that each thread requests resources in an
increasing order of enumeration.
H. Each thread can request resources only in an increasing order of enumeration. That
is, a thread can initially request an instance of a resource—say, Ri. After that, the
thread can request an instance of resource Rj if and only if F(Rj) > F(Ri).
‫الوقاية من الطريق المسدود ‪Deadlock Prevention‬‬

‫‪.A‬من خالل التأكد من أن شر ً‬


‫طا واحدًا على األقل من الشروط الضرورية األربعة ال يمكن أن يصمد ‪ ،‬يمكننا منع حدوث‬
‫مأزق‪.‬‬
‫‪ Mutual Exclusion.B‬االستبعاد المتبادل‪ :‬ال تتطلب الموارد القابلة للمشاركة وصوالً حصريًا للطرفين ‪ ،‬وبالتالي ال‬
‫يمكن التورط في مأزق‪ .‬تعد ملفات القراءة فقط ً‬
‫مثاال جيدًا لمورد قابل للمشاركة‪.‬‬
‫‪ . Hold and Wait.C‬االمساك – واالنتظار‪ :‬يجب أن نضمن أنه عندما يطلب موضوع ما موردًا ‪ ،‬فإنه ال يحتفظ بأي‬
‫موارد أخرى‪ .‬يمكن استخدام بروتوكولين‪.‬‬
‫‪-1‬يطلب كل موضوع وتخصيص جميع موارده قبل أن يبدأ التنفيذ‪.‬‬
‫‪-2‬يسمح لسلسلة رسائل بطلب الموارد فقط عندما ال تحتوي على أي موارد‪.‬‬

‫‪ Disadvantages.E‬سلبيات‬
‫ضا‪.‬‬
‫‪-1‬قد يكون استخدام الموارد منخف ً‬
‫‪-2‬الجوع ممكن‪.‬‬
‫‪ No Preemption‬ال توجد إجراءات وقائية‪ :‬للتأكد من أن هذا الشرط ال ينطبق ‪ ،‬يمكننا استخدام البروتوكول التالي‪.‬‬
‫‪-1‬إذا كان مؤشر الترابط يحتفظ ببعض الموارد ويطلب موردًا آخر ال يمكن تخصيصه له على الفور (أي ‪ ،‬يجب أن ينتظر‬
‫مؤشر الترابط) ‪ ،‬فإن جميع الموارد التي يحتفظ بها مؤشر الترابط حاليًا تكون ُمسبقة‪.‬‬
‫او‬
‫‪-2‬إذا طلبت سلسلة رسائل بعض الموارد ‪ ،‬فتحقق مما إذا كان‬
‫‪-1‬متاح‪ :‬تخصيصها‪.‬‬
‫‪-2‬غير متاح‪:‬‬
‫‪-1‬مخصص لبعض سالسل الرسائل األخرى التي تنتظر موارد إضافية‪ :‬استبق الموارد المطلوبة من سلسلة الرسائل‬
‫المنتظرة وقم بتخصيصها إلى سلسلة الرسائل المطلوبة‪.‬‬
‫‪-2‬في حالة عدم توفر الموارد أو عدم االحتفاظ بها بواسطة سلسلة انتظار ‪ ،‬يجب أن ينتظر مؤشر الترابط المطلوب‬

‫‪ Circular Wait.F‬انتظار دائري‪ :‬إحدى الطرق للتأكد من عدم استمرار هذا الشرط هي فرض ترتيب إجمالي لجميع‬
‫أنواع الموارد والمطالبة بأن يطلب كل مؤشر ترابط الموارد بترتيب متزايد من التعداد‪.‬‬
‫‪.H‬يمكن لكل مؤشر ترابط أن يطلب موارد فقط بترتيب متزايد من التعداد‪ .‬وهذا يعني أن الخيط يمكن أن يطلب في البداية‬
‫مثيال لمورد — على سبيل المثال ‪Ri. ،‬بعد ذلك ‪ ،‬يمكن أن يطلب مؤشر الترابط مثيالً للمورد ‪ Rj‬إذا وفقط إذا كان ‪F‬‬
‫ً‬
‫‪(Rj)> F (Ri).‬‬
• Example: In a system if
• F (tape drive) = 1.
• F (disk drive) =5
• F (printer) = 12
• If a thread is holding a tape drive, it can request a disk drive or a printer. If it is
holding a disk drive , it can request only the printer. If it is holding a printer it can
request none of the other resources. The solution is to release the printer and request
other resources in an increasing order.
‫ في نظام إذا‬:‫مثال‬
1 = )‫)محرك الشريط‬F*
5 = )‫)محرك األقراص‬F*
12 = )‫)طابعة‬F *
‫ فيمكنه‬، ‫ إذا كان يحتوي على محرك أقراص‬.‫ فيمكنه طلب محرك أقراص أو طابعة‬، ‫*إذا كان الخيط يحمل محرك أشرطة‬
‫ الحل هو تحرير الطابعة وطلب‬.‫ فال يمكنها طلب أي من الموارد األخرى‬، ‫ إذا كانت تحتفظ بطابعة‬.‫طلب الطابعة فقط‬
.‫موارد أخرى بترتيب متزايد‬

Deadlock Avoidance
* Safe sequence: A sequence of threads is a safe sequence for the current allocation state if,
for each Ti, the resource requests that Ti can still make can be satisfied by the currently
available resources plus the resources held by all Tj, with
j < i.
*If no such sequence exists, then the system state is said to be unsafe
*A safe state is not a deadlocked state.
* A deadlocked state is an unsafe state.
* Not all unsafe states are deadlocks, An unsafe state may lead to a deadlock.
‫تجنب الجمود ‪Deadlock Avoidance‬‬

‫*التسلسل اآلمن‪ :‬تسلسل الخيوط هو تسلسل آمن لحالة التخصيص الحالية ‪ ،‬إذا كان من الممكن تلبية طلبات الموارد التي ال‬
‫يزال بإمكان ‪ Ti‬القيام بها من خالل الموارد المتاحة حاليًا باإلضافة إلى الموارد التي يحتفظ بها كل ‪ ، Tj‬مع ‪i>j‬‬

‫*في حالة عدم وجود مثل هذا التسلسل ‪ ،‬يقال إن حالة النظام غير آمنة‬

‫*الدولة اآلمنة ليست حالة من الجمود‪.‬‬

‫*إن حالة الجمود هي دولة غير آمنة‪.‬‬

‫*ليست كل الدول غير اآلمنة في طريق مسدود ‪ ،‬فالدولة غير اآلمنة قد تؤدي إلى طريق مسدود‪.‬‬

‫‪Example:‬‬
‫‪consider a system with twelve resources and three threads. The maximum needs and current‬‬
‫‪needs are as shown in the table:‬‬

‫‪To reach maximum need for T0 :10 - 5 = 5‬‬


‫‪To reach maximum need for T1: 4 – 2 = 2‬‬
‫‪To reach maximum need for T2: 9 – 2 = 7‬‬
‫‪Total current needs: 5 + 2 + 2 = 9‬‬
‫‪Free available resources 12 – 9 = 3‬‬
• T0 needs 5 instances. The available is 3, so it cannot be granted.
• T1 needs 2 in stances. It is allocated from the available, then
• Available=3-2=1. Then after it finishes it release all the 4 instances to be added to the
available; available= 1+4=5,
• T2 needs 7 instances. The available is 5, so it cannot be granted.
• We go back to T0. It needs 5 instances. Since the available is 5, then it can be allocated from
the available. So available=5-5=0. After it finishes it release all the 10 instances to be added to
the available; available=0+10=10.
• We go to the remaining thread T2. It is allocated the 7 instances which it needs from the
available. So, available=10-7=3. After it finishes it release all the 9 instances to be added to the
available; available=3+9=12.
• Hence, at time T0, the system is in a safe state. The sequence satisfies the safety condition.
Suppose that, at time t1, thread T2 requests and is allocated one more resource. The system
is no longer in a safe state.

:‫مثال‬
‫ الحد األقصى لالحتياجات واالحتياجات‬.‫ضع في اعتبارك نظا ًما يحتوي على اثني عشر موردًا وثالثة مؤشرات ترابط‬
:‫الحالية كما هو موضح في الجدول‬
‫‪5=5-10 :‬‬ ‫للوصول إلى الحد األقصى من الحاجة إلى ‪T0‬‬
‫‪2=2-4 :‬‬ ‫للوصول إلى الحاجة القصوى لـ ‪T1‬‬
‫للوصول إلى الحد األقصى من الحاجة إلى ‪7=2-9 : T2‬‬
‫‪9 =2+2+5 :‬‬ ‫إجمالي االحتياجات الحالية‬
‫‪3=9-12 :‬‬ ‫الموارد المتاحة مجانا‬

‫*‪ T0‬يحتاج ‪ 5‬حاالت‪ .‬المتاح هو ‪ ، 3‬لذلك ال يمكن منحه‪.‬‬


‫*‪T1‬يحتاج ‪ 2‬في الوقفات‪ .‬يخصص من المتاح اذن‬
‫*متاح = ‪ .1 = 2-3‬ثم بعد انتهائه ‪ ،‬قم بتحرير جميع الحاالت األربع إلضافتها إلى المتاحة ؛ متاح = ‪، 5 = 4 + 1‬‬
‫*‪ T2‬يحتاج ‪ 7‬حاالت‪ .‬المتاح ‪ ، 5‬لذلك ال يمكن منحه‬
‫*نعود إلى ‪T0.‬يحتاج ‪ 5‬حاالت‪ .‬بما أن المتاح ‪ ، 5‬فيمكن تخصيصه من المتاح‪ .‬متوفر لذلك = ‪ .0 = 5-5‬بعد انتهائه ‪ ،‬قم‬
‫بتحرير جميع النسخ العشر إلضافتها إلى المتاح ؛ متاح = ‪.10 = 10 + 0‬‬
‫*نذهب إلى الخيط المتبقي ‪T2.‬يتم تخصيص الحاالت السبعة التي يحتاجها من المتاح‪ .‬إذن ‪ ،‬متاح = ‪ .3 = 7-10‬بعد‬
‫انتهائه ‪ ،‬حرر جميع الحاالت التسعة لتتم إضافتها إلى المتاح ؛ متاح = ‪.12 = 9 + 3‬‬
‫*ومن ثم ‪ ،‬في الوقت ‪ ، T0‬يكون النظام في حالة آمنة‪ .‬التسلسل يلبي شروط السالمة‪.‬‬
‫افترض أنه في الوقت ‪ ، t1‬طلبات الخيط ‪ T2‬وتخصيص مورد آخر‪ .‬النظام لم يعد في حالة آمنة‬

You might also like