gpt4 book ai didi

c - c中的Producer_consumer使用p线程和信号量

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:37 24 4
gpt4 key购买 nike

/*Producer_consumer problem in c using semaphores and pthreads*/


#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

#define RAND_DIVISOR 100000000
#define TRUE 1

typedef int buffer_item;
#define BUFFER_SIZE 5

/* The mutex lock */
pthread_mutex_t mutex;

/* the semaphores */
sem_t full, empty;

/* the buffer */
buffer_item buffer[BUFFER_SIZE];

/* buffer counter */
int counter;

pthread_t tid; //Thread ID
pthread_attr_t attr; //Set of thread attributes

void *producer(void *param); /* the producer thread */
void *consumer(void *param); /* the consumer thread */
void initializeData() {

/* Create the mutex lock */
pthread_mutex_init(&mutex, NULL);

/* Create the full semaphore and initialize to 0 */
sem_init(&full, 0, 0);

/* Create the empty semaphore and initialize to BUFFER_SIZE */
sem_init(&empty, 0, BUFFER_SIZE);

/* Get the default attributes */
pthread_attr_init(&attr);

/* init buffer */
counter = 0;
}

/* Producer Thread */
void *producer(void *param) {
buffer_item item;

while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(rNum);

/* generate a random number */
item = rand();

/* acquire the empty lock */
sem_wait(&empty);
/* acquire the mutex lock */
pthread_mutex_lock(&mutex);

if(insert_item(item)) {
fprintf(stderr, " Producer report error condition\n");
}
else {
printf("producer produced %d\n", item);
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal full */
sem_post(&full);
}
}

/* Consumer Thread */
void *consumer(void *param) {
buffer_item item;

while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(rNum);

/* aquire the full lock */
sem_wait(&full);
/* aquire the mutex lock */
pthread_mutex_lock(&mutex);
if(remove_item(&item)) {
fprintf(stderr, "Consumer report error condition\n");
}
else {
printf("consumer consumed %d\n", item);
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal empty */
sem_post(&empty);
}
}

/* Add an item to the buffer */
int insert_item(buffer_item item) {
/* When the buffer is not full add the item
and increment the counter*/
if(counter < BUFFER_SIZE) {
buffer[counter] = item;
counter++;
return 0;
}
else { /* Error the buffer is full */
return -1;
}
}

/* Remove an item from the buffer */
int remove_item(buffer_item *item) {
/* When the buffer is not empty remove the item
and decrement the counter */
if(counter > 0) {
*item = buffer[(counter-1)];
counter--;
return 0;
}
else { /* Error buffer empty */
return -1;
}
}

int main(int argc, char *argv[]) {
/* Loop counter */
int i;

/* Verify the correct number of arguments were passed in */
if(argc != 4) {
fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
}

int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */
int numProd = atoi(argv[2]); /* Number of producer threads */
int numCons = atoi(argv[3]); /* Number of consumer threads */

/* Initialize the app */
initializeData();

/* Create the producer threads */
for(i = 0; i < numProd; i++) {
/* Create the thread */
pthread_create(&tid,&attr,producer,NULL);
}

/* Create the consumer threads */
for(i = 0; i < numCons; i++) {
/* Create the thread */
pthread_create(&tid,&attr,consumer,NULL);
}

/* Sleep for the specified amount of time in milliseconds */
sleep(mainSleepTime);

/* Exit the program */
printf("Exit the program\n");
exit(0);
}

当我使用 gcc 编译它时,出现以下错误:cse@cse-未指定:~/11011P0519$ vi producer_consumer2.c

cse@cse-Not-Specified:~/11011P0519$ gcc producer_consumer2.c

/tmp/ccu6fydZ.o:在函数“initializeData”中:

producer_consumer2.c:(.text+0x32): 对 `sem_init' 的 undefined reference

producer_consumer2.c:(.text+0x4e): 对 `sem_init' 的 undefined reference

/tmp/ccu6fydZ.o: 在函数“生产者”中:

producer_consumer2.c:(.text+0xac): 对 `sem_wait' 的 undefined reference

producer_consumer2.c:(.text+0x11d): 对 `sem_post' 的 undefined reference

/tmp/ccu6fydZ.o:在函数“消费者”中:

producer_consumer2.c:(.text+0x160): 对 `sem_wait' 的 undefined reference

producer_consumer2.c:(.text+0x1d1): 对 `sem_post' 的 undefined reference

/tmp/ccu6fydZ.o: 在函数“main”中:

producer_consumer2.c:(.text+0x2ef): 对 `pthread_create' 的 undefined reference

producer_consumer2.c:(.text+0x32d): 对 `pthread_create' 的 undefined reference

collect2: ld 返回了 1 个退出状态

当我尝试使用 gcc -lpthreads 时,我得到找不到 lpthread。

如何解决这个问题?

最佳答案

在使用 semphore.h 中的函数时,您需要使用 -lrt-pthread 链接

编译如下

gcc producer_consumer2.c -pthread

参见 man sem_init

关于c - c中的Producer_consumer使用p线程和信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19537981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com