我通常是 C 新手,所以我有很多语法错误和引用问题。我已经编写了 prodcons.c,这是一个 Struct,它将创建一个大小为 50 的有界缓冲区和一些辅助变量。在 main 中,我创建了这个数据结构的变量并对其进行初始化。我试图将任意数量的参数(IE {1..1000})说成数千个并将它们推入缓冲区,同时另一个 C 文件 producer.c 正试图从同一个缓冲区中弹出我通过线程调用共享给它。PUSH 和 POP 都有一个互斥锁来同时停止对关键部分的任何更改。然后,生产者线程将初始化另一个数据结构,从其共享缓冲区中的 main 获取每个参数的主要因素,与线程中的 Consumer.c 函数调用共享一个新的初始化数据结构。消费者只需打印数字及其主要因素。我相信我的所有文件中都必须有#include prodcons.h 才能使用它的功能,但如果我错了,请纠正我。另外,我正在尝试创建 BUSY WAITNG,main 和 producer 每次都尝试使用 while 循环到达关键部分,调用 POP 和 PUSH,但这很可能是错误的。请帮助,建议,语法。任何事物。
老实说,我只是尝试编译代码,但是我的引用和指针以及 C 中共享文件的任何正确方法以及对数据结构的指针调用都已关闭。我想我只是需要很多更正。比如当使用结构指针时,我是使用 pc->function() 还是 pc.function()?这些小的语法错误让我陷入了困境。我正在通过 consumer.h 和 consumer.c 分享所有内容,因为找出主要和生产者中的问题,然后我可以找出消费者。
'''prodcons.h
#include <pthread.h>
#define BUFFER_SIZE (50)
struct prodcons {
int buffer[BUFFER_SIZE];
int count;
int top;
int next;
pthread_mutex_t count_lock;
};
void pc_init(struct prodcons *pc);
int pc_pop(struct prodcons *pc);
void pc_push(struct prodcons *pc, int val);
'''
'''prodcons.c
void pc_init(struct prodcons *pc)
{
count = 0;
top = 0;
next = 0;
}
int pc_pop(struct prodcons *pc)
{
int val;
pthread_mutex_lock(&pc.count_lock);
if (counter > top)
{
val = pc.buffer[count];
pc.buffer[count] = 0;
pc.count--;
}
pthread_mutex_unlock(&pc.count_lock);
return val;
}
void pc_push(struct prodcons *pc, int val)
{
pthread_mutex_lock(&pc.count_lock);
if (count < BUFFER_SIZE)
{
pc.buffer[count] = val;
pc.count++;
}
pthread_mutex_unlock(&pc.count_lock);
}
'''main.c file
#include <pthread.h>
#include <stdlib.h>
#include <math.h>
#include "producer.h"
#include "prodcons.h"
int main(int argc, char *argv[])
{
int index = 1;
int num;
struct prodcons pc_nums;
//pthread_t tid[argc - 1];
pthread_t tid;
pthread_attr_t attr;
if (argc < 2) {
fprintf(stderr, "usage: No arguments\n");
return -1;
}
if (atoi(argv[1]) <= 0)
{
fprintf(stderr, "%d not > 0 or you must provide a positive integer.\n", atoi(argv[1]));
return -1;
}
/*get the default attributes */
pthread_attr_init(&attr);
//init the data structure
pc_init(&pc_nums);
//create producer thread
pthread_create(&tid, &attr, *producer, &pc_nums);
while (index < argc)
{
num = atoi(argv[index]);
pc_push(&pc_nums, num);
index++;
}
}
'''producer.h
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include "consumer.h"
void factor2pc(struct prodcons *pc, int number);
void *producer(void *data);
'''producer.c
#include <math.h>
#include <pthread.h>
void factor2pc(struct prodcons *pc, int number) {
pthread_t tid;
pthread_attr_t attr;
//int counter = 0;
//Add original value to int* primeNumbers for return printing
pc->pc_push(&pc, number);
//Print twos that divide argument
while (number % 2 == 0)
{
pc->pc_push(&pc, 2);
number = number/2;
}
//Argument is odd. Check using i + 2
for (int i = 3; i <= sqrt(number); i = i + 2)
{
while (number % i == 0)
{
pc->pc_push(&pc, i);
number = number/i;
}
}
//When n is a prime number greater than 2
if (number > 2)
{
pc->pc_push(&pc, number);
}
//add -1 to end of number prime factors to sign
pc->pc_push(&pc, number);
}
void *producer(void *data) {
int number;
struct prodcons primeNums;
pc_init(&primeNums);
//call consumer thread
pthread_t tid;
pthread_attr_t attr;
pthread_create(&tid, attr, *consumer, &primeNums);
while (data.count < BUFFER_SIZE)
{
number = data.pc_pop(data);
factor2pc(&primeNums, number);
}
}
我希望输出每个数字及其素数。