From 7bc3c05af9cdb0ebb0d0eb3cf0de74bdb87ab609 Mon Sep 17 00:00:00 2001 From: sherAfzal861 <95088124+sherAfzal861@users.noreply.github.com> Date: Thu, 19 Sep 2024 17:17:26 +0500 Subject: [PATCH] improved the code --- operating_system/process_synchronization/semaphore.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/operating_system/process_synchronization/semaphore.py b/operating_system/process_synchronization/semaphore.py index c07a6c22664a..d1caa9e7a188 100644 --- a/operating_system/process_synchronization/semaphore.py +++ b/operating_system/process_synchronization/semaphore.py @@ -1,4 +1,3 @@ -from random import random from threading import Event, Thread, current_thread from time import sleep @@ -57,6 +56,11 @@ class ThreadManager: Thread 1 is working with the resource. Thread-2 (thread_task) has acquired a resource. Thread 2 is working with the resource. + >>> sleep(4) + Thread 1 has finished and is releasing the resource. + A resource has been released. + Thread 2 has finished and is releasing the resource. + A resource has been released. """ def __init__(self, semaphore: CustomSemaphore, thread_count: int) -> None: @@ -74,7 +78,7 @@ def thread_task(self, thread_id: int) -> None: thread_name = current_thread().name self.semaphore.acquire(event, thread_name) print(f"Thread {thread_id} is working with the resource.") - sleep(2 + random()) # Simulate some work + sleep(2) # Simulate some work print(f"Thread {thread_id} has finished and is releasing the resource.") self.semaphore.release()