threaded, process and buffer producers/consumers

This commit is contained in:
2024-12-08 21:08:10 +02:00
parent 9386abc939
commit eeebe5604e
8 changed files with 255 additions and 22 deletions

22
include/bconsumer.h Normal file
View File

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

22
include/bproducer.h Normal file
View File

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

View File

@@ -0,0 +1,20 @@
//
// Created by lumijiez on 12/8/24.
//
#ifndef P_PRODUCER_CONSUMER_H
#define P_PRODUCER_CONSUMER_H
#define PNUM_PRODUCERS 3
#define PNUM_CONSUMERS 2
#define PBUFFER_SIZE 10
#define PSEM_MUTEX_NAME "/buffer_mutex"
#define PSEM_FULL_NAME "/buffer_full"
#define PSEM_EMPTY_NAME "/buffer_empty"
void pproducer(int producer_id, int write_fd);
void pconsumer(int consumer_id, int read_fd);
#endif //P_PRODUCER_CONSUMER_H