Compare commits
2 commits
master
...
extremelyS
| Author | SHA1 | Date | |
|---|---|---|---|
| b12ae5b0cb | |||
| 70aad3fe1c |
7 changed files with 160 additions and 235 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
### Erklärung der Praktikumsaufgabe (Synchronisation)
|
/* ### Erklärung der Praktikumsaufgabe (Synchronisation)
|
||||||
|
|
||||||
#### **Kernziel der Aufgabe**
|
#### **Kernziel der Aufgabe**
|
||||||
Sie sollen ein **multithreaded Sensornetzwerk** simulieren, das drei Komponenten umfasst:
|
Sie sollen ein **multithreaded Sensornetzwerk** simulieren, das drei Komponenten umfasst:
|
||||||
|
|
@ -152,3 +152,4 @@ Mit dieser Struktur erfüllen Sie alle Lernziele:
|
||||||
✅ Reader-Writer-Problem
|
✅ Reader-Writer-Problem
|
||||||
✅ Producer-Consumer-Pattern
|
✅ Producer-Consumer-Pattern
|
||||||
✅ Vermeidung von Race Conditions & Deadlocks!
|
✅ Vermeidung von Race Conditions & Deadlocks!
|
||||||
|
*/
|
||||||
|
|
@ -1,43 +1,32 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <condition_variable>
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread-sicheres Analysemodell
|
||||||
|
* Vereinfachte Implementierung mit:
|
||||||
|
* - Einfachem Mutex-Schutz (kein Reader-Writer-Lock)
|
||||||
|
* - Für seltene Schreibzugriffe geeignet
|
||||||
|
*/
|
||||||
class AnalysisModel {
|
class AnalysisModel {
|
||||||
int value = 0;
|
int value = 0; // Der gespeicherte Wert
|
||||||
int reader_count = 0;
|
std::mutex mtx; // Schützt Lese/Schreibzugriffe
|
||||||
|
|
||||||
std::mutex model_mutex;
|
|
||||||
std::mutex count_mutex;
|
|
||||||
std::condition_variable no_writer;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* Liest den aktuellen Wert
|
||||||
|
* @return Der gespeicherte Wert
|
||||||
|
*/
|
||||||
int read() {
|
int read() {
|
||||||
std::unique_lock<std::mutex> count_lock(count_mutex);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
reader_count++;
|
return value;
|
||||||
|
|
||||||
if(reader_count == 1) {
|
|
||||||
model_mutex.lock();
|
|
||||||
}
|
|
||||||
count_lock.unlock();
|
|
||||||
|
|
||||||
int result = value;
|
|
||||||
|
|
||||||
count_lock.lock();
|
|
||||||
reader_count--;
|
|
||||||
if(reader_count == 0) {
|
|
||||||
model_mutex.unlock();
|
|
||||||
no_writer.notify_one();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
/**
|
||||||
}
|
* Schreibt einen neuen Wert
|
||||||
|
* @param new_val Der neue Wert
|
||||||
void write(int new_value) {
|
*/
|
||||||
std::unique_lock<std::mutex> lock(model_mutex);
|
void write(int new_val) {
|
||||||
value = new_value;
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
value = new_val;
|
||||||
no_writer.wait(lock, [this]() {
|
|
||||||
return reader_count == 0;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
83
main.cpp
83
main.cpp
|
|
@ -1,75 +1,26 @@
|
||||||
#include "sensor_network.h"
|
#include "sensor_network.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
|
||||||
#include <limits>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
constexpr size_t DEFAULT_NUM_SENSORS = 3;
|
/**
|
||||||
constexpr size_t DEFAULT_NUM_ANALYSERS = 2;
|
* Hauptprogramm
|
||||||
constexpr int DEFAULT_RUN_TIME = 30;
|
* Startet die Simulation mit festen Parametern
|
||||||
constexpr size_t DEFAULT_BUFFER_SIZE = 8;
|
* (Könnte leicht für interaktive Eingabe erweitert werden)
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
// Netzwerk mit Puffergröße 8 erstellen
|
||||||
|
SensorNetwork<8> network;
|
||||||
|
|
||||||
template<size_t N>
|
std::cout << "Starting simulation...\n";
|
||||||
void run_simulation(size_t num_sensors, size_t num_analysers, int run_time) {
|
|
||||||
SensorNetwork<N> network;
|
|
||||||
std::cout << "\n=== Simulation gestartet ===\n"
|
|
||||||
<< "Sensoren: " << num_sensors << "\n"
|
|
||||||
<< "Analysemodule: " << num_analysers << "\n"
|
|
||||||
<< "Puffergröße: " << N << "\n"
|
|
||||||
<< "Laufzeit: " << run_time << "s\n\n";
|
|
||||||
|
|
||||||
network.start(num_sensors, num_analysers);
|
// 2 Sensoren und 2 Analyse-Module starten
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(run_time));
|
network.start(2, 2);
|
||||||
|
|
||||||
|
// 30 Sekunden laufen lassen
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(30));
|
||||||
|
|
||||||
|
// Netzwerk stoppen
|
||||||
network.stop();
|
network.stop();
|
||||||
|
|
||||||
std::cout << "\n=== Simulation beendet ===\n";
|
std::cout << "Simulation finished\n";
|
||||||
}
|
|
||||||
|
|
||||||
size_t get_input(const std::string& prompt, size_t default_value) {
|
|
||||||
std::cout << prompt << " [" << default_value << "]: ";
|
|
||||||
std::string input;
|
|
||||||
std::getline(std::cin, input);
|
|
||||||
|
|
||||||
if(input.empty()) return default_value;
|
|
||||||
|
|
||||||
try {
|
|
||||||
return std::stoul(input);
|
|
||||||
} catch(...) {
|
|
||||||
std::cout << "Ungültige Eingabe. Verwende Standardwert: "
|
|
||||||
<< default_value << "\n";
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
std::cout << "=== Sensornetzwerk-Simulation ===\n"
|
|
||||||
<< "(Leere Eingabe verwendet Standardwerte)\n";
|
|
||||||
|
|
||||||
size_t num_sensors = get_input("Anzahl Sensoren", DEFAULT_NUM_SENSORS);
|
|
||||||
size_t num_analysers = get_input("Anzahl Analysemodule", DEFAULT_NUM_ANALYSERS);
|
|
||||||
int run_time = static_cast<int>(
|
|
||||||
get_input("Laufzeit (Sekunden)", DEFAULT_RUN_TIME)
|
|
||||||
);
|
|
||||||
size_t buffer_size = get_input("Puffergröße", DEFAULT_BUFFER_SIZE);
|
|
||||||
|
|
||||||
switch(buffer_size) {
|
|
||||||
case 8:
|
|
||||||
run_simulation<8>(num_sensors, num_analysers, run_time);
|
|
||||||
break;
|
|
||||||
case 16:
|
|
||||||
run_simulation<16>(num_sensors, num_analysers, run_time);
|
|
||||||
break;
|
|
||||||
case 32:
|
|
||||||
run_simulation<32>(num_sensors, num_analysers, run_time);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::cout << "Nicht unterstützte Puffergröße. Verwende Standard ("
|
|
||||||
<< DEFAULT_BUFFER_SIZE << ")\n";
|
|
||||||
run_simulation<DEFAULT_BUFFER_SIZE>(
|
|
||||||
num_sensors, num_analysers, run_time
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "Simulation erfolgreich abgeschlossen.\n";
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,63 +1,63 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <vector>
|
#include <array>
|
||||||
#include <cstddef>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread-sicherer Ringpuffer mit fester Größe N
|
||||||
|
* Implementiert das Producer-Consumer-Pattern mit:
|
||||||
|
* - Mutex für exklusiven Zugriff
|
||||||
|
* - Condition Variable für blockierendes Lesen
|
||||||
|
* - Überschreibt älteste Daten bei vollem Puffer
|
||||||
|
*/
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
class RingBuffer {
|
class RingBuffer {
|
||||||
static_assert(N > 1, "Buffer size must be greater than 1");
|
std::array<int, N> data; // Speicher für die Elemente
|
||||||
|
size_t read = 0; // Lese-Position
|
||||||
|
size_t write = 0; // Schreib-Position
|
||||||
|
bool full = false; // Flag für vollen Puffer
|
||||||
|
|
||||||
private:
|
std::mutex mtx; // Schützt alle Zugriffe
|
||||||
std::vector<int> data;
|
std::condition_variable cv; // Synchronisiert Leser
|
||||||
size_t read_ptr = 0;
|
|
||||||
size_t write_ptr = 0;
|
|
||||||
bool full = false;
|
|
||||||
|
|
||||||
std::mutex mtx;
|
|
||||||
std::condition_variable not_empty;
|
|
||||||
|
|
||||||
size_t advance(size_t ptr) const {
|
|
||||||
return (ptr + 1) % N;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RingBuffer() : data(N, 0) {}
|
/**
|
||||||
|
* Schreibt einen Wert in den Puffer
|
||||||
|
* @param value Der zu schreibende Wert
|
||||||
|
*
|
||||||
|
* Funktionsablauf:
|
||||||
|
* 1. Sperrt den Puffer mit Mutex
|
||||||
|
* 2. Schreibt Wert an aktueller Position
|
||||||
|
* 3. Überschreibt ältesten Wert wenn voll
|
||||||
|
* 4. Aktualisiert Schreib-Position
|
||||||
|
* 5. Benachrichtigt wartende Leser
|
||||||
|
*/
|
||||||
void push(int value) {
|
void push(int value) {
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
data[write] = value;
|
||||||
data[write_ptr] = value;
|
write = (write + 1) % N; // Ringverhalten
|
||||||
|
if (full) read = (read + 1) % N; // Überschreiben
|
||||||
if(full) {
|
full = (write == read); // Update Voll-Flag
|
||||||
read_ptr = advance(read_ptr);
|
cv.notify_one(); // Wecke einen Leser
|
||||||
}
|
|
||||||
|
|
||||||
write_ptr = advance(write_ptr);
|
|
||||||
full = (write_ptr == read_ptr);
|
|
||||||
|
|
||||||
not_empty.notify_one();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liest einen Wert aus dem Puffer (blockierend)
|
||||||
|
* @return Der gelesene Wert
|
||||||
|
*
|
||||||
|
* Funktionsablauf:
|
||||||
|
* 1. Sperrt den Puffer
|
||||||
|
* 2. Wartet bis Daten verfügbar
|
||||||
|
* 3. Liest Wert und aktualisiert Position
|
||||||
|
* 4. Gibt Wert zurück
|
||||||
|
*/
|
||||||
int pop() {
|
int pop() {
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
// Warte bis Daten da sind (verhindert Busy Waiting)
|
||||||
not_empty.wait(lock, [this]() {
|
cv.wait(lock, [this]{ return full || write != read; });
|
||||||
return !is_empty();
|
int val = data[read];
|
||||||
});
|
read = (read + 1) % N; // Ringverhalten
|
||||||
|
full = false; // Nicht mehr voll
|
||||||
int value = data[read_ptr];
|
return val;
|
||||||
read_ptr = advance(read_ptr);
|
|
||||||
full = false;
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_empty() const {
|
|
||||||
return !full && (read_ptr == write_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_full() const {
|
|
||||||
return full;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
BIN
sensor_network
BIN
sensor_network
Binary file not shown.
|
|
@ -3,92 +3,77 @@
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Startet das Sensornetzwerk
|
||||||
|
* @param sensors Anzahl der Sensor-Threads
|
||||||
|
* @param analysers Anzahl der Analyse-Threads
|
||||||
|
*/
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
void SensorNetwork<N>::start(size_t num_sensors, size_t num_analysers) {
|
void SensorNetwork<N>::start(size_t sensors, size_t analysers) {
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
for(size_t i = 0; i < num_sensors; ++i) {
|
// Sensor-Threads erstellen
|
||||||
sensors.emplace_back([this, i] {
|
for (size_t i = 0; i < sensors; ++i) {
|
||||||
sensor_thread(i);
|
threads.emplace_back([this] {
|
||||||
});
|
std::mt19937 gen(std::random_device{}());
|
||||||
}
|
std::uniform_int_distribution<> dist(0, 100);
|
||||||
|
|
||||||
for(size_t i = 0; i < num_analysers; ++i) {
|
while (running) {
|
||||||
analysers.emplace_back([this, i] {
|
// Zufälliges Intervall (100-500ms)
|
||||||
analyser_thread(i);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
controller = std::thread([this] {
|
|
||||||
controller_thread();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t N>
|
|
||||||
void SensorNetwork<N>::stop() {
|
|
||||||
running = false;
|
|
||||||
|
|
||||||
for(auto& t : sensors) {
|
|
||||||
if (t.joinable()) t.join();
|
|
||||||
}
|
|
||||||
for(auto& t : analysers) {
|
|
||||||
if (t.joinable()) t.join();
|
|
||||||
}
|
|
||||||
if (controller.joinable()) {
|
|
||||||
controller.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t N>
|
|
||||||
void SensorNetwork<N>::sensor_thread(int id) {
|
|
||||||
std::random_device rd;
|
|
||||||
std::mt19937 gen(rd());
|
|
||||||
std::uniform_int_distribution<> data_gen(0, 100);
|
|
||||||
std::uniform_int_distribution<> sleep_gen(100, 500);
|
|
||||||
|
|
||||||
while(running) {
|
|
||||||
std::this_thread::sleep_for(
|
std::this_thread::sleep_for(
|
||||||
std::chrono::milliseconds(sleep_gen(gen))
|
std::chrono::milliseconds(100 + gen() % 400));
|
||||||
);
|
|
||||||
|
|
||||||
int value = data_gen(gen);
|
// Messwert generieren und speichern
|
||||||
buffer.push(value);
|
buffer.push(dist(gen));
|
||||||
|
}
|
||||||
std::cout << "Sensor " << id << " produced: " << value << "\n";
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t N>
|
// Analyse-Threads erstellen
|
||||||
void SensorNetwork<N>::analyser_thread(int id) {
|
for (size_t i = 0; i < analysers; ++i) {
|
||||||
while(running) {
|
threads.emplace_back([this] {
|
||||||
|
while (running) {
|
||||||
|
// Daten aus Puffer lesen
|
||||||
int data = buffer.pop();
|
int data = buffer.pop();
|
||||||
|
|
||||||
int model_value = model.read();
|
// Analysemodell lesen
|
||||||
|
int model_val = model.read();
|
||||||
|
|
||||||
std::cout << "Analyser " << id << " processed: " << data
|
// Ausgabe (könnte auch analysieren)
|
||||||
<< " | Model: " << model_value << "\n";
|
std::cout << "Data: " << data
|
||||||
|
<< " Model: " << model_val << "\n";
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t N>
|
// Controller-Thread erstellen
|
||||||
void SensorNetwork<N>::controller_thread() {
|
threads.emplace_back([this] {
|
||||||
std::random_device rd;
|
std::mt19937 gen(std::random_device{}());
|
||||||
std::mt19937 gen(rd());
|
while (running) {
|
||||||
std::uniform_int_distribution<> update_gen(0, 100);
|
// Zufälliges Update-Intervall (500-2000ms)
|
||||||
std::uniform_int_distribution<> sleep_gen(500, 2000);
|
|
||||||
|
|
||||||
while(running) {
|
|
||||||
std::this_thread::sleep_for(
|
std::this_thread::sleep_for(
|
||||||
std::chrono::milliseconds(sleep_gen(gen))
|
std::chrono::milliseconds(500 + gen() % 1500));
|
||||||
);
|
|
||||||
|
|
||||||
int new_value = update_gen(gen);
|
// Analysemodell aktualisieren
|
||||||
model.write(new_value);
|
model.write(gen() % 100);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << "Controller updated model to: " << new_value << "\n";
|
/**
|
||||||
|
* Stoppt das Sensornetzwerk und wartet auf Threads
|
||||||
|
*/
|
||||||
|
template <size_t N>
|
||||||
|
void SensorNetwork<N>::stop() {
|
||||||
|
running = false; // Signal zum Stoppen
|
||||||
|
|
||||||
|
// Auf alle Threads warten
|
||||||
|
for (auto& t : threads) {
|
||||||
|
if (t.joinable()) t.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Explizite Instanziierungen für gängige Puffergrößen
|
||||||
template class SensorNetwork<8>;
|
template class SensorNetwork<8>;
|
||||||
template class SensorNetwork<16>;
|
template class SensorNetwork<16>;
|
||||||
template class SensorNetwork<32>;
|
template class SensorNetwork<32>;
|
||||||
|
|
|
||||||
|
|
@ -5,26 +5,25 @@
|
||||||
#include "ring_buffer.h"
|
#include "ring_buffer.h"
|
||||||
#include "analysis_model.h"
|
#include "analysis_model.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hauptklasse für das Sensornetzwerk
|
||||||
|
* @tparam N Größe des Ringpuffers
|
||||||
|
*
|
||||||
|
* Verwaltet alle Komponenten:
|
||||||
|
* - Ringpuffer für Sensordaten
|
||||||
|
* - Analysemodell
|
||||||
|
* - Threads für Sensoren, Analyse und Controller
|
||||||
|
*/
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
class SensorNetwork {
|
class SensorNetwork {
|
||||||
RingBuffer<N> buffer;
|
RingBuffer<N> buffer; // Gemeinsamer Datenpuffer
|
||||||
AnalysisModel model;
|
AnalysisModel model; // Geteiltes Analysemodell
|
||||||
std::atomic<bool> running{false};
|
std::atomic<bool> running = false; // Steuerflag für Threads
|
||||||
|
std::vector<std::thread> threads; // Alle Threads
|
||||||
std::vector<std::thread> sensors;
|
|
||||||
std::vector<std::thread> analysers;
|
|
||||||
std::thread controller;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~SensorNetwork() {
|
~SensorNetwork() { if (running) stop(); }
|
||||||
if (running) stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void start(size_t num_sensors, size_t num_analysers);
|
void start(size_t sensors, size_t analysers);
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
|
||||||
void sensor_thread(int id);
|
|
||||||
void analyser_thread(int id);
|
|
||||||
void controller_thread();
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue