refactor code

This commit is contained in:
2025-04-24 23:59:44 +08:00
parent ffa95bc8fc
commit de6e8570fe
4 changed files with 34 additions and 33 deletions

View File

@@ -36,7 +36,6 @@ std::deque<Customer> customer_q;
std::counting_semaphore<100> customer_num_sem{0}; std::counting_semaphore<100> customer_num_sem{0};
// END_LST_COUNT_SEM // END_LST_COUNT_SEM
void signal_handler(int signum) { void signal_handler(int signum) {
// std::cout << "\nReceived signal " << signum << ", stopping...\n"; // std::cout << "\nReceived signal " << signum << ", stopping...\n";
write(STDOUT_FILENO, "\nCaught SIGINT, shutting down...\n", 33); write(STDOUT_FILENO, "\nCaught SIGINT, shutting down...\n", 33);
@@ -59,11 +58,17 @@ void teller_thread(
(curr_tick - start_serving_time) / TIME_GRAN_MSEC)) (curr_tick - start_serving_time) / TIME_GRAN_MSEC))
.count() <= curr_customer.service_time) { .count() <= curr_customer.service_time) {
// We are still serving, do nothing // We are still serving, do nothing
std::osyncstream(std::cout) << "Teller id " << id << " is serving customer " << curr_customer.id << " at tick " << tick_count << std::endl; std::osyncstream(std::cout)
<< "Teller id " << id << " is serving customer "
<< curr_customer.id << " at tick " << tick_count
<< std::endl;
goto next_tick; goto next_tick;
} else { } else {
// We have just finished serving. // We have just finished serving.
std::osyncstream(std::cout) << "Teller id " << id << " finishied serving customer " << curr_customer.id << " at tick " << tick_count << std::endl; std::osyncstream(std::cout)
<< "Teller id " << id << " finishied serving customer "
<< curr_customer.id << " at tick " << tick_count
<< std::endl;
is_serving = false; is_serving = false;
} }
} }
@@ -86,7 +91,8 @@ void teller_thread(
} }
} else { } else {
std::osyncstream(std::cout) std::osyncstream(std::cout)
<< "Teller id " << id << " is idle at tick " << tick_count << std::endl; << "Teller id " << id << " is idle at tick " << tick_count
<< std::endl;
} }
next_tick: next_tick:
@@ -108,7 +114,9 @@ void customer_thread(
std::chrono::time_point curr_tick = start_tick; std::chrono::time_point curr_tick = start_tick;
unsigned int tick_count = 0; unsigned int tick_count = 0;
std::chrono::time_point target_tick = start_tick + std::chrono::milliseconds(TIME_GRAN_MSEC) * info.arrival_time; std::chrono::time_point target_tick =
start_tick +
std::chrono::milliseconds(TIME_GRAN_MSEC) * info.arrival_time;
while (curr_tick < target_tick) { while (curr_tick < target_tick) {
if (!bank_open) { if (!bank_open) {
return; return;
@@ -117,9 +125,11 @@ void customer_thread(
curr_tick += std::chrono::milliseconds(TIME_GRAN_MSEC); curr_tick += std::chrono::milliseconds(TIME_GRAN_MSEC);
std::this_thread::sleep_until(curr_tick); std::this_thread::sleep_until(curr_tick);
} }
// Wake up and push self into queue, then V(customer_num_sem) // Wake up and push self into queue, then V(customer_num_sem)
std::osyncstream(std::cout) << "Customer id " << info.id << " arrived at tick " << tick_count << std::endl; std::osyncstream(std::cout)
<< "Customer id " << info.id << " arrived at tick " << tick_count
<< std::endl;
{ {
std::lock_guard<std::mutex> lock(customer_q_mtx); std::lock_guard<std::mutex> lock(customer_q_mtx);
customer_q.push_back(cus); customer_q.push_back(cus);
@@ -180,14 +190,15 @@ int main(int argc, char *argv[]) {
for (auto cu : customer_infos) { for (auto cu : customer_infos) {
try { try {
customers.emplace_back(customer_thread, cu, start_tick); customers.emplace_back(customer_thread, cu, start_tick);
} catch (const std::system_error &e) { }
std::cerr << "Failed to create customer thread, id = " << cu.id << ": " catch (const std::system_error &e) {
<< e.what() << std::endl; std::cerr << "Failed to create customer thread, id = " << cu.id
<< ": " << e.what() << std::endl;
} }
} }
// Wait for all children to shutdown (by ctrl-c handler) // Wait for all children to shutdown (by ctrl-c handler)
for (auto &t: customers) { for (auto &t : customers) {
if (t.joinable()) { if (t.joinable()) {
t.join(); t.join();
} }

View File

@@ -17,7 +17,7 @@ struct ThreadArgs {
int high; int high;
}; };
void swap(int *a, int* b) { void swap(int *a, int *b) {
int temp = *b; int temp = *b;
*b = *a; *b = *a;
*a = temp; *a = temp;
@@ -25,7 +25,7 @@ void swap(int *a, int* b) {
// Returns the index of pivot in arr[]. // Returns the index of pivot in arr[].
int partition(int low, int high) { int partition(int low, int high) {
int pivot = numbers[high]; // Pivot element int pivot = numbers[high]; // Pivot element
int i = low - 1; int i = low - 1;
for (int j = low; j < high; j++) { for (int j = low; j < high; j++) {
@@ -51,7 +51,7 @@ void qsort_nothread(int low, int high) {
} }
void *qsort_thread(void *arg) { void *qsort_thread(void *arg) {
struct ThreadArgs* args = (struct ThreadArgs*) arg; struct ThreadArgs *args = (struct ThreadArgs *)arg;
// printf("Low: %d, high: %d\n", args->low, args->high); // printf("Low: %d, high: %d\n", args->low, args->high);
if (args->high - args->low <= SINGLE_THREAD_SRTH) { if (args->high - args->low <= SINGLE_THREAD_SRTH) {
qsort_nothread(args->low, args->high); qsort_nothread(args->low, args->high);
@@ -59,14 +59,9 @@ void *qsort_thread(void *arg) {
} }
int pivot_idx = partition(args->low, args->high); int pivot_idx = partition(args->low, args->high);
struct ThreadArgs left_thd_arg = { struct ThreadArgs left_thd_arg = {.low = args->low, .high = pivot_idx - 1};
.low = args->low, struct ThreadArgs right_thd_arg = {.low = pivot_idx + 1,
.high = pivot_idx - 1 .high = args->high};
};
struct ThreadArgs right_thd_arg = {
.low = pivot_idx + 1,
.high = args->high
};
pthread_t left_handle, right_handle; pthread_t left_handle, right_handle;
pthread_create(&left_handle, NULL, qsort_thread, (void *)&left_thd_arg); pthread_create(&left_handle, NULL, qsort_thread, (void *)&left_thd_arg);
pthread_create(&right_handle, NULL, qsort_thread, (void *)&right_thd_arg); pthread_create(&right_handle, NULL, qsort_thread, (void *)&right_thd_arg);
@@ -77,14 +72,10 @@ void *qsort_thread(void *arg) {
// LST_MAIN_FUNC // LST_MAIN_FUNC
void do_qsort() { void do_qsort() {
struct ThreadArgs arg = { struct ThreadArgs arg = {.low = 0, .high = NUM_LENGTH - 1};
.low = 0,
.high = NUM_LENGTH - 1
};
qsort_thread(&arg); qsort_thread(&arg);
} }
int main() { int main() {
printf("Start reading file...\n"); printf("Start reading file...\n");
numbers = malloc(sizeof(int) * NUM_LENGTH); numbers = malloc(sizeof(int) * NUM_LENGTH);

View File

@@ -1,12 +1,12 @@
#include <iostream>
#include <thread>
#include <fstream>
#include <chrono> #include <chrono>
#include <fstream>
#include <iostream>
#include <syncstream> #include <syncstream>
#include <thread>
void writer() { void writer() {
std::osyncstream(std::cout) << "Writer started" << std::endl; std::osyncstream(std::cout) << "Writer started" << std::endl;
std::ofstream pipe_write("/dev/wendy_in"); std::ofstream pipe_write("/dev/wendy_in");
if (!pipe_write.is_open()) { if (!pipe_write.is_open()) {
std::cerr << "Cannot open pipe in" << std::endl; std::cerr << "Cannot open pipe in" << std::endl;

View File

@@ -73,7 +73,6 @@ static int mypipe_release(struct inode *inode, struct file *file) {
pdata.is_active = false; pdata.is_active = false;
mutex_unlock(&isactive_lock); mutex_unlock(&isactive_lock);
wake_up_interruptible(&read_queue); wake_up_interruptible(&read_queue);
} }
return 0; return 0;
} }
@@ -98,7 +97,7 @@ static ssize_t mypipe_read(struct file *filep, char __user *buf, size_t size,
// We currently have no data, but we can wait for possible new to write in // We currently have no data, but we can wait for possible new to write in
while (pdata.data_len == 0) { while (pdata.data_len == 0) {
mutex_unlock(&mypipe_lock); mutex_unlock(&mypipe_lock);
// Check if the writer is still active // Check if the writer is still active
if (mutex_lock_interruptible(&isactive_lock)) { if (mutex_lock_interruptible(&isactive_lock)) {
return -ERESTARTSYS; return -ERESTARTSYS;