diff --git a/day7/homework/h1.c b/day7/homework/h1.c new file mode 100644 index 0000000..63ed4dd --- /dev/null +++ b/day7/homework/h1.c @@ -0,0 +1,58 @@ +// 编写一个程序, 创建两个线程, 一个线程负责打印奇数, 另一个线程负责打印偶数, 要求打印的结果为1、2、3、4、5、6..., 直到指定的最大值。 +#include +#include +#include + +void *printOdd(void *data) // 打印奇数 +{ + int *n = (int *)data; + for (int i = 1; i <= *n; i++) + { + usleep(100 * 1000); // 睡眠 0.1 秒 + if (i % 2 == 1) + { + printf("%d", i); + if (i != *n) // 不是最后一个字符,则追加'、' + printf("、"); + else if (i == *n) // 到最后一个字符,则追加'\n'换行 + printf("\n"); + fflush(stdout); + } + } + return NULL; +} + +void *printEven(void *data) // 打印偶数 +{ + int *n = (int *)data; + for (int i = 1; i <= *n; i++) + { + usleep(100 * 1000); // 睡眠 0.1 秒 + if (i % 2 == 0) + { + printf("%d", i); + if (i != *n) + printf("、"); + else if (i == *n) + printf("\n"); + fflush(stdout); + } + } + + return NULL; +} + +int main(int argc, char const **argv) +{ + int *n; + printf("请输入最大值: "); + fflush(stdout); + scanf("%d", n); + + pthread_t tid1, tid2; + pthread_create(&tid1, NULL, printOdd, (void *)n); + pthread_create(&tid2, NULL, printEven, (void *)n); + pthread_join(tid1, NULL); + pthread_join(tid2, NULL); + return 0; +} \ No newline at end of file diff --git a/day7/homework/h2.c b/day7/homework/h2.c new file mode 100644 index 0000000..84f2ded --- /dev/null +++ b/day7/homework/h2.c @@ -0,0 +1,67 @@ +/* +编写一个程序, 创建两个线程, 一个线程负责向共享缓冲区写入数据, 另一个线程负责从缓冲区读取数据。要求实现线程间的同步和互斥, 确保读取线程只能在缓冲区有数据时才能读取, 写入线程只能在缓冲区为空时才能写入。 +【提示】缓冲区可以在main函数中创建char 数组, 并传入到线程中。 +*/ +#include +#include +#include +#include + +pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; +int buffer_empty = 1; // 缓冲区是否为空的标志,初始为空 + +void *w_thread(void *data) +{ + char *str = (char *)data; + + pthread_rwlock_wrlock(&rwlock); // 获取写入锁 + + while (!buffer_empty) // 当缓冲区非空时,释放锁,等待 1 毫秒再次尝试获取写入锁 + { + pthread_rwlock_unlock(&rwlock); // 释放写入锁 + usleep(1000); // 等待缓冲区为空 + pthread_rwlock_wrlock(&rwlock); // 再次获取写入锁 + } + printf("请输入内容:"); + fflush(stdout); // 刷新输出缓冲 + scanf("%s", str); + buffer_empty = 0; // 缓冲区非空 + + pthread_rwlock_unlock(&rwlock); // 释放写入锁 + + return NULL; +} + +void *r_thread(void *data) +{ + char *str = (char *)data; + + pthread_rwlock_rdlock(&rwlock); // 获取读取锁 + + while (buffer_empty) // 当缓冲区为空时,释放锁,等待1毫秒缓冲区非空后(等待写缓冲区线程写入内容),再次获取锁,直到判断非空,在进行后续打印 + { + pthread_rwlock_unlock(&rwlock); // 释放读取锁 + usleep(1000); // 等待缓冲区非空 + pthread_rwlock_rdlock(&rwlock); // 再次获取读取锁 + } + + printf("输入的内容为: %s\n", str); + buffer_empty = 1; // 缓冲区为空 + + pthread_rwlock_unlock(&rwlock); // 释放读取锁 + + return NULL; +} + +int main(int argc, char const *argv[]) +{ + char str[100]; + + pthread_t t1, t2; + pthread_create(&t1, NULL, w_thread, (void *)str); + pthread_create(&t2, NULL, r_thread, (void *)str); + pthread_join(t1, NULL); + pthread_join(t2, NULL); + + return 0; +} diff --git a/day7/homework/h2_2.c b/day7/homework/h2_2.c new file mode 100644 index 0000000..ebfcd6f --- /dev/null +++ b/day7/homework/h2_2.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t cond = PTHREAD_COND_INITIALIZER; +int buffer_empty = 1; // 缓冲区是否为空的标志,初始为空 + +void *w_thread(void *data) +{ + char *str = (char *)data; + + pthread_mutex_lock(&mutex); // 获取互斥锁 + + while (!buffer_empty) // 当缓冲区非空时,等待条件变量被唤醒 + { + pthread_cond_wait(&cond, &mutex); + } + + printf("请输入内容:"); + fflush(stdout); // 刷新输出缓冲 + scanf("%s", str); + buffer_empty = 0; // 缓冲区非空 + + pthread_cond_signal(&cond); // 唤醒等待的读取线程 + pthread_mutex_unlock(&mutex); // 释放互斥锁 + + return NULL; +} + +void *r_thread(void *data) +{ + char *str = (char *)data; + + pthread_mutex_lock(&mutex); // 获取互斥锁 + + while (buffer_empty) // 当缓冲区为空时,等待条件变量被唤醒 + { + pthread_cond_wait(&cond, &mutex); + } + + printf("输入的内容为: %s\n", str); + buffer_empty = 1; // 缓冲区为空 + + pthread_cond_signal(&cond); // 唤醒等待的写入线程 + pthread_mutex_unlock(&mutex); // 释放互斥锁 + + return NULL; +} + +int main(int argc, char const *argv[]) +{ + char str[100]; + + pthread_t t1, t2; + pthread_create(&t1, NULL, w_thread, (void *)str); + pthread_create(&t2, NULL, r_thread, (void *)str); + pthread_join(t1, NULL); + pthread_join(t2, NULL); + + return 0; +}