Segmentation fault when executing bounded buffer program
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;
}
Fixed code but now getting weird output
I was using '*' as a NULL so when the producer read this value it would terminate.
I modified my function to this
Code:
void *write_to_buffer (char buffer[])
{
int i=0;
FILE *fp;
/*Open the input file*/
fp = fopen("/home/in.dat", "r");
if (fp == NULL)
{
printf("\nFailed to open the file.\n");
exit(1);
}
else
{
printf("File is opened. Time to work.\n");
while(TRUE)
{
newchar = fgetc(fp);
if(newchar!= nul)
{
printf("%c",newchar);
}
else
{
break;
}
}
/*Close the file*/
fclose(fp);
return 0;
}
}
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
void *read_from_buffer (char buffer[])
{
int j;
while (buffer[j] !=nul)
{
printf("%c\n", &buffer);
sleep(1);
++j;
}
return 0;
}
Now, I'm getting a weird output. The statement, "File is opened. Time to work." as well as the contents of the file I'm opening are printed 10 times. Is this because I'm trying to open the file in this function. Should it go in main instead?
Thanks for your help.