Python Implementation of Mutual-Exclusion with semaphore














































Python Implementation of Mutual-Exclusion with semaphore



Description: Mutual exclusion is a program object that prevents simultaneous access to a shared resource. This concept is used in concurrent programming with a critical section, a piece of code in which processes or threads access a shared resource. Only one thread owns the critical section at a time. When a thread holds a resource, it has to lock the access to shared resource from other threads to prevent concurrent access of the resource. Upon releasing the shared resource, the thread leaves critical section allowing other thread to enter the critical section.

Program:
import threading import time class CricticalSection(): def __init__(self): self.sem = threading.Semaphore() #initializing semaphore using Semaphore class in threading module def process_1(self): while True: print("Entry Section 1") self.sem.acquire() #decrement the value of semahpore
self.criticalsection() #entering crictical section(process 1) self.sem.release() #incrementing the value of semaphore hence allowing
other thread to enter critical section
print("Critical Section over for process 1") #remainder section of the
process 1
time.sleep(3) #allowing some delay in the process def process_2(self): while True: print("Entry Section-2") self.sem.acquire() #decrement the value of semahpore
self.criticalsection() #entering crictical section (process 2) self.sem.release() #incrementing the value of semaphore hence allowing
other thread to enter critical section
print("Critical Section over for process 2") #remainder section of the
process 2
time.sleep(3) #allowing some delay in the process def criticalsection(self): print(" Entered Critical Section!. Perform operation on shared resource") def main(self): t1 = threading.Thread(target = self.process_1) #calling process 1 t1.start() t2 = threading.Thread(target = self.process_2) #calling process 2 t2.start() if __name__=="__main__": c = CricticalSection() c.main()

Output:
Entry Section 1 Entered Critical Section!. Perform operation on shared resource Critical Section over for process 1 Entry Section-2 Entered Critical Section!. Perform operation on shared resource Critical Section over for process 2 Entry Section 1 Entered Critical Section!. Perform operation on shared resource Entry Section-2 Critical Section over for process 1 Entered Critical Section!. Perform operation on shared resource Critical Section over for process 2
#Exit with KeyboardInterrupt


Comments