From d1bc1e2410ff187fe09d812057e441b1395a42a6 Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 21 Aug 2023 22:01:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day6/homework/h2.c | 28 ++++++++++++++ day6/homework/h3.c | 51 +++++++++++++++++++++++++ day6/homework/h4.c | 37 ++++++++++++++++++ day6/homework/h5.c | 50 +++++++++++++++++++++++++ day6/homework/h6.c | 74 ++++++++++++++++++++++++++++++++++++ day6/homework/h7.c | 83 +++++++++++++++++++++++++++++++++++++++++ day6/homework/test.c | 79 +++++++++++++++++++++++++++++++++++++++ day6/homework/tmp1.html | 2 + day6/homework/tmp2.html | 7 ++++ 9 files changed, 411 insertions(+) create mode 100644 day6/homework/h2.c create mode 100644 day6/homework/h3.c create mode 100644 day6/homework/h4.c create mode 100644 day6/homework/h5.c create mode 100644 day6/homework/h6.c create mode 100644 day6/homework/h7.c create mode 100644 day6/homework/test.c create mode 100644 day6/homework/tmp1.html create mode 100644 day6/homework/tmp2.html diff --git a/day6/homework/h2.c b/day6/homework/h2.c new file mode 100644 index 0000000..4a04c15 --- /dev/null +++ b/day6/homework/h2.c @@ -0,0 +1,28 @@ +// 编写一个程序, 开启二个线程, 第一个线程向终端输出A, 第二个线程向终端输出B, 每个线程打印10遍。 +#include +#include + +void *printA(void *data) +{ + for (int i = 0; i < 10; i++) + printf("A\n"); + return NULL; +} + +void *printB(void *data) +{ + for (int i = 0; i < 10; i++) + printf("B\n"); + return NULL; +} + +int main() +{ + pthread_t tid1, tid2; + pthread_create(&tid1, NULL, printA, NULL); + pthread_create(&tid2, NULL, printB, NULL); + pthread_join(tid1, NULL); + pthread_join(tid2, NULL); + + return 0; +} diff --git a/day6/homework/h3.c b/day6/homework/h3.c new file mode 100644 index 0000000..29f5bab --- /dev/null +++ b/day6/homework/h3.c @@ -0,0 +1,51 @@ +// 编写一个程序, 创建一个线程, 该线程计算并打印斐波那契数列的前n项, n由用户输入 +#include +#include +#include // atoi + +void *fibonacci(void *data) +{ + int n = *((int *)data); + + int a = 1, b = 1, c; + if (n >= 1) + { + c = a; + printf("%d\t", c); + } + + if (n >= 2) + { + c = b; + printf("%d\t", c); + } + + int temp = n - 2; + while (n > 2 && temp > 0) + { + c = a + b; + a = b; + b = c; + temp--; + printf("%d\t", c); + } + printf("\n"); + + return NULL; +} + +int main(int argc, char const **argv) +{ + if (argc != 2) + { + printf("用法: %s 数字\n", argv[0]); + return 0; + } + + int input = atoi(argv[1]); + + pthread_t tid; + pthread_create(&tid, NULL, fibonacci, (void *)&input); + pthread_join(tid, NULL); + return 0; +} \ No newline at end of file diff --git a/day6/homework/h4.c b/day6/homework/h4.c new file mode 100644 index 0000000..5e4dfd2 --- /dev/null +++ b/day6/homework/h4.c @@ -0,0 +1,37 @@ +// 编写一个程序, 创建两个线程, 一个线程打印奇数, 另一个线程打印偶数, 要求交替打印1到100的数字。 +#include +#include +#include + +void *printOdd(void *data) // 打印奇数 +{ + for (int i = 1; i <= 100; i++) + { + usleep(100 * 1000); // 睡眠 0.1 秒 + if (i % 2 == 1) + printf("奇数: %d\n", i); + } + return NULL; +} + +void *printEven(void *data) // 打印偶数 +{ + for (int i = 1; i <= 100; i++) + { + usleep(100 * 1000); // 睡眠 0.1 秒 + if (i % 2 == 0) + printf("偶数: %d\n", i); + } + + return NULL; +} + +int main(int argc, char const **argv) +{ + pthread_t tid1, tid2; + pthread_create(&tid1, NULL, printOdd, NULL); + pthread_create(&tid2, NULL, printEven, NULL); + pthread_join(tid1, NULL); + pthread_join(tid2, NULL); + return 0; +} \ No newline at end of file diff --git a/day6/homework/h5.c b/day6/homework/h5.c new file mode 100644 index 0000000..0129205 --- /dev/null +++ b/day6/homework/h5.c @@ -0,0 +1,50 @@ +// 编写一个程序, 创建一个线程, 该线程从标准输入读取字符串, 然后将字符串逆序输出。 +#include +#include +#include +#include + +void *reverseOrder(void *data) +{ + char *src_str = (char *)data; + int start = 0; + int end = strlen(src_str) - 1; + + // 动态分配内存来存储反序的字符串,确保在主函数中使用 pthread_join 获取线程的返回值时,该字符串仍然有效 + char *dst_str = (char *)malloc((strlen(src_str) + 1) * sizeof(char)); + strcpy(dst_str, src_str); + + while (start < end) + { + // 交换起始位置和末尾位置的字符 + char tmp = dst_str[start]; + dst_str[start] = dst_str[end]; + dst_str[end] = tmp; + + // 移动指针 + start++; + end--; + } + + pthread_exit(dst_str); +} + +int main(int argc, char const *argv[]) +{ + if (argc != 2) + { + printf("用法: %s 字符串\n", argv[0]); + return 0; + } + + pthread_t tid; + pthread_create(&tid, NULL, reverseOrder, (void *)argv[1]); + char *dst_str; + pthread_join(tid, (void **)&dst_str); + printf("字符串反序后为:\n%s\n", dst_str); + + // 释放动态分配的内存 + free(dst_str); + + return 0; +} \ No newline at end of file diff --git a/day6/homework/h6.c b/day6/homework/h6.c new file mode 100644 index 0000000..4b99aba --- /dev/null +++ b/day6/homework/h6.c @@ -0,0 +1,74 @@ +/* + 编写一个程序, 由主线程从键盘接收输入的网址(如http://www.baidu.com), 由子线程完成网址的下载并写入到tmpN.html临时文件中, 并在子线程中打印输出结果.输入exit时, 退出程序. + 【提示】基于execlp()函数执行`curl 网址 > tmp1.html`, 其中N是第几次请求. +*/ +#include +#include +#include +#include +#include + +typedef struct url_info_s +{ + char *url; + int N; +} URL_INFO; + +void *download(void *data) +{ + URL_INFO *info = (URL_INFO *)data; + char end_name[20]; + sprintf(end_name, "tmp%d.html", info->N); + // execlp("curl", "curl", info->url, "-o", end_name, NULL); + // 注意:这里使用 "-o" 参数指定输出文件名 + char command[200]; + sprintf(command, "curl %s -o %s", info->url, end_name); + system(command); + + sprintf(command, "cat %s", end_name); + system(command); + + return NULL; +} + +// void *view(void *data) +// { +// URL_INFO *info = (URL_INFO *)data; +// char end_name[20]; +// sprintf(end_name, "tmp%d.html", info->N); +// execlp("cat", "cat", end_name, NULL); +// // 使用 execlp 调用 cat 命令打印输出结果 + +// return NULL; +// } + +int main() +{ + int n = 1; // 初始化 n 的值为 1 + char buf[100]; + + while (1) + { + printf("请输入网址(输入exit退出):"); + fgets(buf, sizeof(buf), stdin); + buf[strlen(buf) - 1] = '\0'; + if (strcmp(buf, "exit") == 0) + { + break; + } + + URL_INFO urls; + urls.url = buf; + urls.N = n++; + + pthread_t download_tid, view_tid; + pthread_create(&download_tid, NULL, download, &urls); + pthread_join(download_tid, NULL); + + // pthread_create(&view_tid, NULL, view, &urls); + // pthread_join(view_tid, NULL); + } + + printf("---over---\n"); + return 0; +} diff --git a/day6/homework/h7.c b/day6/homework/h7.c new file mode 100644 index 0000000..cc891da --- /dev/null +++ b/day6/homework/h7.c @@ -0,0 +1,83 @@ +/* +编写一个程序,创建多个线程,每个线程负责对一个数组的一部分进行排序,最后将整个数组合并排序。 +【提示】线程接收的是一个结构体地址,结构体成员由数组元素指针、起始值和结束值组成。 +*/ +#include +#include +#include +#include + +typedef struct array_s +{ + int *arr; // 数组元素头指针 + int start_pos; // 起始位置 + int end_pos; // 结束位置 +} ARRAY; + +void *sort(void *data) +{ + ARRAY *array = (ARRAY *)data; + 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]; + } + } + } + + return NULL; +} + +int main() +{ + int arr[9] = {1, 4, 2, 9, 0, 5, 6, 1, 8}; + ARRAY array1 = {arr, 0, 2}; + ARRAY array2 = {arr, 3, 5}; + ARRAY array3 = {arr, 6, 8}; + + 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]; + } + + // 创建数组结构和新线程,将合并后的数组进行排序 + ARRAY arr4 = {merged, 0, 8}; + 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"); + + return 0; +} diff --git a/day6/homework/test.c b/day6/homework/test.c new file mode 100644 index 0000000..18632cf --- /dev/null +++ b/day6/homework/test.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +typedef struct array_s +{ + int *arr; // 数组元素头指针 + int start_pos; // 起始位置 + int end_pos; // 结束位置 +} ARRAY; + +void *sort(void *data) +{ + ARRAY *array = (ARRAY *)data; + 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]; + } + } + } + + return NULL; +} + +int main() +{ + int arr[9] = {1, 4, 2, 9, 0, 5, 6, 1, 8}; + ARRAY array1 = {arr, 0, 2}; + ARRAY array2 = {arr, 3, 5}; + ARRAY array3 = {arr, 6, 8}; + + 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]; + } + + // 创建数组结构和新线程,将合并后的数组进行排序 + ARRAY arr4 = {merged, 0, 8}; + 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"); + + return 0; +} diff --git a/day6/homework/tmp1.html b/day6/homework/tmp1.html new file mode 100644 index 0000000..2cbc927 --- /dev/null +++ b/day6/homework/tmp1.html @@ -0,0 +1,2 @@ + + 百度一下,你就知道

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

diff --git a/day6/homework/tmp2.html b/day6/homework/tmp2.html new file mode 100644 index 0000000..9ebb46b --- /dev/null +++ b/day6/homework/tmp2.html @@ -0,0 +1,7 @@ + +301 Moved Permanently + +

301 Moved Permanently

+
nginx/1.18.0 (Ubuntu)
+ +