C 生产者和消费者代码don';不出现在屏幕上
我有以下代码,但生产者和消费者代码没有出现。它只打印“退出程序”。就像消费者和生产者的函数永远不会执行一样。我不明白为什么会这样。有人能解释为什么吗? 代码如下:C 生产者和消费者代码don';不出现在屏幕上,c,mutex,semaphore,C,Mutex,Semaphore,我有以下代码,但生产者和消费者代码没有出现。它只打印“退出程序”。就像消费者和生产者的函数永远不会执行一样。我不明白为什么会这样。有人能解释为什么吗? 代码如下: /* buffer.h */ typedef int buffer_item; #define BUFFER_SIZE 18 /* main.c */ #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <
/* buffer.h */
typedef int buffer_item;
#define BUFFER_SIZE 18
/* main.c */
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
/* #include "buffer.h" */
#define RAND_DIVISOR 100000000
#define TRUE 1
/* 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);
}
/*buffer.h*/
typedef int buffer_项;
#定义缓冲区大小18
/*main.c*/
#包括
#包括
#包括
#包括
/*#包括“buffer.h”*/
#定义RAND_除数100000000
#定义真1
/*互斥锁*/
pthread_mutex_t mutex;
/*信号灯*/
sem_t满,空;
/*缓冲区*/
缓冲区\项目缓冲区[缓冲区\大小];
/*缓冲计数器*/
整数计数器;
pthread_t tid//线程ID
pthread_attr_t attr//线程属性集
无效*生产者(无效*参数);/*生产线*/
无效*使用者(无效*参数);/*消费者线索*/
无效初始化数据()
{
/*创建互斥锁*/
pthread_mutex_init(&mutex,NULL);
/*创建完整信号量并初始化为0*/
sem_init(&full,0,0);
/*创建空信号量并初始化为缓冲区大小*/
sem_init(&empty,0,缓冲区大小);
/*获取默认属性*/
pthread_attr_init(&attr);
/*初始化缓冲区*/
计数器=0;
}
/*生产者线程*/
无效*生产者(无效*参数)
{
缓冲项;
while(TRUE)
{
/*随机睡眠一段时间*/
int rNum=rand()/rand\u除数;
睡眠(rNum);
/*生成一个随机数*/
item=rand();
/*获取空锁*/
sem_等待(&empty);
/*获取互斥锁*/
pthread_mutex_lock(&mutex);
如果(插入项目(项目))
fprintf(标准,“生产者报告错误条件”);
其他的
printf(“生产商生产%d\n”,物料);
/*释放互斥锁*/
pthread_mutex_unlock(&mutex);
/*信号满*/
sem_职位(完整);
}
}
/*消费线程*/
无效*使用者(无效*参数)
{
缓冲项;
while(TRUE){
/*随机睡眠一段时间*/
int rNum=rand()/rand\u除数;
睡眠(rNum);
/*获得全锁*/
sem_等待(&full);
/*获取互斥锁*/
pthread_mutex_lock(&mutex);
如果(删除_项(&项)){
fprintf(标准,“消费者报告错误条件”);
}否则{
printf(“消费者消费%d\n”,项目);
}
/*释放互斥锁*/
pthread_mutex_unlock(&mutex);
/*空信号*/
sem_post(&空);
}
}
/*将项目添加到缓冲区*/
int insert_项(缓冲区_项)
{
/*当缓冲区未满时,添加该项
然后递增计数器*/
if(计数器<缓冲区大小){
缓冲区[计数器]=项目;
计数器++;
返回0;
}否则{/*错误缓冲区已满*/
返回-1;
}
}
/*从缓冲区中删除项目*/
int删除项目(缓冲区项目*项目)
{
/*当缓冲区不为空时,移除该项
并减小计数器的值*/
如果(计数器>0){
*项目=缓冲区[(计数器-1)];
计数器--;
返回0;
}else{/*错误缓冲区为空*/
返回-1;
}
}
int main(int argc,char*argv[])
{
/*循环计数器*/
int i;
/*验证传入的参数数量是否正确*/
如果(argc!=4){
fprintf(stderr,“用法:./main.out\n”);
}
int main sleeptime=atoi(argv[1]);/*main睡眠的时间(以秒为单位)*/
int numProd=atoi(argv[2]);/*生产者线程数*/
int numCons=atoi(argv[3]);/*使用者线程数*/
/*初始化应用程序*/
初始化数据();
/*创建生产者线程*/
对于(i=0;i
首先,您需要为线程应用不同的tid
pthread_t tid1,tid2代码>
然后使用以下tid:
pthread_创建(&tid1,&attr,producer,NULL)代码>
pthread_创建(&tid2,&attr,consumer,NULL)代码>
最后,跳过睡眠并加入线程
pthread_-join(tid1,NULL)代码>
pthread_-join(tid2,NULL)代码>
我已经对它进行了测试,并在我的系统上运行。下面是完整的工作代码
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include "buffer.h"
#define RAND_DIVISOR 100000000
#define TRUE 1
/* 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 tid1, tid2; //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(&tid1,&attr,producer,NULL);
}
/* Create the consumer threads */
for(i = 0; i < numCons; i++) {
/* Create the thread */
pthread_create(&tid2,&attr,consumer,NULL);
}
/* Sleep for the specified amount of time in milliseconds */
//sleep(mainSleepTime);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
/* Exit the program */
printf("Exit the program\n");
exit(0);
}
#包括
#包括
#包括
#包括
#包括“buffer.h”
#定义RAND_除数100000000
#定义真1
/*互斥锁*/
pthread_mutex_t mutex;
/*信号灯*/
sem_t满,空;
/*缓冲区*/
缓冲区\项目缓冲区[缓冲区\大小];
/*缓冲计数器*/
整数计数器;
pthread_t tid1,tid2//线程ID
pthread_attr_t attr//线程属性集
无效*生产者(无效*参数);/*生产线*/
无效*使用者(无效*参数);/*消费者线索*/
无效初始化数据(){
/*创建互斥锁*/
pthread_mutex_init(&mutex,NULL);
/*创建完整信号量并初始化为0*/
sem_init(&full,0,0);
/*创建空信号量并初始化为缓冲区大小*/
sem_init(&empty,0,缓冲区大小);
/*获取默认属性*/
pthread_attr_init(&attr);
/*初始化缓冲区*/
计数器=0;
}
/*生产者线程*/
无效*生产者(无效*参数){
缓冲项;
while(TRUE){
/*随机睡眠一段时间*/
int rNum=rand()/rand\u除数;
睡眠(rNum);
/*生成一个随机数*/
item=rand();
/*获取空锁*/
sem_等待(&empty);
/*获取互斥锁*/
pthread_mutex_lock(&mutex);
如果(插入项目(项目)){