From e9cfa972d7ffd09584b678b42cb60bf50b8efa92 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 11 Jun 2021 11:24:08 +0800 Subject: import(life): ... --- .../ParallelCalculationDemo.cpp | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 works/life/operating-system-experiment/ParallelCalculationDemo.cpp (limited to 'works/life/operating-system-experiment/ParallelCalculationDemo.cpp') diff --git a/works/life/operating-system-experiment/ParallelCalculationDemo.cpp b/works/life/operating-system-experiment/ParallelCalculationDemo.cpp new file mode 100644 index 0000000..31ff2bf --- /dev/null +++ b/works/life/operating-system-experiment/ParallelCalculationDemo.cpp @@ -0,0 +1,73 @@ +#include "Thread.h" + +#include + +const long long N = 1e9; + +int main(int argc, char **argv) { + int thread_number; + if (argc == 1) { + thread_number = 1; + } else if (argc == 2) { + thread_number = std::atoi(argv[1]); + if (thread_number <= 0) { + std::cerr << "Argument must be a positive integer.\n"; + } + } else { + std::cerr << "Too many arguments.\n"; + } + + std::cout << "Use " << thread_number << " threads to calculate sum of 1-" << N + << ".\n"; + + if (thread_number == 1) { + long long sum = 0; + for (long long i = 1; i <= 1e9; i++) { + sum += i; + } + std::cout << "Sum of 1-" << N << " is " << sum << '\n'; + } else { + std::vector threads(thread_number); + std::vector partial_sum(thread_number); + + long long step = N / thread_number; + + for (int i = 0; i < thread_number; i++) { + long long start = step * i; + long long end = step * (i + 1); + + long long &ps = partial_sum[i]; + + if (i == thread_number - 1) { + threads[i] = cru::Thread([&ps, start] { + long long sum = 0; + for (long long j = start; j <= 1e9; j++) { + sum += j; + } + ps = sum; + }); + } else { + threads[i] = cru::Thread([&ps, start, end] { + long long sum = 0; + for (int j = start; j < end; j++) { + sum += j; + } + ps = sum; + }); + } + } + + for (auto &thread : threads) { + thread.Join(); + } + + long long sum = 0; + for (auto ps : partial_sum) { + sum += ps; + } + + std::cout << "Sum of 1-" << N << " is " << sum << '\n'; + } + + return 0; +} -- cgit v1.2.3 From 2c7ceca3e58543c36098fd46de711d9cd80a4d12 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 11 Jun 2021 11:34:16 +0800 Subject: import(life): ... --- works/life/operating-system-experiment/ParallelCalculationDemo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'works/life/operating-system-experiment/ParallelCalculationDemo.cpp') diff --git a/works/life/operating-system-experiment/ParallelCalculationDemo.cpp b/works/life/operating-system-experiment/ParallelCalculationDemo.cpp index 31ff2bf..7340da8 100644 --- a/works/life/operating-system-experiment/ParallelCalculationDemo.cpp +++ b/works/life/operating-system-experiment/ParallelCalculationDemo.cpp @@ -41,7 +41,7 @@ int main(int argc, char **argv) { if (i == thread_number - 1) { threads[i] = cru::Thread([&ps, start] { long long sum = 0; - for (long long j = start; j <= 1e9; j++) { + for (long long j = start; j <= N; j++) { sum += j; } ps = sum; -- cgit v1.2.3 From dc0dfeec23158268268da5a16364abb1f0de5a06 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 12 Jun 2021 23:20:15 +0800 Subject: import(life): ... --- .../operating-system-experiment/CMakeLists.txt | 2 ++ .../DeadLockDetectionDemo.cpp | 25 ++++++++++++++++++++++ .../ParallelCalculationDemo.cpp | 2 ++ works/life/operating-system-experiment/Thread.h | 8 ------- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 works/life/operating-system-experiment/DeadLockDetectionDemo.cpp (limited to 'works/life/operating-system-experiment/ParallelCalculationDemo.cpp') diff --git a/works/life/operating-system-experiment/CMakeLists.txt b/works/life/operating-system-experiment/CMakeLists.txt index 5671b4a..79fe786 100644 --- a/works/life/operating-system-experiment/CMakeLists.txt +++ b/works/life/operating-system-experiment/CMakeLists.txt @@ -33,3 +33,5 @@ target_link_libraries(semaphore_avoid_data_race_demo PRIVATE cru_system) add_executable(parallel_calculation_demo ParallelCalculationDemo.cpp) target_link_libraries(parallel_calculation_demo PRIVATE cru_system) + +add_executable(dead_lock_detection_demo DeadLockDetectionDemo.cpp) diff --git a/works/life/operating-system-experiment/DeadLockDetectionDemo.cpp b/works/life/operating-system-experiment/DeadLockDetectionDemo.cpp new file mode 100644 index 0000000..b22549e --- /dev/null +++ b/works/life/operating-system-experiment/DeadLockDetectionDemo.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +int main() { + std::vector ns; + + while (!std::cin.eof() || std::cin) { + int n; + std::cin >> n; + ns.push_back(n); + } + + if (!std::cin.eof()) { + std::cerr << "Failed to parse input.\n"; + return -1; + } + + if (ns.size() % 2 != 0) { + std::cerr << "Input integer number must be even.\n"; + return -1; + } + + +} diff --git a/works/life/operating-system-experiment/ParallelCalculationDemo.cpp b/works/life/operating-system-experiment/ParallelCalculationDemo.cpp index 7340da8..0174c55 100644 --- a/works/life/operating-system-experiment/ParallelCalculationDemo.cpp +++ b/works/life/operating-system-experiment/ParallelCalculationDemo.cpp @@ -12,9 +12,11 @@ int main(int argc, char **argv) { thread_number = std::atoi(argv[1]); if (thread_number <= 0) { std::cerr << "Argument must be a positive integer.\n"; + return -1; } } else { std::cerr << "Too many arguments.\n"; + return -1; } std::cout << "Use " << thread_number << " threads to calculate sum of 1-" << N diff --git a/works/life/operating-system-experiment/Thread.h b/works/life/operating-system-experiment/Thread.h index 735407c..4ad1ef4 100644 --- a/works/life/operating-system-experiment/Thread.h +++ b/works/life/operating-system-experiment/Thread.h @@ -66,14 +66,6 @@ private: #endif }; -namespace details { -#ifdef CRU_WINDOWS -CRU_API DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter); -#else -void *ThreadProc(void *data); -#endif -} // namespace details - template Thread::Thread(Fn &&process, Args &&...args) { std::tuple...> a{std::forward(args)...}; -- cgit v1.2.3