diff --git a/Aufgabe3/.idea/.idea.ConsoleApplication1/.idea/.name b/Aufgabe3/.idea/.idea.ConsoleApplication1/.idea/.name new file mode 100644 index 0000000000000000000000000000000000000000..dcd883001e6c3127e4b622983becc39d25199cf6 --- /dev/null +++ b/Aufgabe3/.idea/.idea.ConsoleApplication1/.idea/.name @@ -0,0 +1 @@ +ConsoleApplication1 \ No newline at end of file diff --git a/Aufgabe3/.idea/.idea.ConsoleApplication1/.idea/vcs.xml b/Aufgabe3/.idea/.idea.ConsoleApplication1/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..6c0b8635858dc7ad44b93df54b762707ce49eefc --- /dev/null +++ b/Aufgabe3/.idea/.idea.ConsoleApplication1/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$/.." vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/Aufgabe3/ConsoleApplication1/Buffer.cs b/Aufgabe3/ConsoleApplication1/Buffer.cs index bc30db5b211f49c64c9345b3bf4399ad86cc95b2..dd61b6e59bae918be9bc7512b1820cc14b7ec169 100644 --- a/Aufgabe3/ConsoleApplication1/Buffer.cs +++ b/Aufgabe3/ConsoleApplication1/Buffer.cs @@ -1,79 +1,67 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Threading; namespace ConsoleApplication1 { /// <summary> - /// Created a Class Buffer that contains a Queue which represents the "Garage". - /// The Queue is limited by the size + /// Creating a Class Buffer that contains a Queue that represent the "Parking lot". + /// The Queue is limited by the size that can manually be setted /// </summary> - + public class Buffer { - private Queue<Car> carQueue; - private int size = 10; - private Mutex mutex; + private Queue<Car> _carQueue; + private int _size = 10; + private Mutex _mutex; - // The Buffer has a Mutex and a Queue + // Constructor initialized with a Mutex and a Queue. public Buffer() { - mutex = new Mutex(); - carQueue = new Queue<Car>(); + _mutex = new Mutex(); + _carQueue = new Queue<Car>(); } - // This functions adds a Car object into the Queue - public void push(Car c) + // This function adds a Car Object into the Queue. + public void Push(Car c) { - // Throws an Exception if the Buffer size equals size - if (carQueue.Count == size) + // Check whether the Queue is full and throws an Exception if so. + if (_carQueue.Count == _size) { throw new Exception("Buffer full"); - } - // Adds the passed Car object with the default Enqueue method - carQueue.Enqueue(c); + } + + _carQueue.Enqueue(c); } - // removes a Car from the Queue and returns the Queue - public Car pop() + // Removes a Car Object from the Queue and returns the Queue. + public Car Pop() { - // Throws an Exception if the Buffer size equals 0 - if (carQueue.Count == 0) + if (_carQueue.Count == 0) { throw new Exception("Buffer empty"); } - return carQueue.Dequeue(); + return _carQueue.Dequeue(); } - // returns a wheather the Queue is full or not - public bool full() + // Check if the Queue is full. + public bool Full() { - bool isFull = false; - if (carQueue.Count == size) - { - isFull = true; - } - - return isFull; + return _carQueue.Count == _size; } - // returns a wheather the Queue is empty or not - public bool empty() + // Check if the Queue is empty. + public bool Empty() { - if (carQueue.Count == 0) - { - return true; - } - - return false; + return _carQueue.Count == 0; } - // returns the mutex - public Mutex getMutex() + // returns the Mutex. + public Mutex GetMutex() { - return this.mutex; + return this._mutex; } - } } diff --git a/Aufgabe3/ConsoleApplication1/Car.cs b/Aufgabe3/ConsoleApplication1/Car.cs index f7aa24860cc3f1e37f4298237f6d9cf5b70e6a00..4d194d201afe6ab824ff1baec3f391fc8417d04e 100644 --- a/Aufgabe3/ConsoleApplication1/Car.cs +++ b/Aufgabe3/ConsoleApplication1/Car.cs @@ -1,33 +1,31 @@ using System; - - namespace ConsoleApplication1 { - +//Class for the objects which get stored in the buffer from the class Producer and deleted from the class Consumer public class Car { static int carId = 0; private int thisCarId; + // Constructor which sets the car id and increments ++ the carId for the next object public Car() { - thisCarId = carId; - carId++; + thisCarId = carId++; Console.WriteLine("car created. id = " + thisCarId); } - - public void count() + + // Destructor which decrements -- the carId + ~Car() { - for (int i = 0; i < 100; i++) - { - Console.WriteLine(i); - } + carId--; + Console.WriteLine("car deleted. id = " + thisCarId); } - + + // Returns the carId for Consumer and Producer so they can get the carId public int getThisCarId() { return thisCarId; diff --git a/Aufgabe3/ConsoleApplication1/Consumer.cs b/Aufgabe3/ConsoleApplication1/Consumer.cs index 4b10acddf2194927523318b1d756db145416de59..d79fa9c26d8bc2b7f10ff7ae4964f229bff6df26 100644 --- a/Aufgabe3/ConsoleApplication1/Consumer.cs +++ b/Aufgabe3/ConsoleApplication1/Consumer.cs @@ -1,58 +1,63 @@ using System; -using System.Runtime.CompilerServices; +using System.Runtime.ConstrainedExecution; using System.Threading; + namespace ConsoleApplication1 { public class Consumer { - private Buffer b; - private Random r; - private Boolean isAsleep; - private static Boolean wakeConsumersUp; + private Buffer _buffer; + private Random _random; + private Boolean _isAsleep; + private static Boolean _wakeConsumersUp; - public Consumer(Buffer b) + public Consumer(Buffer buffer) { - this.b = b; - r = new Random(); + _buffer = buffer; + _random = new Random(); } - - public void consume() + + public void Consume() { while (true) { - if (isAsleep) + //if isAsleep = true: method checks if wakeConsumersUp has been set to true every 50ms + if (_isAsleep) { Console.WriteLine("consumer asleep..."); Thread.Sleep(50); - - if (wakeConsumersUp) + //if wakeConsumersUp = true is asAsleep is set to true and if(isAsleep) "loop" is exited on next iteration + if (_wakeConsumersUp) { Console.WriteLine("consumer was woken up"); - isAsleep = false; + _isAsleep = false; } } + //if consumer isnt asleep: consumer gets mutex from buffer, wakes up producers if buffer is full and removes cars if consumer is not empty else { - Thread.Sleep(r.Next(500, 1500)); - Mutex m = b.getMutex(); + Thread.Sleep(_random.Next(500, 1500)); + Mutex m = _buffer.getMutex(); m.WaitOne(); - if (b.full()) + if (_buffer.full()) { - Producer.wakeUp(); + Producer.WakeUp(); } - if (!b.empty()) + if (!_buffer.empty()) { - Car c = b.pop(); + Car c = _buffer.pop(); Console.WriteLine("car pulled out of parking space. car id = " + c.getThisCarId()); } + + //if buffer is empty: consumers are set to sleep, own instance is set to sleep else { - wakeConsumersUp = false; - isAsleep = true; + _wakeConsumersUp = false; + _isAsleep = true; } m.ReleaseMutex(); @@ -60,9 +65,11 @@ namespace ConsoleApplication1 } } } - public static void wakeUp() + + //this consumer instance can be woken up by calling this method + public static void WakeUp() { - wakeConsumersUp = true; + _wakeConsumersUp = true; } } diff --git a/Aufgabe3/ConsoleApplication1/Producer.cs b/Aufgabe3/ConsoleApplication1/Producer.cs index 9301e9477b451a7d5bba67cfc728877607a012d6..7e8c4977b2c3806338aca66da414ebb1e7783eab 100644 --- a/Aufgabe3/ConsoleApplication1/Producer.cs +++ b/Aufgabe3/ConsoleApplication1/Producer.cs @@ -6,55 +6,59 @@ namespace ConsoleApplication1 public class Producer { - private Buffer b; - private Random r; - private Boolean isAsleep; - private static Boolean wakeProducersUp; + private Buffer _buffer; + private Random _random; + private Boolean _isAsleep; + private static Boolean _wakeProducersUp; - public Producer(Buffer b) + public Producer(Buffer buffer) { - r = new Random(); - this.b = b; + _random = new Random(); + _buffer = buffer; } - public void produce() + public void Produce() { while (true) { - if (isAsleep) + //if isAsleep = true: method checks if wakeProducersUp has been set to true every 50ms + if (_isAsleep) { Console.WriteLine("producer asleep..."); Thread.Sleep(50); - if (wakeProducersUp) + //if wakeProducersUp = true is asAsleep is set to true and if(isAsleep) "loop" is exited on next iteration + if (_wakeProducersUp) { Console.WriteLine("producer was woken up"); - isAsleep = false; + _isAsleep = false; } } + //if producer isnt asleep: producer gets mutex from buffer, wakes up consumers if buffer is empty and removes cars if producers is not full else { - Thread.Sleep(r.Next(500, 1500)); - Mutex m = b.getMutex(); + Thread.Sleep(_random.Next(500, 1500)); + Mutex m = _buffer.getMutex(); m.WaitOne(); - if (b.empty()) + if (_buffer.empty()) { - Consumer.wakeUp(); + Consumer.WakeUp(); } - if (!b.full()) + if (!_buffer.full()) { Car c = new Car(); - b.push(c); + _buffer.push(c); Console.WriteLine("car " + c.getThisCarId() + " added"); } + //if buffer is full: producers are set to sleep, own instance is set to sleep else { - wakeProducersUp = false; - isAsleep = true; + _wakeProducersUp = false; + _isAsleep = true; } @@ -64,9 +68,10 @@ namespace ConsoleApplication1 } } - public static void wakeUp() + //this consumer instance can be woken up by calling this method + public static void WakeUp() { - wakeProducersUp = true; + _wakeProducersUp = true; } } diff --git a/Aufgabe3/ConsoleApplication1/Program.cs b/Aufgabe3/ConsoleApplication1/Program.cs index 26bb25504e988cab73285534f9c0ab4023a655f2..e9d6e7d3077815e21d5d798deddb376c323e8a03 100644 --- a/Aufgabe3/ConsoleApplication1/Program.cs +++ b/Aufgabe3/ConsoleApplication1/Program.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Net.Mime; +using System.Runtime.CompilerServices; using System.Runtime.Remoting.Messaging; using System.Threading; @@ -12,21 +14,35 @@ namespace ConsoleApplication1 { public static void Main(string[] args) { + + int nrProd = 0; + int nrCons = 0; - //Parse received program arguments to ints - int nrProd = int.Parse(args[0]); - int nrCons = int.Parse(args[1]); + //Parse received program arguments to int + try + { + nrProd = int.Parse(args[0]); + nrCons = int.Parse(args[1]); + } + catch (FormatException ignored) + { + Console.WriteLine("Please provide two positive integers as program arguments"); + Environment.Exit(1); + } + + + - Buffer b = new Buffer(); + Buffer buffer = new Buffer(); //Create n new producers where n = received argument for(int i = 0; i < nrProd; i++) { //create new Producer object - Producer p = new Producer(b); + Producer p = new Producer(buffer); //create new Thread and start it - Thread newThread = new Thread(p.produce); + Thread newThread = new Thread(p.Produce); newThread.Start(); } @@ -34,10 +50,10 @@ namespace ConsoleApplication1 for (int i = 0; i < nrCons; i++) { //create new Consumer object - Consumer c = new Consumer(b); + Consumer c = new Consumer(buffer); //create new Thread and start it - Thread newThread = new Thread(c.consume); + Thread newThread = new Thread(c.Consume); newThread.Start(); } diff --git a/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.exe b/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.exe index 02aa27e8606244769bbca3da4331b159205bf03c..774de8d4f13bbf69d2c9d90bfab528e54ccd92be 100644 Binary files a/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.exe and b/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.exe differ diff --git a/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.pdb b/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.pdb index 9f7cb6a08434058f9533c684880c78f9253e3888..cc3427a8e02ea48332d80d114b47b4b4aa19d210 100644 Binary files a/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.pdb and b/Aufgabe3/ConsoleApplication1/bin/Debug/ConsoleApplication1.pdb differ diff --git a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csproj.FileListAbsolute.txt b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csproj.FileListAbsolute.txt index 732c55b417cbe29066fef6454737ef73b79c6231..8ac6070d45f60bdd96a7fa82c7b1ab5684f533b7 100644 --- a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csproj.FileListAbsolute.txt +++ b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csproj.FileListAbsolute.txt @@ -5,3 +5,9 @@ C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\Co C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.CoreCompileInputs.cache C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb +C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe +C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb +C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.csprojAssemblyReference.cache +C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.CoreCompileInputs.cache +C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe +C:\Users\Niklas\RiderProjects\inf3_gitlab\Aufgabe3\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb diff --git a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csprojAssemblyReference.cache b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csprojAssemblyReference.cache new file mode 100644 index 0000000000000000000000000000000000000000..c2a8e7ad6ad1d4f93124723da4bc5cc00957a005 Binary files /dev/null and b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.csprojAssemblyReference.cache differ diff --git a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.exe b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.exe index 02aa27e8606244769bbca3da4331b159205bf03c..774de8d4f13bbf69d2c9d90bfab528e54ccd92be 100644 Binary files a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.exe and b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.exe differ diff --git a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.pdb b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.pdb index 9f7cb6a08434058f9533c684880c78f9253e3888..cc3427a8e02ea48332d80d114b47b4b4aa19d210 100644 Binary files a/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.pdb and b/Aufgabe3/ConsoleApplication1/obj/Debug/ConsoleApplication1.pdb differ