add include directory
This commit is contained in:
22
include/bconsumer.h
Normal file
22
include/bconsumer.h
Normal 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
22
include/bproducer.h
Normal 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
|
||||||
28
include/buffer.h
Normal file
28
include/buffer.h
Normal 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
22
include/consumer.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// Created by lumijiez on 11/16/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef CONSUMER_H
|
||||||
|
#define CONSUMER_H
|
||||||
|
|
||||||
|
#include <semaphore>
|
||||||
|
|
||||||
|
class Consumer {
|
||||||
|
public:
|
||||||
|
Consumer(int id, int read_fd, std::counting_semaphore<5>& consumerSem);
|
||||||
|
void run(std::atomic<int>& itemsConsumed, const std::atomic<bool>& stopFlag) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int consumerId;
|
||||||
|
int readFd;
|
||||||
|
std::counting_semaphore<5>& consumerSemaphore;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
20
include/p_producer_consumer.h
Normal file
20
include/p_producer_consumer.h
Normal 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
|
||||||
21
include/producer.h
Normal file
21
include/producer.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Created by lumijiez on 11/16/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef PRODUCER_H
|
||||||
|
#define PRODUCER_H
|
||||||
|
|
||||||
|
#include <semaphore>
|
||||||
|
|
||||||
|
class Producer {
|
||||||
|
public:
|
||||||
|
Producer(int id, int write_fd, std::counting_semaphore<3>& producerSem);
|
||||||
|
void run(std::atomic<int>& itemsProduced, const std::atomic<bool>& stopFlag) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int producerId;
|
||||||
|
int writeFd;
|
||||||
|
std::counting_semaphore<3>& producerSemaphore;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
30
include/reader_writer.h
Normal file
30
include/reader_writer.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
//
|
||||||
|
// Created by lumijiez on 12/8/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef READER_WRITER_H
|
||||||
|
#define READER_WRITER_H
|
||||||
|
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
class ReaderWriter {
|
||||||
|
public:
|
||||||
|
ReaderWriter(int numReaders, int numWriters, std::string filename);
|
||||||
|
~ReaderWriter();
|
||||||
|
void run();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int numReaders;
|
||||||
|
int numWriters;
|
||||||
|
std::string filename;
|
||||||
|
|
||||||
|
sem_t resourceAccess{};
|
||||||
|
sem_t readCountAccess{};
|
||||||
|
int readCount;
|
||||||
|
int writeCount;
|
||||||
|
|
||||||
|
static void* reader(void* arg);
|
||||||
|
static void* writer(void* arg);
|
||||||
|
};
|
||||||
|
#endif //READER_WRITER_H
|
||||||
18
include/signal_handler.h
Normal file
18
include/signal_handler.h
Normal 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
|
||||||
Reference in New Issue
Block a user