From ec72d12002c6d3b707a014645c0da1b397dc7ee4 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 17 Mar 2021 17:15:19 +0800 Subject: import(life): Add OS challenges. --- works/life/operating-system-challenge/1/README.md | 10 +++++++ works/life/operating-system-challenge/1/effect.png | Bin 0 -> 136979 bytes works/life/operating-system-challenge/1/main.cpp | 33 +++++++++++++++++++++ works/life/operating-system-challenge/1/sample.jpg | Bin 0 -> 125777 bytes 4 files changed, 43 insertions(+) create mode 100644 works/life/operating-system-challenge/1/README.md create mode 100644 works/life/operating-system-challenge/1/effect.png create mode 100644 works/life/operating-system-challenge/1/main.cpp create mode 100644 works/life/operating-system-challenge/1/sample.jpg (limited to 'works/life/operating-system-challenge/1') diff --git a/works/life/operating-system-challenge/1/README.md b/works/life/operating-system-challenge/1/README.md new file mode 100644 index 0000000..2088658 --- /dev/null +++ b/works/life/operating-system-challenge/1/README.md @@ -0,0 +1,10 @@ +> 挑战一:写一个程序,运行该进程时,你的电脑整体 CPU 利用率保持在 100% +> 示例效果如 +> ![sample](sample.jpg) + +Transaltion: Write a program and make your CPU's usage rate 100%. + +Code is in `main.cpp`. Remember to not compile it with O2 optimization, or my dead cycle will be eliminated. + +Final effect: +![effect](effect.png) diff --git a/works/life/operating-system-challenge/1/effect.png b/works/life/operating-system-challenge/1/effect.png new file mode 100644 index 0000000..b173ea0 Binary files /dev/null and b/works/life/operating-system-challenge/1/effect.png differ diff --git a/works/life/operating-system-challenge/1/main.cpp b/works/life/operating-system-challenge/1/main.cpp new file mode 100644 index 0000000..0f780ef --- /dev/null +++ b/works/life/operating-system-challenge/1/main.cpp @@ -0,0 +1,33 @@ +// Remember not to use O2 optimization! + +#include +#include + +int main() { + std::vector threads; + + // Because my CPU has 4-core and 8-threads. I open 8 threads. + for (int i = 0; i < 8; i++) { + std::thread t([] { + int i; + int j; + int k; + double d; + while (true) { + i = i + i; + j = j - j; + k = k * k; + d = d + d; + i = i & i; + j = j | j; + } + }); + threads.push_back(std::move(t)); + } + + for (auto &t : threads) { + t.join(); + } + + return 0; +} diff --git a/works/life/operating-system-challenge/1/sample.jpg b/works/life/operating-system-challenge/1/sample.jpg new file mode 100644 index 0000000..7276bec Binary files /dev/null and b/works/life/operating-system-challenge/1/sample.jpg differ -- cgit v1.2.3 From ac32eebb6cad203e7a45834d3dc90b639e3d6ed3 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 19 Mar 2021 14:27:01 +0800 Subject: import(life): Finish OS challenge 2. --- works/life/operating-system-challenge/1/README.md | 2 +- works/life/operating-system-challenge/2/README.md | 5 ++ works/life/operating-system-challenge/2/effect.png | Bin 0 -> 179599 bytes works/life/operating-system-challenge/2/main.cpp | 73 +++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 works/life/operating-system-challenge/2/effect.png create mode 100644 works/life/operating-system-challenge/2/main.cpp (limited to 'works/life/operating-system-challenge/1') diff --git a/works/life/operating-system-challenge/1/README.md b/works/life/operating-system-challenge/1/README.md index 2088658..db7b627 100644 --- a/works/life/operating-system-challenge/1/README.md +++ b/works/life/operating-system-challenge/1/README.md @@ -4,7 +4,7 @@ Transaltion: Write a program and make your CPU's usage rate 100%. -Code is in `main.cpp`. Remember to not compile it with O2 optimization, or my dead cycle will be eliminated. +Code is in `main.cpp`. Remember to not compile it with O2 optimization, or my dead loop will be eliminated. Final effect: ![effect](effect.png) diff --git a/works/life/operating-system-challenge/2/README.md b/works/life/operating-system-challenge/2/README.md index e8316ad..86672f5 100644 --- a/works/life/operating-system-challenge/2/README.md +++ b/works/life/operating-system-challenge/2/README.md @@ -3,3 +3,8 @@ > ![sample](sample.jpg) Translation: Write a program and make each core of your CPU get a different but regular usage rate graph. + +Code is in `main.cpp`. Remember to not compile it with O2 optimization, or my loop will be eliminated. + +Final effect: +![effect](effect.png) diff --git a/works/life/operating-system-challenge/2/effect.png b/works/life/operating-system-challenge/2/effect.png new file mode 100644 index 0000000..984a3da Binary files /dev/null and b/works/life/operating-system-challenge/2/effect.png differ diff --git a/works/life/operating-system-challenge/2/main.cpp b/works/life/operating-system-challenge/2/main.cpp new file mode 100644 index 0000000..a497442 --- /dev/null +++ b/works/life/operating-system-challenge/2/main.cpp @@ -0,0 +1,73 @@ +#include +#include + +DWORD WINAPI Thread1(_In_ LPVOID lpParameter) { + int i; + while (true) { + i++; + } + return 0; +} + +DWORD WINAPI Thread2(_In_ LPVOID lpParameter) { + while (true) { + for (long long i = 0; i < 5000000000; i++) + ; + Sleep(10000); + } + + return 0; +} + +DWORD WINAPI Thread3(_In_ LPVOID lpParameter) { + while (true) { + for (int i = 0; i < 4000000; i++) + ; + Sleep(10); + } + + return 0; +} + +DWORD WINAPI Thread4(_In_ LPVOID lpParameter) { + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + + LARGE_INTEGER count; + QueryPerformanceCounter(&count); + + double current_y = 0; + + while (true) { + for (int i = 0; i < 3000000.0 * (1.0 + current_y); i++) + ; + Sleep(10 * (1 - current_y)); + + LARGE_INTEGER new_count; + QueryPerformanceCounter(&new_count); + + auto s = (new_count.QuadPart - count.QuadPart) / frequency.QuadPart; + current_y = std::sin(s / 30.0 * 2 * 3.1415926535) * 0.6; + } + + return 0; +} + +int main() { + HANDLE thread1 = CreateThread(NULL, 0, Thread1, NULL, 0, NULL); + HANDLE thread2 = CreateThread(NULL, 0, Thread2, NULL, 0, NULL); + HANDLE thread3 = CreateThread(NULL, 0, Thread3, NULL, 0, NULL); + HANDLE thread4 = CreateThread(NULL, 0, Thread4, NULL, 0, NULL); + + SetThreadAffinityMask(thread1, 1); + SetThreadAffinityMask(thread2, 1 << 1); + SetThreadAffinityMask(thread3, 1 << 2); + SetThreadAffinityMask(thread4, 1 << 3); + + WaitForSingleObject(thread1, INFINITE); + WaitForSingleObject(thread2, INFINITE); + WaitForSingleObject(thread3, INFINITE); + WaitForSingleObject(thread4, INFINITE); + + return 0; +} -- cgit v1.2.3