多线程编程功能要求 1. 主进程从命令行接受三个参数pa, pb, pc(pa > pb > pc)。其中,pa表示线程A产生的有序数组的长度,pb表示线程B的除数,pc表示线程C的除数。 2. 生成并运行四个线程,各线程的工作如下: 线程A每隔15毫秒按序产生一个数(从1到pa),把它们放到长度不超过20的buffer数 组中。如果buffer已满,则需要等待空闲时再插入; 线程B每隔20毫秒查询一次buffer,统计所有能被pb整除的数字的个数并求和; 线程C每隔15毫秒查询一次buffer,统计所有能被pc整除的数字的个数并求和; 线程D每隔10毫秒查询一次buffer,统计所有其他数字的个数并求和; 3. 主进程分别打印出进程B,C,D的数字个数与各自的求和。格式为Thread [id],[count], [sum]。例如: Thread B, 20, 1050 Thread C, 33, 1683 Thread D, 53, 2632 以下是代码框架main.c,请在以下基础框架下进行补充完整。 #include <stdio.h> #include <pthread.h> #define BUFFER_SIZE 20 int buffer[BUFFER_SIZE]; int main(int argc, char* argv[]){ if (argc != 4) { fprintf(stderr, "usage: main <pa> <pb> <pc>\n"); } // ... } void *threadA(...){} void *threadBC(...){} void *threadD(...){}
时间: 2023-06-20 14:08:20 浏览: 85
以下是完整的多线程编程代码,已经注释说明了每个部分的功能:
```c
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 20
int buffer[BUFFER_SIZE]; // 共享缓冲区
int pa, pb, pc; // 命令行参数
int countB = 0, countC = 0, countD = 0; // 统计线程B、C、D处理的数字个数
int sumB = 0, sumC = 0, sumD = 0; // 统计线程B、C、D处理的数字总和
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁,保护共享缓冲区
void *threadA(void *arg){
int i;
for(i=1; i<=pa; i++){
// 等待空闲缓冲区
while(1){
pthread_mutex_lock(&mutex);
if(buffer[BUFFER_SIZE-1] == 0){
break;
}
pthread_mutex_unlock(&mutex);
usleep(1000); // 睡眠1毫秒
}
// 插入新数据
int j;
for(j=0; j<BUFFER_SIZE-1; j++){
if(buffer[j] == 0){
buffer[j] = i;
break;
}
}
pthread_mutex_unlock(&mutex);
usleep(15000); // 睡眠15毫秒
}
return NULL;
}
void *threadBC(void *arg){
int *divisor = (int*)arg;
int i;
while(1){
// 查询缓冲区
pthread_mutex_lock(&mutex);
int sum = 0;
for(i=0; i<BUFFER_SIZE-1; i++){
if(buffer[i] == 0){
break;
}
if(buffer[i] % (*divisor) == 0){
sumB++;
sum += buffer[i];
}
else if(*divisor == pb){
countD++;
sumD += buffer[i];
}
else{
countC++;
sumC += buffer[i];
}
buffer[i] = 0;
}
pthread_mutex_unlock(&mutex);
usleep(1000); // 睡眠1毫秒
// 打印结果
if(*divisor == pb){
printf("Thread B, %d, %d\n", countB, sumB);
}
else if(*divisor == pc){
printf("Thread C, %d, %d\n", countC, sumC);
}
else{
printf("Thread D, %d, %d\n", countD, sumD);
}
}
return NULL;
}
int main(int argc, char* argv[]){
// 解析命令行参数
if (argc != 4){
fprintf(stderr, "usage: main <pa> <pb> <pc>\n");
return -1;
}
pa = atoi(argv[1]);
pb = atoi(argv[2]);
pc = atoi(argv[3]);
// 创建线程A
pthread_t thread_a;
pthread_create(&thread_a, NULL, threadA, NULL);
// 创建线程B、C、D
pthread_t thread_b, thread_c, thread_d;
pthread_create(&thread_b, NULL, threadBC, &pb);
pthread_create(&thread_c, NULL, threadBC, &pc);
pthread_create(&thread_d, NULL, threadBC, NULL);
// 等待线程A结束
pthread_join(thread_a, NULL);
// 结束线程B、C、D
pthread_cancel(thread_b);
pthread_cancel(thread_c);
pthread_cancel(thread_d);
return 0;
}
```
注意:线程B、C、D是无限循环的,需要使用 `pthread_cancel` 函数来结束它们。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)