I am generally new to C so I have loads of syntax errors and referencing problems. I have written prodcons.c, a Struct that will create a bounded buffer of size 50 and a few helper variables. In main I create a variable of this data struct and initialize it. I am attempting to take any amount of arguments (IE {1..1000}) lets say into the thousands and PUSH these into the buffer, while at the same time another C file producer.c is attempting to POP from the same buffer that I shared to it with a thread call. PUSH and POP both have a mutex lock to stop any changes to the critical section at the same time. Producer thread will then initialize another data struct, take the prime factors of each argument from main in their shared buffer, share a new initialized data struct with a Consumer.c function call in a thread. Consumer simply prints the number and its prime factors. I believe I have to have #include prodcons.h in all of my files to use its functions, but please correct me if I am wrong. Also, I am trying to create BUSY WAITNG, with main and producer each attempt to get to the critical section with while loops, calling POP and PUSH, but this is most likely wrong. PLEASE HELP, ADVICE, SYNTAX. ANYTHING.
I honestly have tried to just compile the code, but my referencing and pointers and any correct way in C to share files and pointer calls to the data structure are off. I think I just need a lot of corrections. Such as when using a struct pointer do I use pc->function() or pc.function()? These small syntax errors have got me stuck. I am sharing everything by consumer.h and consumer.c because figure out the problems in main and producer then consumer I can figure out.
'''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);
}
}
I expect an output each number and its prime numbers.