Can't seem to figure out this error. I have the semaphore.h file included, but for some reason I keep getting compiler errors stating it can't identify a reference to functions sem_init(), sem_post(), and sem_wait(). Here's the code I have:
Code:
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#include "buffer.h"
/* POSITION MARKERS (CONCURRENCY ISSUE BECAUSE GLOBAL DATA SHARED AMONG THREADS)*/
int in_mrkr; /* in marker points to the next free position in the buffer */
int out_mrkr; /* out marker points to the first full position in the buffer*/
buffer_item buffer[BUFFER_SIZE]; /* buffer used by producer/consumer pair */
/* SEMAPHORES */
sem_t empty; /* counter indicating all positions of the buffer are available */
sem_t full; /* counter indicating all positions of the buffer are unavailable */
/* MUTEXES */
pthread_mutex_t mutex; /* lock for allowing consumer or producer to access buffer exclusively */
void buffer_init(void) {
/* initialize markers */
in_mrkr = 0;
out_mrkr = 0;
/* initialize semphores */
if (sem_init(&empty, 0, BUFFER_SIZE))
print_err();
if (sem_init(&full, 0, 0))
print_err();
/* initialize mutex lock */
if (pthread_mutex_init(&mutex, NULL))
print_err();
}
int insert_item(buffer_item item) {
/* decrement empty semaphore for producer */
sem_wait(&empty);
/* lock thread so producer can add item to the buffer */
pthread_mutex_lock(&mutex);
//--- PRODUCER CRITICAL SECTION ---
/* add item to buffer and move the in marker to next free position */
buffer[in_mrkr] = item;
in_mrkr = (in_mrkr + 1) % BUFFER_SIZE;
/* release the lock on the producer thread */
pthread_mutex_unlock(&mutex);
/* increment full semaphore to inidicate buffer has a filled slot */
sem_post(&full);
return 0; // NEED ERROR CHECKING -- indicate successfull return for now
}
int remove_item(buffer_item *item) {
/* decrement full semaphore for consumer */
sem_wait(&full);
/* lock thread so consumer can pull the item off the buffer */
pthread_mutex_lock(&mutex);
//--- CONSUMER CRITICAL SECTION ---
/* remove an item from the buffer and move the out marker to next full position */
*item = buffer[out_mrkr];
out_mrkr = (out_mrkr + 1) % BUFFER_SIZE;
/* release the lock on the consumer thread */
pthread_mutex_unlock(&mutex);
/* increment empty semaphore to inidicate buffer has a empty slot */
int a = sem_post(&empty);
return 0; // NEED ERROR CHECKING -- indicate successfull return for now
}
I checked the semaphore header and it contains the prototype definitions as follows:
Code:
/* Initialize semaphore object SEM to VALUE. If PSHARED then share it
with other processes. */
extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value)
__THROW;
/* Wait for SEM being posted.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int sem_wait (sem_t *__sem);
/* Post SEM. */
extern int sem_post (sem_t *__sem) __THROW;
I don't understand why I'm getting errors like this from the compiler
In function `buffer_init':
/home/jshowa/NetBeansProjects/producer_consumer_prob/buffer.c:40: undefined reference to `sem_init'
/home/jshowa/NetBeansProjects/producer_consumer_prob/buffer.c:43: undefined reference to `sem_init'
There's error for the insert_item and remove_item functions that contain sem_wait() and sem_post(), but the error is the same.
If anyone could shed some light on this, I'd appreciate it, thanks.