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 (12)
Showing
with 135 additions and 97 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.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
{
private Queue<Car> carQueue;
private int size = 10;
private Mutex mutex;
private Queue<Car> _carQueue;
private int _size = 10;
private Mutex _mutex;
// Constructor initialized with a Mutex and a Queue.
public Buffer()
{
mutex = new Mutex();
carQueue = new Queue<Car>();
_mutex = new Mutex();
_carQueue = new Queue<Car>();
}
public void push(Car c)
// This function adds a Car Object into the Queue.
public void Push(Car c)
{
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");
}
}
carQueue.Enqueue(c);
_carQueue.Enqueue(c);
}
public Car pop()
// Removes a Car Object from the Queue and returns the Queue.
public Car Pop()
{
if (carQueue.Count == 0)
if (_carQueue.Count == 0)
{
throw new Exception("Buffer empty");
}
return carQueue.Dequeue();
return _carQueue.Dequeue();
}
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;
}
public bool empty()
// Check if the Queue is empty.
public bool Empty()
{
if (carQueue.Count == 0)
{
return true;
}
return false;
return _carQueue.Count == 0;
}
public Mutex getMutex()
// returns the Mutex.
public Mutex GetMutex()
{
return this.mutex;
return this._mutex;
}
}
}
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;
......
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;
}
}
......
......@@ -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;
}
}
......
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();
}
......
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
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
No preview for this file type
No preview for this file type