From 062a32fd3da4c04cbcbff36b798601e35e932a53 Mon Sep 17 00:00:00 2001 From: Thomas Letzgus <thomas.letzgus@student.reutlingen-university.de> Date: Thu, 25 May 2023 18:34:30 +0000 Subject: [PATCH] Upload New File --- Main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Main.py diff --git a/Main.py b/Main.py new file mode 100644 index 0000000..39586f7 --- /dev/null +++ b/Main.py @@ -0,0 +1,47 @@ +from buffer import Buffer +from producer import Producer +from consumer import Consumer + +import threading +import sys +import time + + +def main(producers, consumers): + buffer_size = 10 # Size of the buffer + buffer = Buffer(size=buffer_size) + + producer_threads = [] + consumer_threads = [] + + # Create producer threads + for i in range(producers): + producer = Producer(buffer) + producer_threads.append(producer) + producer_thread = threading.Thread(target=producer.produce) + producer_thread.start() + + # Create consumer threads + for i in range(consumers): + consumer = Consumer(buffer) + consumer_threads.append(consumer) + consumer_thread = threading.Thread(target=consumer.consume) + consumer_thread.start() + + while True: + # Print buffer state and sleep for a while + print("Buffer state:") + print("Empty:", buffer.empty()) + print("Full:", buffer.full()) + print("Buffer:", buffer.buffer) + print("------------------------") + sys.stdout.flush() + time.sleep(5) + + +# Example usage: +if __name__ == "__main__": + num_producers = int(sys.argv[1]) # Number of producers from program argument + num_consumers = int(sys.argv[2]) # Number of consumers from program argument + + main(num_producers, num_consumers) -- GitLab