Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • kehrberm/inf3
1 result
Show changes
Commits on Source (6)
Showing
with 91 additions and 56 deletions
ConsoleApplication1
\ No newline at end of file
<?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
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Threading; using System.Threading;
namespace ConsoleApplication1 namespace ConsoleApplication1
...@@ -8,7 +9,7 @@ namespace ConsoleApplication1 ...@@ -8,7 +9,7 @@ namespace ConsoleApplication1
/// Creating a Class Buffer that contains a Queue that represent the "Parking lot". /// 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 /// The Queue is limited by the size that can manually be setted
/// </summary> /// </summary>
public class Buffer public class Buffer
{ {
private Queue<Car> carQueue; private Queue<Car> carQueue;
......
...@@ -14,8 +14,7 @@ namespace ConsoleApplication1 ...@@ -14,8 +14,7 @@ namespace ConsoleApplication1
// Constructor which sets the car id and increments ++ the carId for the next object // Constructor which sets the car id and increments ++ the carId for the next object
public Car() public Car()
{ {
thisCarId = carId; thisCarId = carId++;
carId++;
Console.WriteLine("car created. id = " + thisCarId); Console.WriteLine("car created. id = " + thisCarId);
} }
......
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution;
using System.Threading; using System.Threading;
namespace ConsoleApplication1 namespace ConsoleApplication1
{ {
public class Consumer public class Consumer
{ {
private Buffer b; private Buffer _buffer;
private Random r; private Random _random;
private Boolean isAsleep; private Boolean _isAsleep;
private static Boolean wakeConsumersUp; private static Boolean _wakeConsumersUp;
public Consumer(Buffer b) public Consumer(Buffer buffer)
{ {
this.b = b; _buffer = buffer;
r = new Random(); _random = new Random();
} }
public void consume() public void Consume()
{ {
while (true) while (true)
{ {
//if isAsleep = true: method checks if wakeConsumersUp has been set to true every 50ms //if isAsleep = true: method checks if wakeConsumersUp has been set to true every 50ms
if (isAsleep) if (_isAsleep)
{ {
Console.WriteLine("consumer asleep..."); Console.WriteLine("consumer asleep...");
Thread.Sleep(50); Thread.Sleep(50);
//if wakeConsumersUp = true is asAsleep is set to true and if(isAsleep) "loop" is exited on next interation //if wakeConsumersUp = true is asAsleep is set to true and if(isAsleep) "loop" is exited on next iteration
if (wakeConsumersUp) if (_wakeConsumersUp)
{ {
Console.WriteLine("consumer was woken up"); 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 //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 else
{ {
Thread.Sleep(r.Next(500, 1500)); Thread.Sleep(_random.Next(500, 1500));
Mutex m = b.getMutex(); Mutex m = _buffer.getMutex();
m.WaitOne(); 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()); 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 //if buffer is empty: consumers are set to sleep, own instance is set to sleep
else else
{ {
wakeConsumersUp = false; _wakeConsumersUp = false;
isAsleep = true; _isAsleep = true;
} }
m.ReleaseMutex(); m.ReleaseMutex();
...@@ -66,9 +67,9 @@ namespace ConsoleApplication1 ...@@ -66,9 +67,9 @@ namespace ConsoleApplication1
} }
//this consumer instance can be woken up by calling this method //this consumer instance can be woken up by calling this method
public static void wakeUp() public static void WakeUp()
{ {
wakeConsumersUp = true; _wakeConsumersUp = true;
} }
} }
......
...@@ -7,55 +7,59 @@ namespace ConsoleApplication1 ...@@ -7,55 +7,59 @@ namespace ConsoleApplication1
public class Producer public class Producer
{ {
private Buffer b; private Buffer _buffer;
private Random r; private Random _random;
private Boolean isAsleep; private Boolean _isAsleep;
private static Boolean wakeProducersUp; private static Boolean _wakeProducersUp;
public Producer(Buffer b) public Producer(Buffer buffer)
{ {
r = new Random(); _random = new Random();
this.b = b; _buffer = buffer;
} }
public void produce() public void Produce()
{ {
while (true) while (true)
{ {
if (isAsleep) //if isAsleep = true: method checks if wakeProducersUp has been set to true every 50ms
if (_isAsleep)
{ {
Console.WriteLine("producer asleep..."); Console.WriteLine("producer asleep...");
Thread.Sleep(50); 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"); 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 else
{ {
Thread.Sleep(r.Next(500, 1500)); Thread.Sleep(_random.Next(500, 1500));
Mutex m = b.getMutex(); Mutex m = _buffer.getMutex();
m.WaitOne(); m.WaitOne();
if (b.empty()) if (_buffer.empty())
{ {
Consumer.wakeUp(); Consumer.WakeUp();
} }
if (!b.full()) if (!_buffer.full())
{ {
Car c = new Car(); Car c = new Car();
b.push(c); _buffer.push(c);
Console.WriteLine("car " + c.getThisCarId() + " added"); Console.WriteLine("car " + c.getThisCarId() + " added");
} }
//if buffer is full: producers are set to sleep, own instance is set to sleep
else else
{ {
wakeProducersUp = false; _wakeProducersUp = false;
isAsleep = true; _isAsleep = true;
} }
...@@ -65,9 +69,10 @@ namespace ConsoleApplication1 ...@@ -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;
} }
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Mime;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging; using System.Runtime.Remoting.Messaging;
using System.Threading; using System.Threading;
...@@ -12,21 +14,35 @@ namespace ConsoleApplication1 ...@@ -12,21 +14,35 @@ namespace ConsoleApplication1
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
int nrProd = 0;
int nrCons = 0;
//Parse received program arguments to ints //Parse received program arguments to int
int nrProd = int.Parse(args[0]); try
int nrCons = int.Parse(args[1]); {
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 //Create n new producers where n = received argument
for(int i = 0; i < nrProd; i++) for(int i = 0; i < nrProd; i++)
{ {
//create new Producer object //create new Producer object
Producer p = new Producer(b); Producer p = new Producer(buffer);
//create new Thread and start it //create new Thread and start it
Thread newThread = new Thread(p.produce); Thread newThread = new Thread(p.Produce);
newThread.Start(); newThread.Start();
} }
...@@ -34,10 +50,10 @@ namespace ConsoleApplication1 ...@@ -34,10 +50,10 @@ namespace ConsoleApplication1
for (int i = 0; i < nrCons; i++) for (int i = 0; i < nrCons; i++)
{ {
//create new Consumer object //create new Consumer object
Consumer c = new Consumer(b); Consumer c = new Consumer(buffer);
//create new Thread and start it //create new Thread and start it
Thread newThread = new Thread(c.consume); Thread newThread = new Thread(c.Consume);
newThread.Start(); newThread.Start();
} }
......
No preview for this file type
No preview for this file type
...@@ -5,3 +5,9 @@ C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\Co ...@@ -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.csproj.CoreCompileInputs.cache
C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe
C:\Users\Home\RiderProjects\ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb 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
No preview for this file type
No preview for this file type