无法使用信号量编译代码:函数sem_signal()缺失

时间:2014-04-04 17:19:57

标签: c

有人可以帮我看看我做错了什么。当我尝试编译下面的代码时,我不断收到以下错误(即使我使用-lrt-pthread-lpthread进行编译时:

ppcc.c:51:3: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
ppcc.c:57:3: warning: implicit declaration of function ‘sem_signal’ [-Wimplicit-function-declaration]
/tmp/ccXQDdG7.o: In function `producer': ppcc.c:(.text+0xef): undefined reference to `sem_signal'
/tmp/ccXQDdG7.o: In function `consumer': ppcc.c:(.text+0x18a): undefined reference to `sem_signal' collect2: ld returned 1 exit status

代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/shm.h>

typedef int buffer_item;
#define BUFFER_SIZE 5
#define SLEEP_TIME 5
#define TRUE 1
#ifdef RAND_MAX     //only generate #s between 1 & 20
#undef RAND_MAX
#define RAND_MAX [20]
#endif

buffer_item buffer[BUFFER_SIZE];
pthread_mutex_t mutex;
sem_t empty;
sem_t full;

int count=0;

int insert_item(buffer_item item)
{
    if(count<BUFFER_SIZE){
        buffer[count]=item;
        count++;
    }
    return 0;
}

int remove_item(buffer_item *item)
{
    if(count>=0){
        count--;
    }
    return 0;
}


void *producer(void *param)
{
    buffer_item item;
    int return_result1;     //holds the return value of insert_item for error check
    do{
        int rand_sleep_time=((rand()/2)+5);
        item=rand();
        sleep(rand_sleep_time);
        sem_wait(&empty);
        pthread_mutex_lock(&mutex);
        return_result1=insert_item(item);
        printf("Producer generated random integer: %d and stored it at index: %d/n",item,(count-1));
        pthread_mutex_unlock(&mutex);
        sem_signal(&full);
    }while(TRUE);
}

void *consumer(void *param)
{
    int return_result2;     //holds the return value of remove_item for error check
    do{
        int rand_sleep_time=((rand()/2)+5);
        sleep(rand_sleep_time);
        sem_wait(&full);
        pthread_mutex_lock(&mutex);
        return_result2=remove_item(&buffer[count]);
        printf("Consumer consumed random integer stored at index: %d/n",(count+1));
        pthread_mutex_unlock(&mutex);
        sem_signal(&empty);
    }while(TRUE);
}

int main(int argc, char *argv[])
{
    int sleepTime;

    if(argc != 2)
    {
        fprintf(stderr, "Useage: <sleep time> \n");
        return -1;
    }

    sleepTime = atoi(argv[1]);

    //Initizlize the rounded array 
    for(int i=0; i<BUFFER_SIZE; i++)
        buffer[i] = -1;

    //Initialize the the locks
    pthread_mutex_init(&mutex, NULL);
    sem_init(&empty, 0, BUFFER_SIZE);
    sem_init(&full, 0, 0);
    srand(time(0));

    //Create the producer and consumer threads
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_create(&tid, &attr, producer, NULL);

    pthread_create(&tid, &attr, consumer, NULL);

    //Sleep for user specified time
    sleep(sleepTime);
    return 0;
}

0 个答案:

没有答案