#c #arrays #multithreading #pointers #queue
#c #массивы #многопоточность #указатели #очередь
Вопрос:
Я пытаюсь работать с проблемой производителя и потребителя, я долгое время занимался программированием на C. Моя конечная цель — иметь 3 производителя и 1 потребителя. На данный момент у меня есть 1 производитель и 1 потребитель. Я установил ограничение в верхней части, для которого должны выполняться циклы. Код работает хорошо до предела 46, однако это дает мне ошибку сегментации при любом значении выше 46.Это значение #define max 46. Я не уверен, почему это происходит. Любая помощь будет оценена.
Вот мой код. Я основывался на коде из книги Таненбаума о современных ОС.
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
//#define MAX 10000000000
#define max 50 //Works well till 46
#define NP 1
#define NC 1
pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
char queue[1][80], data[80];
int front, rear,reply;
void *producer(void *ptr)
{
int i,index;
index = (int)ptr;
printf("Producing!n");
for(i=1; i<=max; i )
{
printf("The value of i in prod %d with index %dn: ", i, index);
pthread_mutex_lock(amp;the_mutex);
//wait till the queue is null. After that insert
while(notNULL(amp;front,amp;rear) != -1)
pthread_cond_wait(amp;condp, amp;the_mutex);
reply =insq(queue, amp;rear, "test string");
if (i == max)
printf("DONE PRO!!!!n");
if( reply == -1 ){
printf("n Queue is Full, ERROR!!!n");
//break;
//exit(0);
}
pthread_cond_signal(amp;condc);
pthread_mutex_unlock(amp;the_mutex);
}
pthread_exit(0);
}
void *consumer(void *ptr)
{
int i, index;
index = (int)ptr;
printf("Consumingn");
for(i=1; i<=max; i )
{
printf("The value of i in con is %d with index %dn: ", i, index);
pthread_mutex_lock(amp;the_mutex);
while(notNULL(amp;front, amp;rear) == -1)
pthread_cond_wait(amp;condc, amp;the_mutex);
reply = delq(queue, amp;front, amp;rear, data);
if (i == max)
printf("DONE CON!!!!n");
if( reply == -1 )
printf("n Queue is Empty n");
pthread_cond_signal(amp;condp);
pthread_mutex_unlock(amp;the_mutex);
}
pthread_exit(0);
}
//int notNULL(queue[max][80],int *rear, int *front)
int notNULL(int *front, int *rear)
{
if(*front == *rear)
return(-1);
else
return 0;
}
int insq(char queue[1][80], int *rear, char data[80])
{
printf("rear is in insq %sn",queue[*rear]);
printf("value of rear is %dn",*rear);
//printf("Value of max-1 is %dn",(max-1) );
if(*rear == max -1)
return(-1);
else
{
*rear = *rear 1;
printf("Data is %sn",data );
strcpy(queue[*rear], data);
return(1);
}
}
int delq(char queue[1][80], int *front, int *rear, char data[80])
{
if(*front == *rear)
return(-1);
else
{
(*front) ;
strcpy(data, queue[*front]);
return(1);
}
}
int main(int argc, char *argv[])
{
front = rear = -1;
int i;
pthread_t pro,con;
pthread_mutex_init(amp;the_mutex,0);
pthread_cond_init(amp;condc, 0);
pthread_cond_init(amp;condp, 0);
for (i = 0; i < NC; i )
{
pthread_create(amp;con, 0, consumer, (void*)i);
}
for (i = 0; i < NP; i )
{
pthread_create(amp;pro, 0, producer, (void*)i);
}
pthread_join(pro,0);
pthread_join(con,0);
pthread_cond_destroy(amp;condc);
pthread_cond_destroy(amp;condp);
pthread_mutex_destroy(amp;the_mutex);
}
Комментарии:
1. Не слишком ли это
char queue[1]
мало?2. О чем
char queue[max][80];
?3. @alk Это, кажется, работает, но в некоторых случаях это переходит в цикл. Особенно, когда я увеличиваю количество производителей.