I have the following peace of code for the producer_consumer problem:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
void queueAdd (queue *q, int in)
{
q->buf[q->tail] = in;
q->tail++;
if (q->tail== QUEUESIZE)
q->tail= 0;
if (q->tail== q->head)
q->full= 1;
q->empty = 0;
return;
}
void queueDel (queue *q, int *out)
{
*out= q->buf[q->head];
q->head++;
if (q->head == QUEUESIZE)
q->head= 0;
if (q->head== q->tail)
q->empty= 1;
q->full = 0;
return;
}
I get this result:
bt Desktop # gcc -m32 -g -o consumer consumer.c
consumer.c:129:2: warning: no newline at end of file
/tmp/ccdRObpm.o: In function `main':
/root/Desktop/consumer.c:35: undefined reference to `pthread_create'
/root/Desktop/consumer.c:36: undefined reference to `pthread_create'
/root/Desktop/consumer.c:37: undefined reference to `pthread_join'
/root/Desktop/consumer.c:38: undefined reference to `pthread_join'
/root/Desktop/consumer.c:39: undefined reference to `phthread_mutex_lock'
/root/Desktop/consumer.c:40: undefined reference to `phthread_mutex_unlock'
collect2: ld returned 1 exit status
Par from the no new line,i have no idea how to fix the errors.any help is appreciated.
This is cplusplus.com, not c.com. A C++ programmer would create a Queue class. ;-)
However, the fix is to link with -lpthread.
I'd like to make one other recommendation if I might: don't program as root. You can really damage your system by running buggy code as root. Create a regular user account and use that.