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 7de3950656149f76616e91009a1befe908e9c01f..dd61b6e59bae918be9bc7512b1820cc14b7ec169 100644 --- a/Aufgabe3/ConsoleApplication1/Buffer.cs +++ b/Aufgabe3/ConsoleApplication1/Buffer.cs @@ -1,9 +1,14 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Threading; namespace ConsoleApplication1 { + /// <summary> + /// 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 { diff --git a/Aufgabe3/ConsoleApplication1/Car.cs b/Aufgabe3/ConsoleApplication1/Car.cs index eacb0d971dc62af256bd81cb14e66419a0490270..4d194d201afe6ab824ff1baec3f391fc8417d04e 100644 --- a/Aufgabe3/ConsoleApplication1/Car.cs +++ b/Aufgabe3/ConsoleApplication1/Car.cs @@ -14,8 +14,7 @@ namespace ConsoleApplication1 // 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); } diff --git a/Aufgabe3/ConsoleApplication1/Consumer.cs b/Aufgabe3/ConsoleApplication1/Consumer.cs index 55248acbe1fc2261b16b7358d7644895857c154a..d79fa9c26d8bc2b7f10ff7ae4964f229bff6df26 100644 --- a/Aufgabe3/ConsoleApplication1/Consumer.cs +++ b/Aufgabe3/ConsoleApplication1/Consumer.cs @@ -1,62 +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 = true: method checks if wakeConsumersUp has been set to true every 50ms - if (isAsleep) + if (_isAsleep) { Console.WriteLine("consumer asleep..."); Thread.Sleep(50); - //if wakeConsumersUp = true is asAsleep is set to true and if(isAsleep) "loop" is exited on next interation - 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(); @@ -66,9 +67,9 @@ namespace ConsoleApplication1 } //this consumer instance can be woken up by calling this method - public static void wakeUp() + public static void WakeUp() { - wakeConsumersUp = true; + _wakeConsumersUp = true; } } diff --git a/Aufgabe3/ConsoleApplication1/Producer.cs b/Aufgabe3/ConsoleApplication1/Producer.cs index dbb11500b3e0f95ad15330edb96e364cf43e96ac..e2a54b4e05ce638c60d2671bf86f04f4e1cf8721 100644 --- a/Aufgabe3/ConsoleApplication1/Producer.cs +++ b/Aufgabe3/ConsoleApplication1/Producer.cs @@ -7,55 +7,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; } @@ -65,9 +69,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