From cd00bb2f50ffd7d0362dc8d3dbac1b8952fa096b Mon Sep 17 00:00:00 2001 From: 423A35C7 <609514299@qq.com> Date: Mon, 10 Jun 2024 19:43:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AE=9E=E9=AA=8C7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- labcodes_answer/lab6_result/kern/process/proc.c | 3 + labcodes_answer/lab7_result/kern/process/proc.c | 4 +- .../lab7_result/kern/sync/read_write_sync.c | 76 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 labcodes_answer/lab7_result/kern/sync/read_write_sync.c diff --git a/labcodes_answer/lab6_result/kern/process/proc.c b/labcodes_answer/lab6_result/kern/process/proc.c index 994e77c..4eb1531 100644 --- a/labcodes_answer/lab6_result/kern/process/proc.c +++ b/labcodes_answer/lab6_result/kern/process/proc.c @@ -845,6 +845,9 @@ init_main(void *arg) { int i = 0; for (; i < 10; i++) { int pid = kernel_thread(my_test_user_main, (void *)i, 0); + if (pid <= 0) { + panic("create my_test_user_main failed.\n"); + } struct proc_strucht *proc = find_proc(pid); } diff --git a/labcodes_answer/lab7_result/kern/process/proc.c b/labcodes_answer/lab7_result/kern/process/proc.c index a726dda..4122c03 100644 --- a/labcodes_answer/lab7_result/kern/process/proc.c +++ b/labcodes_answer/lab7_result/kern/process/proc.c @@ -844,8 +844,8 @@ init_main(void *arg) { if (pid <= 0) { panic("create user_main failed.\n"); } - extern void check_sync(void); - check_sync(); // check philosopher sync problem + extern void read_write_sync(void); + read_write_sync(); // check philosopher sync problem while (do_wait(0, NULL) == 0) { schedule(); diff --git a/labcodes_answer/lab7_result/kern/sync/read_write_sync.c b/labcodes_answer/lab7_result/kern/sync/read_write_sync.c new file mode 100644 index 0000000..59cb60d --- /dev/null +++ b/labcodes_answer/lab7_result/kern/sync/read_write_sync.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include + +#define READER_NUM 100 // 读者数量,取值应大于1,小于int范围 +#define WRITER_NUM 100 // 写者数量,取值应大于1,小于int范围 +// #define BUFFER_NUM 10 // 缓冲区数量,取值应大于1,小于int范围 +#define SLEEP_TIME 10 + +// down和up、P和V 都实在是用不习惯,还是用acquire和release吧。 +#define acquire down +#define release up + +semaphore_t mutex; /* 临界区互斥 */ +semaphore_t synchronization; // 同步 +int read_pos_write_neg = 0; // 表示正在读取的读者数量 +int buffer; + +int reader(int index){ + lab6_set_priority(rand() % 100); + int result; + acquire(&mutex); + read_pos_write_neg++; + if (read_pos_write_neg == 1) + acquire(&synchronization); + release(&mutex); + do_sleep(rand() % SLEEP_TIME); // 模拟读延迟 + result = buffer; + acquire(&mutex); + read_pos_write_neg--; + assert(read_pos_write_neg >=0); + if (read_pos_write_neg == 0) { + release(&synchronization); + } + release(&mutex); + cprintf("reader %2d read %2d\n", index, result); + return result; +} + +void writer(int index) { + lab6_set_priority(rand() % 100); + int write_data = rand() % 100; + acquire(&synchronization); + do_sleep(rand() % SLEEP_TIME); // 模拟写延迟 + buffer = write_data; + release(&synchronization); + cprintf("writer %2d writed %2d\n", index, write_data); +} + +void read_write_sync() { + sem_init(&mutex, 1); + sem_init(&synchronization, 1); + int reader_num = 0, writer_num = 0; + while (reader_num < READER_NUM || writer_num < WRITER_NUM) { + if (rand() % 2) { + if (reader_num < READER_NUM) { + int pid = kernel_thread(reader, (void *)reader_num, 0); + if (pid <= 0) { + panic("create No.%d reader failed.\n"); + } + reader_num++; + } + } else { + if (writer_num < WRITER_NUM) { + int pid = kernel_thread(writer, (void *)writer_num, 0); + if (pid <= 0) { + panic("create No.%d writer failed.\n"); + } + writer_num++; + } + } + do_sleep(rand() % SLEEP_TIME); // 模拟入队延迟 + } +} \ No newline at end of file