From 721846a61dee4de4404433346a70a6fb23ffbe39 Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 22 Aug 2023 23:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E4=BB=BB=E5=8A=A1=E4=BA=92=E6=96=A5?= =?UTF-8?q?=E4=B8=8E=E5=90=8C=E6=AD=A5(=E4=BA=92=E6=96=A5=E9=94=81,=20?= =?UTF-8?q?=E8=AF=BB=E5=86=99=E9=94=81,=20=E6=9D=A1=E4=BB=B6=E5=8F=98?= =?UTF-8?q?=E9=87=8F,=20=E4=BF=A1=E5=8F=B7=E9=87=8F,=20=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E4=BF=A1=E5=8F=B7=E9=87=8F):=20=E4=BD=9C=E4=B8=9A1,2,3,4?= =?UTF-8?q?=E4=BF=AE=E8=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day7/homework/h1_2.c | 85 +++++++++++++++++++++++++++++++++++++++ day7/homework/h2.c | 2 + day7/homework/h2_2.c | 3 ++ day7/homework/h3.c | 2 +- day7/homework/h4.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ day7/homework/test.c | 58 +++++++++++++++++++++++++++ 6 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 day7/homework/h1_2.c create mode 100644 day7/homework/h4.c create mode 100644 day7/homework/test.c diff --git a/day7/homework/h1_2.c b/day7/homework/h1_2.c new file mode 100644 index 0000000..1717ffb --- /dev/null +++ b/day7/homework/h1_2.c @@ -0,0 +1,85 @@ +#include +#include + +pthread_mutex_t mutex; +pthread_cond_t condition; +int max_number; +int current_number; + +void *print_odd(void *arg) +{ + while (1) + { + pthread_mutex_lock(&mutex); + while (current_number % 2 == 0) + { // 等待偶数线程通知 + pthread_cond_wait(&condition, &mutex); + } + if (current_number > max_number) + { // 达到最大值时退出 + pthread_mutex_unlock(&mutex); + break; + } + printf("%d", current_number); + if (current_number != max_number) + printf("、"); + else if (current_number == max_number) + printf("\n"); + fflush(stdout); + + current_number++; + pthread_cond_signal(&condition); // 通知偶数线程 + pthread_mutex_unlock(&mutex); + } + return NULL; +} + +void *print_even(void *arg) +{ + while (1) + { + pthread_mutex_lock(&mutex); + while (current_number % 2 != 0) + { // 等待奇数线程通知 + pthread_cond_wait(&condition, &mutex); + } + if (current_number > max_number) + { // 达到最大值时退出 + pthread_mutex_unlock(&mutex); + break; + } + printf("%d", current_number); + if (current_number != max_number) + printf("、"); + else if (current_number == max_number) + printf("\n"); + fflush(stdout); + + current_number++; + pthread_cond_signal(&condition); // 通知奇数线程 + pthread_mutex_unlock(&mutex); + } + return NULL; +} + +int main() +{ + printf("请输入指定的最大值: "); + scanf("%d", &max_number); + + pthread_mutex_init(&mutex, NULL); + pthread_cond_init(&condition, NULL); + current_number = 1; + + pthread_t odd_thread, even_thread; + pthread_create(&odd_thread, NULL, print_odd, NULL); + pthread_create(&even_thread, NULL, print_even, NULL); + + pthread_join(odd_thread, NULL); + pthread_join(even_thread, NULL); + + pthread_mutex_destroy(&mutex); + pthread_cond_destroy(&condition); + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h2.c b/day7/homework/h2.c index 84f2ded..3afbab0 100644 --- a/day7/homework/h2.c +++ b/day7/homework/h2.c @@ -63,5 +63,7 @@ int main(int argc, char const *argv[]) pthread_join(t1, NULL); pthread_join(t2, NULL); + pthread_rwlock_destroy(&rwlock); // 销毁读写锁 + return 0; } diff --git a/day7/homework/h2_2.c b/day7/homework/h2_2.c index ebfcd6f..fa083c4 100644 --- a/day7/homework/h2_2.c +++ b/day7/homework/h2_2.c @@ -59,5 +59,8 @@ int main(int argc, char const *argv[]) pthread_join(t1, NULL); pthread_join(t2, NULL); + pthread_cond_destroy(&cond); // 销毁条件变量 + pthread_mutex_destroy(&mutex); // 销毁互斥锁 + return 0; } diff --git a/day7/homework/h3.c b/day7/homework/h3.c index 7b570f5..f8f0255 100644 --- a/day7/homework/h3.c +++ b/day7/homework/h3.c @@ -36,7 +36,7 @@ int main(int argc, char const *argv[]) printf("现在的 number = %d\n", number); - sem_destroy(&sem); + sem_destroy(&sem); // 销毁信号量 return 0; } diff --git a/day7/homework/h4.c b/day7/homework/h4.c new file mode 100644 index 0000000..1b66e55 --- /dev/null +++ b/day7/homework/h4.c @@ -0,0 +1,95 @@ +/* +编写一个程序, 创建多个线程, 每个线程负责对一个共享数组的不同部分进行排序。要求实现线程间的同步, 确保每个线程在进行排序时不会干扰其他线程的工作。 +*/ +#include +#include +#include +#include + +typedef struct array_s +{ + int *arr; // 数组元素头指针 + int start_pos; // 起始位置 + int end_pos; // 结束位置 + pthread_mutex_t *mutex; // 互斥锁 +} ARRAY; + +void *sort(void *data) +{ + ARRAY *array = (ARRAY *)data; + pthread_mutex_lock(array->mutex); // 线程加锁 + printf("%ld 线程开始排序\n", pthread_self()); + + for (int i = array->start_pos; i < array->end_pos; i++) + { + for (int j = array->start_pos; j < array->end_pos - i; j++) + { + if (array->arr[j] > array->arr[j + 1]) + { + array->arr[j] ^= array->arr[j + 1]; + array->arr[j + 1] ^= array->arr[j]; + array->arr[j] ^= array->arr[j + 1]; + } + } + } + + pthread_mutex_unlock(array->mutex); // 线程解锁 + + return NULL; +} + +int main() +{ + int arr[9] = {1, 4, 2, 9, 0, 5, 6, 1, 8}; + pthread_mutex_t mutex; + pthread_mutex_init(&mutex, NULL); + + ARRAY array1 = {arr, 0, 2, &mutex}; + ARRAY array2 = {arr, 3, 5, &mutex}; + ARRAY array3 = {arr, 6, 8, &mutex}; + + pthread_t t1, t2, t3; + pthread_create(&t1, NULL, sort, &array1); + pthread_create(&t2, NULL, sort, &array2); + pthread_create(&t3, NULL, sort, &array3); + + pthread_join(t1, NULL); + pthread_join(t2, NULL); + pthread_join(t3, NULL); + + // 合并排序结果 + int merged[9]; + int i = 0; + for (int j = array1.start_pos; j <= array1.end_pos; j++) + { + merged[i++] = arr[j]; + } + for (int j = array2.start_pos; j <= array2.end_pos; j++) + { + merged[i++] = arr[j]; + } + for (int j = array3.start_pos; j <= array3.end_pos; j++) + { + merged[i++] = arr[j]; + } + + pthread_mutex_unlock(&mutex); // 解锁 + + // 创建数组结构和新线程,将合并后的数组进行排序 + ARRAY arr4 = {merged, 0, 8, &mutex}; + pthread_t t4; + pthread_create(&t4, NULL, sort, &arr4); + pthread_join(t4, NULL); + + // 输出排序结果 + printf("排序后的数组:"); + for (int i = 0; i < 9; i++) + { + printf("%d ", merged[i]); + } + printf("\n"); + + pthread_mutex_destroy(&mutex); // 销毁互斥锁 + + return 0; +} diff --git a/day7/homework/test.c b/day7/homework/test.c new file mode 100644 index 0000000..1ed697c --- /dev/null +++ b/day7/homework/test.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +//设置办理业务的人数 +int num = 5; +//创建信号量 +sem_t sem; +//模拟办理业务的过程 +void *get_service(void *arg) +{ + int id = *((int *)arg); + //信号量成功“减 1”后才能继续执行 + if (sem_wait(&sem) == 0) + { + printf("---customer%d 正在办理业务\n", id); + sleep(2); + printf("---customer%d 已办完业务\n", id); + //信号量“加 1” + sem_post(&sem); + } + return 0; +} + +int main() +{ + int flag, i, j; + //创建 5 个线程代表 5 个人 + pthread_t customer[5]; + //初始化信号量 + sem_init(&sem, 0, 2); + for (i = 0; i < num; i++) + { + flag = pthread_create(&customer[i], NULL, get_service, &i); + if (flag != 0) + { + printf("线程创建失败!\n"); + return 0; + } + else + { + printf("customer%d 来办理业务\n", i); + } + sleep(1); + } + + for (j = 0; j < num; j++) + { + flag = pthread_join(customer[j], NULL); + if (flag != 0) + { + printf("tid=%d 等待失败!", customer[i]); + return 0; + } + } + sem_destroy(&sem); + return 0; +} \ No newline at end of file