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') 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