Initial commit

This commit is contained in:
2024-12-08 19:04:50 +02:00
commit 4853b9ef0a
11 changed files with 295 additions and 0 deletions

28
include/buffer.h Normal file
View File

@@ -0,0 +1,28 @@
//
// Created by lumijiez on 11/16/24.
//
#ifndef BUFFER_H
#define BUFFER_H
#include <queue>
#include <mutex>
#include <condition_variable>
class Buffer {
public:
explicit Buffer(int size);
void produce(int producerId, int item);
int consume(int consumerId);
bool isEmpty() const;
bool isFull() const;
private:
std::queue<int> items;
const int maxSize;
mutable std::mutex mtx;
std::condition_variable notFull;
std::condition_variable notEmpty;
};
#endif //BUFFER_H

22
include/consumer.h Normal file
View File

@@ -0,0 +1,22 @@
//
// Created by lumijiez on 11/16/24.
//
#ifndef CONSUMER_H
#define CONSUMER_H
#include "buffer.h"
#include <semaphore>
class Consumer {
public:
Consumer(int id, Buffer& buffer, std::counting_semaphore<5>& sem);
void run() const;
private:
int id;
Buffer& buffer;
std::counting_semaphore<5>& consumerSem;
};
#endif //CONSUMER_H

22
include/producer.h Normal file
View File

@@ -0,0 +1,22 @@
//
// Created by lumijiez on 11/16/24.
//
#ifndef PRODUCER_H
#define PRODUCER_H
#include "buffer.h"
#include <semaphore>
class Producer {
public:
Producer(int id, Buffer& buffer, std::counting_semaphore<3>& sem);
void run() const;
private:
int id;
Buffer& buffer;
std::counting_semaphore<3>& producerSem;
};
#endif //PRODUCER_H

18
include/signal_handler.h Normal file
View File

@@ -0,0 +1,18 @@
//
// Created by lumijiez on 11/16/24.
//
#ifndef SIGNAL_HANDLER_H
#define SIGNAL_HANDLER_H
#include <cmath>
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <stdexcept>
#include <limits>
#include <cstdint>
#include <bits/random.h>
#include <random>
void setupSignalHandlers();
#endif //SIGNAL_HANDLER_H