المشاركات : 3
المواضيع 0
الإنتساب : Oct 2014
السمعة :
0
الشكر: 0
تم شكره 0 مرات في 0 مشاركات
ارجوكم ساعدوني في كود لهذا السيناريو (علما تابع لكورس نظم التشغيل):
write a multithreaded program that implements bankers algorithm. several customers request and and release resources form the bank .the banker will grant a request only if it leaves the system in a safe state. Arequest that leaves the system in unsafe state will be denied
المشاركات : 110
المواضيع 0
الإنتساب : Apr 2022
السمعة :
3
الشكر: 0
تم شكره 47 مرات في 31 مشاركات
تفضلوا هذا هو الكود الذي ينفذ سيناريو Banker's Algorithm المطلوب:
from threading import Thread, Lock
# تعريف دالة لإيجاد نظام آمن
def is_safe_state(available, maximum, allocation, need):
work = available.copy()
finish = [False] * len(allocation)
# الحصول على الموارد المطلوبة
while True:
can_finish = False
for i in range(len(allocation)):
if not finish[i] and all(need[i][j] <= work[j] for j in range(len(available))):
can_finish = True
finish[i] = True
for j in range(len(available)):
work[j] += allocation[i][j]
if not can_finish:
break
return all(finish)
# تعريف دالة لتنفيذ طلب المورد
def request_resources(customer_id, request, available, maximum, allocation, need, lock):
lock.acquire()
# التحقق من أن الطلب يتناسب مع الحد الأقصى والمورد المتاح
if all(request[i] <= need[customer_id][i] for i in range(len(request))) and all(request[i] <= available[i] for i in range(len(request))):
# تجربة تخصيص المورد للعميل
for i in range(len(request)):
available[i] -= request[i]
allocation[customer_id][i] += request[i]
need[customer_id][i] -= request[i]
# التحقق من أن النظام آمن بعد تخصيص المورد
if is_safe_state(available, maximum, allocation, need):
print(f"الموارد المخصصة للعميل {customer_id} هي {request}")
else:
# العودة إلى الحالة الأصلية
for i in range(len(request)):
available[i] += request[i]
allocation[customer_id][i] -= request[i]
need[customer_id][i] += request[i]
print(f"الطلب المقدم من العميل {customer_id} ليس آمناً ولم يتم تخصيص المورد")
else:
print(f"الطلب المقدم من العميل {customer_id} يتعارض مع الحد الأقصى أو المورد المتاح ولم يتم تخصيص المورد")
lock.release()
# تعريف دالة لإطلاق المورد
def release_resources(customer_id, release, available, allocation, need, lock):
lock.acquire()
# تحرير المورد والتحقق من أنه لا يتعارض مع الحد الأقصى
if all(release[i] <= allocation[customer_id][i] for i in range(len(release))):
for i in range(len(release)):
available[i] += release[i]
allocation[customer_id][i] -= release[i]
need[customer