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.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
......@@ -8,7 +9,7 @@ namespace ConsoleApplication1
/// 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;
......
......@@ -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);
}
......
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;
}
}
......
......@@ -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