commit 1bbf813f3ddf94c659efeb1ff1b72d7ff6484103 Author: Yang Huiting <3552600665@qq.com> Date: Tue May 5 22:26:07 2026 +0800 初始化master主分支(最终提交版) diff --git a/shangji.cpp b/shangji.cpp new file mode 100644 index 0000000..51623ce --- /dev/null +++ b/shangji.cpp @@ -0,0 +1,225 @@ +#include +#include +#include +#include + +// ȫֱ +volatile int parking_spot_available = 1; +volatile int turn = 0; +volatile int flag[2] = { 0, 0 }; +volatile int success_count[3] = { 0, 0, 0 }; +volatile int cpu_usage[3] = { 0, 0, 0 }; // CPUռü +volatile int conflict_count = 0; + +// 㷨Aֵշ־ +DWORD WINAPI algorithm_a(LPVOID param) { + int thread_id = (int)param; + int attempts = 40; + for (int i = 0; i < attempts; i++) { + // æȴ - ȴֵԼ + while (turn != thread_id) { + cpu_usage[thread_id]++; // ¼CPUռ + } + // ٽ + if (parking_spot_available) { + Sleep(5); // ģʹóλ + success_count[thread_id]++; + parking_spot_available = 0; + parking_spot_available = 1; + } + // һ߳л + turn = (thread_id + 1) % 3; + Sleep(1); + } + return 0; +} + +// 㷨B˫־鷨 +DWORD WINAPI algorithm_b(LPVOID param) { + int thread_id = (int)param; + int attempts = 40; + int local_conflicts = 0; + for (int i = 0; i < attempts; i++) { + int wait_count = 0; + int max_wait = 10000; + // һԼı־ΪTRUE + flag[thread_id % 2] = 1; + // ڶ̵߳ı־ + int other_thread = (thread_id + 1) % 3; + while (flag[other_thread % 2] && wait_count < max_wait) { + cpu_usage[thread_id]++; // ¼CPUռ + wait_count++; + if (wait_count % 1000 == 0) { + Sleep(1); + } + } + if (wait_count >= max_wait) { + flag[thread_id % 2] = 0; + Sleep(10); + continue; + } + // ǷΥ + if (!parking_spot_available) { + local_conflicts++; + conflict_count++; + } + // ٽ + if (parking_spot_available) { + Sleep(5); + success_count[thread_id]++; + parking_spot_available = 0; + parking_spot_available = 1; + } + // 뿪ٽ + flag[thread_id % 2] = 0; + Sleep(1); + } + return 0; +} + +// 㷨CPeterson㷨 +DWORD WINAPI algorithm_c(LPVOID param) { + int thread_id = (int)param; + int attempts = 40; + if (thread_id == 2) { + // ߳ʹü򵥵ȴ + for (int i = 0; i < attempts; i++) { + while (!parking_spot_available) { + cpu_usage[thread_id]++; // ¼CPUռ + } + if (parking_spot_available) { + Sleep(5); + success_count[thread_id]++; + parking_spot_available = 0; + parking_spot_available = 1; + } + Sleep(1); + } + return 0; + } + // ߳01ʹPeterson㷨 + for (int i = 0; i < attempts; i++) { + flag[thread_id] = 1; + turn = 1 - thread_id; + while (flag[1 - thread_id] && turn == 1 - thread_id) { + cpu_usage[thread_id]++; // ¼CPUռ + } + if (parking_spot_available) { + Sleep(5); + success_count[thread_id]++; + parking_spot_available = 0; + parking_spot_available = 1; + } + flag[thread_id] = 0; + Sleep(1); + } + return 0; +} + +// Ժ +void test_algorithm(int algorithm_type) { + HANDLE threads[3]; + DWORD threadIDs[3]; + // ü + for (int i = 0; i < 3; i++) { + success_count[i] = 0; + cpu_usage[i] = 0; + } + parking_spot_available = 1; + turn = 0; + flag[0] = 0; + flag[1] = 0; + conflict_count = 0; + + printf("\n=== 㷨 %c ===\n", 'A' + algorithm_type - 1); + + // ߳ + for (int i = 0; i < 3; i++) { + switch (algorithm_type) { + case 1: + threads[i] = CreateThread(NULL, 0, algorithm_a, (LPVOID)i, 0, &threadIDs[i]); + break; + case 2: + threads[i] = CreateThread(NULL, 0, algorithm_b, (LPVOID)i, 0, &threadIDs[i]); + break; + case 3: + threads[i] = CreateThread(NULL, 0, algorithm_c, (LPVOID)i, 0, &threadIDs[i]); + break; + } + } + + // ȴ߳ + WaitForMultipleObjects(3, threads, TRUE, INFINITE); + + // + printf("ɹ:\n"); + printf(" ߳0: %d\n", success_count[0]); + printf(" ߳1: %d\n", success_count[1]); + printf(" ߳2: %d\n", success_count[2]); + printf(" ܼ: %d\n", success_count[0] + success_count[1] + success_count[2]); + + printf("\nCPUռ(æȴѭ):\n"); + printf(" ߳0: %d\n", cpu_usage[0]); + printf(" ߳1: %d\n", cpu_usage[1]); + printf(" ߳2: %d\n", cpu_usage[2]); + printf(" ܼ: %d\n", cpu_usage[0] + cpu_usage[1] + cpu_usage[2]); + + if (conflict_count > 0) { + printf("\n*** ⵽%dγͻΥ⣩ ***\n", conflict_count); + } + + // ر߳̾ + for (int i = 0; i < 3; i++) { + CloseHandle(threads[i]); + } +} + +int main() { + printf("=== λģʵ - ַæȴ㷨Ա ===\n"); + printf("ָ: ɹ & CPUռ(æȴѭ)\n"); + printf("ʵ: 3̣߳ÿ̳߳40Σܹ120\n"); + + // 㷨A + printf("\n㷨Aֵշ־"); + printf("\nص㣺ϸ֤⵫Чʵ\n"); + test_algorithm(1); + + // 㷨B + printf("\n㷨B˫־鷨"); + printf("\nص㣺Υ⣬\n"); + test_algorithm(2); + + // 㷨C + printf("\n㷨CPeterson㷨"); + printf("\nص㣺֤⣬\n"); + test_algorithm(3); + + // ʵ + printf("\n=== ʵ ===\n"); + printf("\n1. ȷԶԱȣǷΥ⣩:\n"); + printf(" - 㷨A: ֤⣬޳ͻ\n"); + printf(" - 㷨B: Υ⣬ֳͻ\n"); + printf(" - 㷨C: ֤⣬޳ͻ\n"); + + printf("\n2. ƽԶԱȣɹֲ:\n"); + printf(" - 㷨A: ϸƽ\n"); + printf(" - 㷨B: ֲּܳ\n"); + printf(" - 㷨C: Թƽ\n"); + + printf("\n3. CPU˷ѳ̶ȶԱȣæȴѭ:\n"); + printf(" - 㷨A: CPUռߣ˷\n"); + printf(" - 㷨B: CPUռеȣȷ\n"); + printf(" - 㷨C: CPUռԽϵͣЧʽϺ\n"); + + printf("\n4. ۺ:\n"); + printf(" - 㷨A: ȷԡ ƽԡ CPU˷\n"); + printf(" - 㷨B: ȷԡ ƽԡ CPU˷е\n"); + printf(" - 㷨C: ȷԡ ƽԡ CPU˷ѽϵ\n\n"); + + printf("\n5. ź/:\n"); + printf(" - ⷱæȴCPUռô\n"); + printf(" - ֤ȷԺ͹ƽ\n"); + printf(" - ִϵͳƼʹ\n"); + + return 0; +}