linux用pthread_create创建一个线程并显示你的名字+学号,主线程计算0~20之间被4除整除的数之和并显示。
时间: 2024-06-13 17:07:28 浏览: 10
以下是你需要的代码实现:
```c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *thread(void *arg) {
printf("姓名+学号\n");
return NULL;
}
int main(void) {
pthread_t id;
int i, ret;
int sum = 0;
ret = pthread_create(&id, NULL, thread, NULL);
if (ret != 0) {
printf("error!\n");
exit(1);
}
for (i = 0; i <= 20; i++) {
if (i % 4 == 0) {
sum += i;
}
}
printf("%d\n", sum);
pthread_join(id, NULL);
return 0;
}
```
相关问题
linux用pthread_create创建一个线程并显示你的名字+学号,主线程计算0~20之间被4除整除的数之和并显示的步骤
以下是使用pthread_create创建一个线程并显示你的名字+学号,主线程计算0~20之间被4除整除的数之和并显示的步骤:
```c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *thread(void *arg) {
printf("姓名+学号\n");
return NULL;
}
int main(void) {
pthread_t id;
int i, ret;
int sum = 0;
ret = pthread_create(&id, NULL, thread, NULL);
if (ret != 0) {
printf("error!\n");
exit(1);
}
for (i = 0; i <= 20; i++) {
if (i % 4 == 0) {
sum += i;
}
}
printf("%d\n", sum);
pthread_join(id, NULL);
return 0;
}
```
用pthread_create创建一个线程并显示你的名字+学号;主线程计算0~20之间被4整除的数之和并显示
以下是使用pthread_create创建线程并显示名字和学号,主线程计算0~20之间被4整除的数之和并显示的代码:
```c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *thread(void *arg) {
printf("姓名+学号\n");
return NULL;
}
int main(void) {
pthread_t id;
int i, ret;
int sum = 0;
ret = pthread_create(&id, NULL, thread, NULL);
if (ret != 0) {
printf("error!\n");
exit(1);
}
for (i = 0; i <= 20; i++) {
if (i % 4 == 0) {
sum += i;
}
}
printf("%d\n", sum);
pthread_join(id, NULL);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)