多线程编程功能要求 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 11:08:04 浏览: 79
以下是完整代码:
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 20
int buffer[BUFFER_SIZE];
int buffer_count = 0;
int pa, pb, pc;
pthread_mutex_t buffer_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t buffer_not_full = PTHREAD_COND_INITIALIZER;
pthread_cond_t buffer_not_empty = PTHREAD_COND_INITIALIZER;
void *threadA(void *arg){
int i;
for(i=1; i<=pa; i++){
pthread_mutex_lock(&buffer_lock);
while(buffer_count == BUFFER_SIZE){
pthread_cond_wait(&buffer_not_full, &buffer_lock);
}
buffer[buffer_count++] = i;
printf("Thread A produce %d\n", i);
pthread_cond_signal(&buffer_not_empty);
pthread_mutex_unlock(&buffer_lock);
usleep(15000);
}
return NULL;
}
void *threadB(void *arg){
int i, sum=0, count=0;
while(1){
pthread_mutex_lock(&buffer_lock);
while(buffer_count == 0){
pthread_cond_wait(&buffer_not_empty, &buffer_lock);
}
for(i=0; i<buffer_count; i++){
if(buffer[i] % pb == 0){
count++;
sum += buffer[i];
}
}
pthread_cond_signal(&buffer_not_full);
pthread_mutex_unlock(&buffer_lock);
printf("Thread B, %d, %d\n", count, sum);
usleep(20000);
}
return NULL;
}
void *threadC(void *arg){
int i, sum=0, count=0;
while(1){
pthread_mutex_lock(&buffer_lock);
while(buffer_count == 0){
pthread_cond_wait(&buffer_not_empty, &buffer_lock);
}
for(i=0; i<buffer_count; i++){
if(buffer[i] % pc == 0){
count++;
sum += buffer[i];
}
}
pthread_cond_signal(&buffer_not_full);
pthread_mutex_unlock(&buffer_lock);
printf("Thread C, %d, %d\n", count, sum);
usleep(15000);
}
return NULL;
}
void *threadD(void *arg){
int i, sum=0, count=0;
while(1){
pthread_mutex_lock(&buffer_lock);
while(buffer_count == 0){
pthread_cond_wait(&buffer_not_empty, &buffer_lock);
}
for(i=0; i<buffer_count; i++){
if(buffer[i] % pb != 0 && buffer[i] % pc != 0){
count++;
sum += buffer[i];
}
}
pthread_cond_signal(&buffer_not_full);
pthread_mutex_unlock(&buffer_lock);
printf("Thread D, %d, %d\n", count, sum);
usleep(10000);
}
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]);
pthread_t thread_a, thread_b, thread_c, thread_d;
pthread_create(&thread_a, NULL, threadA, NULL);
pthread_create(&thread_b, NULL, threadB, NULL);
pthread_create(&thread_c, NULL, threadC, NULL);
pthread_create(&thread_d, NULL, threadD, NULL);
pthread_join(thread_a, NULL);
pthread_join(thread_b, NULL);
pthread_join(thread_c, NULL);
pthread_join(thread_d, NULL);
return 0;
}
阅读全文