Reputation: 3751
I am currently experiencing both a deadlock and a core dump.
Outside of main I have the following
char buffer[SLOTCOUNT][SLOTSIZE];
int isEmpty[SLOTCOUNT];
FILE *myInFile;
FILE *myOutFile;
sem_t buf_lock, slot_avail, item_avail;
inside the main I have
int main(int argc, const char * argv[]){
//Create the basics of threads and intilize the sem
pthread_t consumer_t, producer_t;
sem_init(&buf_lock, 0, 1);
sem_init(&slot_avail, 0, 4);
sem_init(&item_avail, 0, 0);
/*Clear and clean the buffer*/
for(int i=0; i <= SLOTCOUNT; ++i){
isEmpty[i] = 0;
for(int j=0; j <= SLOTSIZE; ++j){
buffer[i][j] = NULL;
}
}
/*Check to make sure that the correct number of inputs have been provided*/
checkStart(argc);
/*Get the locations to the files and save it into a local variable. */
char inFile[sizeof(argv[1])];
char outFile[sizeof(argv[2])];
strcpy(inFile, argv[1]);
strcpy(outFile, argv[2]);
/*Load the file to read from and create the file to write to*/
fileReady(inFile, outFile);
/*Get the threads ready to return*/
pthread_create(&producer_t, NULL, producer, NULL);
pthread_create(&consumer_t, NULL, consumer, NULL);
pthread_join(producer_t, NULL);
pthread_join(consumer_t, NULL);
return 0;
}
and finally, my producer function
void* producer(){
/*Critical Section*/
sem_wait(&slot_avail);
printf("gets here\n");
sem_wait(&buf_lock);
printf("Never gets here\n");
for (int i = 0; i <= SLOTCOUNT; ++i){
if(buffer[i][0] == NULL) fread(buffer[i], sizeof(char), READSIZE, myInFile);
}
sem_post(&buf_lock);
sem_post(&item_avail);
/*Critical Section*/
}
Currently, the printf("Never gets here") never prints.
I would like to fix that.
However, if I comment out the sem_wait(&buf_lock)
, it will print it but then it core dumps.
Any ideas what I am doing wrong OR how to debug core dumps
EDIT: I understand this is not the best solution to a problem, however, this is just to show understanding of semaphores.
EDIT: The rest of the code has been independently tested and it works. IE the size of char ... etc.
I have also tried
int errno = sem_wait(&buf_lock);
printf("%s", strerror(errno));
to see whats going on, but it still locks and as expected the print doesnt print.
Upvotes: 2
Views: 1893
Reputation: 78923
Never use such library routines without testing their return value. In particular, sem_wait
may return when there is an interrupt (e.g for IO). Read the corresponding man page for the possible return values of all the functions and how they return errors.
In general, POSIX semaphores are not the right tool for application programming with threads. First, they are only an extension that is not supported by all POSIX systems, and then the tools that are really foreseen as basic tools for inter-thread control are pthread_mutex_t
and ptread_cond_t
.
Also your specification of your producer function is wrong and your compiler shouldn't have accepted it without barking at you. This function will be called by the system with an argument, so you have undefined behavior here. Don't do that, even if you don't need the void*
argument that a thread function receives, you need to declare that parameter.
Another, minor nitpick: names ending with _t
are reserved by POSIX for future extensions. They usually refer to types. Don't use them for variables.
Upvotes: 2