Initial commit
This commit is contained in:
40
src/buffer.cpp
Normal file
40
src/buffer.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by lumijiez on 11/16/24.
|
||||
//
|
||||
#include "buffer.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Buffer::Buffer(const int size) : maxSize(size) {}
|
||||
|
||||
void Buffer::produce(const int producerId, const int item) {
|
||||
std::unique_lock lock(mtx);
|
||||
notFull.wait(lock, [this] { return !isFull(); });
|
||||
|
||||
items.push(item);
|
||||
std::cout << "Producer " << producerId << " produced: " << item << std::endl;
|
||||
|
||||
lock.unlock();
|
||||
notEmpty.notify_one();
|
||||
}
|
||||
|
||||
int Buffer::consume(const int consumerId) {
|
||||
std::unique_lock lock(mtx);
|
||||
notEmpty.wait(lock, [this] { return !isEmpty(); });
|
||||
|
||||
const int item = items.front();
|
||||
items.pop();
|
||||
std::cout << "Consumer " << consumerId << " consumed: " << item << std::endl;
|
||||
|
||||
lock.unlock();
|
||||
notFull.notify_one();
|
||||
return item;
|
||||
}
|
||||
|
||||
bool Buffer::isEmpty() const {
|
||||
return items.empty();
|
||||
}
|
||||
|
||||
bool Buffer::isFull() const {
|
||||
return items.size() >= maxSize;
|
||||
}
|
||||
18
src/consumer.cpp
Normal file
18
src/consumer.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by lumijiez on 11/16/24.
|
||||
//
|
||||
#include "consumer.h"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
Consumer::Consumer(const int id, Buffer& buffer, std::counting_semaphore<5>& sem)
|
||||
: id(id), buffer(buffer), consumerSem(sem) {}
|
||||
|
||||
void Consumer::run() const {
|
||||
while (true) {
|
||||
consumerSem.acquire();
|
||||
int item = buffer.consume(id);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
consumerSem.release();
|
||||
}
|
||||
}
|
||||
24
src/producer.cpp
Normal file
24
src/producer.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by lumijiez on 11/16/24.
|
||||
//
|
||||
#include "producer.h"
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
Producer::Producer(const int id, Buffer& buffer, std::counting_semaphore<3>& sem)
|
||||
: id(id), buffer(buffer), producerSem(sem) {}
|
||||
|
||||
void Producer::run() const {
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(1, 100);
|
||||
|
||||
while (true) {
|
||||
producerSem.acquire();
|
||||
const int item = dis(gen);
|
||||
buffer.produce(id, item);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
producerSem.release();
|
||||
}
|
||||
}
|
||||
27
src/signal_handler.cpp
Normal file
27
src/signal_handler.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by lumijiez on 11/16/24.
|
||||
//
|
||||
#include "signal_handler.h"
|
||||
|
||||
void handleSignal(const int signal) {
|
||||
if (signal == SIGUSR1) {
|
||||
std::cout << "SIGUSR1 received" << std::endl;
|
||||
} else if (signal == SIGUSR2) {
|
||||
std::random_device rd;
|
||||
std::default_random_engine generator(rd());
|
||||
std::uniform_int_distribution distribution(32, 126);
|
||||
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
const char randomChar = static_cast<char>(distribution(generator));
|
||||
std::cout << randomChar;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void setupSignalHandlers() {
|
||||
std::signal(SIGUSR1, handleSignal);
|
||||
std::signal(SIGUSR2, handleSignal);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user