线程作业

This commit is contained in:
flykhan 2023-08-21 22:01:14 +08:00
parent 8df60e2dc6
commit d1bc1e2410
9 changed files with 411 additions and 0 deletions

28
day6/homework/h2.c Normal file
View File

@ -0,0 +1,28 @@
// 编写一个程序, 开启二个线程, 第一个线程向终端输出A, 第二个线程向终端输出B, 每个线程打印10遍。
#include <stdio.h>
#include <pthread.h>
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;
}

51
day6/homework/h3.c Normal file
View File

@ -0,0 +1,51 @@
// 编写一个程序, 创建一个线程, 该线程计算并打印斐波那契数列的前n项, n由用户输入
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h> // 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;
}

37
day6/homework/h4.c Normal file
View File

@ -0,0 +1,37 @@
// 编写一个程序, 创建两个线程, 一个线程打印奇数, 另一个线程打印偶数, 要求交替打印1到100的数字。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
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;
}

50
day6/homework/h5.c Normal file
View File

@ -0,0 +1,50 @@
// 编写一个程序, 创建一个线程, 该线程从标准输入读取字符串, 然后将字符串逆序输出。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
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;
}

74
day6/homework/h6.c Normal file
View File

@ -0,0 +1,74 @@
/*
, 线http://www.baidu.com, 由子线程完成网址的下载并写入到tmpN.html临时文件中, 并在子线程中打印输出结果.输入exit时, 退出程序.
execlp()`curl > tmp1.html`, N是第几次请求.
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
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;
}

83
day6/homework/h7.c Normal file
View File

@ -0,0 +1,83 @@
/*
线线
线
*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
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;
}

79
day6/homework/test.c Normal file
View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
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;
}

2
day6/homework/tmp1.html Normal file
View File

@ -0,0 +1,2 @@
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

7
day6/homework/tmp2.html Normal file
View File

@ -0,0 +1,7 @@
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>