Producer/Consumer - problems creating threads
I'm trying to create 20 producer threads and 5 consumer threads. In my main, I put 20 text file names into a fifo queue.
The producer thread gets the file name from the queue, reads the contents of that file, converts the characters to uppercase, and writes the contents into the buffer slot. (I am supposed to create 5 buffer slots, but to keep things simple, I'm storing everything into just buffer[0] for now until I fix the problem with creating threads).
The consumer thread reads the contents from the buffer and then prints them. And the buffer and queue have to be empty until the consumer stops from continuing.
Fairly straightforward! However, right now I am only having lock creating 20 producer threads with 20 consumer threads. Basically it executes like the following:
-producer stores the contents of in0.txt into the buffer, the consumer reads it and prints the contents.
-producer stores the contents of in1.txt into the buffer, the consumer reads it and prints the contents.
-producer stores the contents of in2.txt into the buffer, the consumer reads it and prints the contents.
etc.. until in19.txt has been read.
I'm glad things work for 20 producers and 20 consumers, but I need it to work for 20 and 5. Please focus your attention to the bold part. If I remove the red comments, my code won't run and it gets hung after the 5th producer gets created. Anyone know how I can code 20 producer threads and 5 consumer threads to run properly? I've been at this for hours with no luck. Any help would be greatly appreciated.
Code:
struct fifo_struct
{
char fileName[1024];
struct fifo_struct* next;
};
struct linked_list
{
struct fifo_struct* head;
struct fifo_struct* tail;
};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t full, empty;
char buffer[BUFFER_SIZE][128];
int main()
{
pthread_t producerVar;
pthread_t consumerVar;
char line[1024];
char str[200];
int i = 0;
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
struct fifo_struct* fifo;
struct linked_list* s = malloc( 1 * sizeof(*s));
if(s == NULL)
{
fprintf(stderr, "LINE: %d, malloc() failed\n", __LINE__);
}
s->head = s->tail = NULL;
for(i = 0; i < (FILESIZE + 1); i++)
{
sprintf(str, "in%d.txt", i);
fifo = malloc(1 * sizeof(*fifo));
if(fifo == NULL)
{
fprintf(stderr, "IN %s, %s: malloc() failed\n", __FILE__, "list_add");
}
strcpy(fifo->fileName,str);
fifo->next = NULL;
if(s == NULL)
{
printf("Error: Queue has not been initialized\n");
}
else if(s->head == NULL && s->tail == NULL)
{
s->head = s->tail = fifo;
}
else if(s->head == NULL || s->tail == NULL)
{
printf("Error: Problem with code\n");
free(fifo);
}
else
{
s->tail->next = fifo;
s->tail = fifo;
}
}
//print_queue(s);
//fifo->mutex = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
//pthread_mutex_init(fifo->mutex, NULL);
for(i = 0; i < 20; i++)
{
pthread_create(&producerVar, NULL, producer, s);
pthread_join(producerVar, NULL);
//}
//for(i = 0; i < 20; i++)
//{
pthread_create(&consumerVar, NULL, consumer, s);
pthread_join(consumerVar, NULL);
}
return 0;
}
I was hoping something like the following would work, but when I run the code, it hangs after the 5th producer thread or so:
for(i = 0; i < 20; i++)
{
pthread_create(&producerVar, NULL, producer, s);
pthread_join(producerVar, NULL);
}
for(i = 0; i < 20; i++)
{
pthread_create(&consumerVar, NULL, consumer, s);
pthread_join(consumerVar, NULL);
}