This is my first post, and I'm fairly new at programming. I have a project where I'm recreating the well known bounded buffer problem (producer-consumer). Upon creating these two threads, they share a buffer of size 10. When I compile the code I get a warning in the function 'write_to_buffer' stating "comparison between pointer and integer". When I execute the program, I receive the following statement "Segmentation Fault".
Can somebody explain what this means and provide any hints on how to fix it?
Thank you very much.
Code:
/*Header files*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <thread.h>
#include <semaphore.h>
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
/*Function declarations*/
void *consumer (void *arg);
void *producer (void *arg);
void *read_from_buffer (char []);
void *write_to_buffer (char []);
/*Global declarations*/
char strng[10];
sem_t mutex, full, empty;
int main()
{
/*-----------------Variable declarations--------------------------*/
/*----------------------------------------------------------------*/
int cons, prod, n;
pthread_t tid1[1];
pthread_t tid2[1];
pthread_attr_t attr[1];
/*Required to schedule thread independently*/
pthread_attr_init(&attr[0]);
pthread_attr_setscope(&attr[0], PTHREAD_SCOPE_SYSTEM);
/*Creating 2 independent threads, Producer and Consumer*/
prod = pthread_create(&tid1[0], &attr[0], producer, NULL);
cons = pthread_create(&tid2[0], &attr[0], consumer, NULL);
/*Initializing semaphores*/
sem_init(&mutex,0,1);
sem_init(&full,0,0);
sem_init(&empty,0,10);
/*---------End of variable declarations---------------------------*/
/*----------------------------------------------------------------*/
/*Destroy attribute and wait for threads*/
pthread_attr_destroy(&attr[0]);
/*Wait for threads to finish before exiting*/
pthread_join(tid1[0],NULL);
pthread_join(tid2[0],NULL);
/*Destroy semaphores*/
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
exit(0);
}
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void *consumer (void *arg)
{
while (TRUE)
{ /*Entry section*/
sem_wait(&full);
/*End of entry section*/
/*Critical section*/
sem_wait(&mutex);
read_from_buffer (strng);
/*End of critical section*/
/*Remainder section*/
sem_post(&mutex);
sem_post(&empty);
}
exit(0);
}
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void *producer (void *arg)
{
while (TRUE)
{ /*Entry section*/
sem_wait(&empty);
/*End of entry section*/
/*Critical section*/
sem_wait(&mutex);
write_to_buffer(strng);
/*End of critical section*/
/*Remainder section*/
sem_post(&mutex);
sem_post(&full);
/*End of remainder section*/
}
exit(0);
}
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
void *write_to_buffer (char buffer[])
{
int i=0;
char newchar;
FILE *fp;
/*Open the input file*/
fp = fopen("/home/csee1/jbarnes3/in.dat", "r");
if ((fp = fopen("in.dat", "r")) == NULL)
printf("File not found\n");
while(fgets(buffer, 1, fp) !='*')
{
buffer[i] = newchar; /*store the character entered*/
++i;
}
buffer[i] = '*'; /*terminate the string*/
/*Close the file*/
close(fp);
return;
}
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
void *read_from_buffer (char buffer[])
{
int j;
while (buffer[j] !='*')
{
printf("%c\n");
puts (buffer);
sleep(1);
++j;
}
return;
}