refactor code
This commit is contained in:
@@ -36,7 +36,6 @@ std::deque<Customer> customer_q;
|
||||
std::counting_semaphore<100> customer_num_sem{0};
|
||||
// END_LST_COUNT_SEM
|
||||
|
||||
|
||||
void signal_handler(int signum) {
|
||||
// std::cout << "\nReceived signal " << signum << ", stopping...\n";
|
||||
write(STDOUT_FILENO, "\nCaught SIGINT, shutting down...\n", 33);
|
||||
@@ -59,11 +58,17 @@ void teller_thread(
|
||||
(curr_tick - start_serving_time) / TIME_GRAN_MSEC))
|
||||
.count() <= curr_customer.service_time) {
|
||||
// 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;
|
||||
} else {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@@ -86,7 +91,8 @@ void teller_thread(
|
||||
}
|
||||
} else {
|
||||
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:
|
||||
@@ -108,7 +114,9 @@ void customer_thread(
|
||||
|
||||
std::chrono::time_point curr_tick = start_tick;
|
||||
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) {
|
||||
if (!bank_open) {
|
||||
return;
|
||||
@@ -117,9 +125,11 @@ void customer_thread(
|
||||
curr_tick += std::chrono::milliseconds(TIME_GRAN_MSEC);
|
||||
std::this_thread::sleep_until(curr_tick);
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
customer_q.push_back(cus);
|
||||
@@ -180,14 +190,15 @@ int main(int argc, char *argv[]) {
|
||||
for (auto cu : customer_infos) {
|
||||
try {
|
||||
customers.emplace_back(customer_thread, cu, start_tick);
|
||||
} catch (const std::system_error &e) {
|
||||
std::cerr << "Failed to create customer thread, id = " << cu.id << ": "
|
||||
<< e.what() << std::endl;
|
||||
}
|
||||
catch (const std::system_error &e) {
|
||||
std::cerr << "Failed to create customer thread, id = " << cu.id
|
||||
<< ": " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for all children to shutdown (by ctrl-c handler)
|
||||
for (auto &t: customers) {
|
||||
for (auto &t : customers) {
|
||||
if (t.joinable()) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
23
lab2/qsort.c
23
lab2/qsort.c
@@ -17,7 +17,7 @@ struct ThreadArgs {
|
||||
int high;
|
||||
};
|
||||
|
||||
void swap(int *a, int* b) {
|
||||
void swap(int *a, int *b) {
|
||||
int temp = *b;
|
||||
*b = *a;
|
||||
*a = temp;
|
||||
@@ -25,7 +25,7 @@ void swap(int *a, int* b) {
|
||||
|
||||
// Returns the index of pivot in arr[].
|
||||
int partition(int low, int high) {
|
||||
int pivot = numbers[high]; // Pivot element
|
||||
int pivot = numbers[high]; // Pivot element
|
||||
int i = low - 1;
|
||||
|
||||
for (int j = low; j < high; j++) {
|
||||
@@ -51,7 +51,7 @@ void qsort_nothread(int low, int high) {
|
||||
}
|
||||
|
||||
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);
|
||||
if (args->high - args->low <= SINGLE_THREAD_SRTH) {
|
||||
qsort_nothread(args->low, args->high);
|
||||
@@ -59,14 +59,9 @@ void *qsort_thread(void *arg) {
|
||||
}
|
||||
|
||||
int pivot_idx = partition(args->low, args->high);
|
||||
struct ThreadArgs left_thd_arg = {
|
||||
.low = args->low,
|
||||
.high = pivot_idx - 1
|
||||
};
|
||||
struct ThreadArgs right_thd_arg = {
|
||||
.low = pivot_idx + 1,
|
||||
.high = args->high
|
||||
};
|
||||
struct ThreadArgs left_thd_arg = {.low = args->low, .high = pivot_idx - 1};
|
||||
struct ThreadArgs right_thd_arg = {.low = pivot_idx + 1,
|
||||
.high = args->high};
|
||||
pthread_t left_handle, right_handle;
|
||||
pthread_create(&left_handle, NULL, qsort_thread, (void *)&left_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
|
||||
void do_qsort() {
|
||||
struct ThreadArgs arg = {
|
||||
.low = 0,
|
||||
.high = NUM_LENGTH - 1
|
||||
};
|
||||
struct ThreadArgs arg = {.low = 0, .high = NUM_LENGTH - 1};
|
||||
qsort_thread(&arg);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("Start reading file...\n");
|
||||
numbers = malloc(sizeof(int) * NUM_LENGTH);
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <syncstream>
|
||||
#include <thread>
|
||||
|
||||
void writer() {
|
||||
std::osyncstream(std::cout) << "Writer started" << std::endl;
|
||||
|
||||
|
||||
std::ofstream pipe_write("/dev/wendy_in");
|
||||
if (!pipe_write.is_open()) {
|
||||
std::cerr << "Cannot open pipe in" << std::endl;
|
||||
|
||||
@@ -73,7 +73,6 @@ static int mypipe_release(struct inode *inode, struct file *file) {
|
||||
pdata.is_active = false;
|
||||
mutex_unlock(&isactive_lock);
|
||||
wake_up_interruptible(&read_queue);
|
||||
|
||||
}
|
||||
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
|
||||
while (pdata.data_len == 0) {
|
||||
mutex_unlock(&mypipe_lock);
|
||||
|
||||
|
||||
// Check if the writer is still active
|
||||
if (mutex_lock_interruptible(&isactive_lock)) {
|
||||
return -ERESTARTSYS;
|
||||
|
||||
Reference in New Issue
Block a user