-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemaphore_producer_consumer.py
49 lines (44 loc) · 1.18 KB
/
semaphore_producer_consumer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import threading
import time
mutex = threading.Semaphore(1)
full = threading.Semaphore(0)
empty = threading.Semaphore(3)
x = 0
def producer():
global mutex, full, empty, x
empty.acquire()
mutex.acquire()
x += 1
print(f"Producer produces item {x}\n")
mutex.release()
full.release()
def consumer():
global mutex, full, empty, x
full.acquire()
mutex.acquire()
print(f"Consumer consumes item {x}\n")
x -= 1
mutex.release()
empty.release()
def main():
while True:
print("1. PRODUCER\n2. CONSUMER\n3. EXIT")
n = int(input("ENTER YOUR CHOICE: \n"))
if n == 1:
if empty._value != 0:
producer_thread = threading.Thread(target=producer)
producer_thread.start()
else:
print("BUFFER IS FULL")
elif n == 2:
if full._value != 0:
consumer_thread = threading.Thread(target=consumer)
consumer_thread.start()
else:
print("BUFFER IS EMPTY")
elif n == 3:
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()