要模拟出消费者生产者模型,就要先了解他们之间的关系,生活中,我们在买东西时候就是一种生产者消费者模型,供货商就相当于生产者,而我们就是消费者,商店或者超市,就是一种可以促使我们与供货商之间进行交易的场所。
总结一下,我们可以简单的记为“321原则“。
- 3是指三种关系,即为消费者与消费者的关系,消费者与生产者的关系,生产者与生产者的关系。
- 2是指两个对象,即为消费者与生产者。
- 1则是指一个交易场所。
既然我们知道有三种关系,那么我们就来仔细理一理这三种关系。
- 消费者与消费者的关系。这个就理解为跟我们一样在超市买东西的人,我们去买同样的东西,假设数量不够的话,要么对方买,要么我们买,很明显是互斥关系。
- 生产者与消费者的关系。这个就理解为供货商和我们,只有在供货商有货供应的时候我们才能去买,不然我们是买不到的,所以这里肯定有同步的关系,而且在供货商送货物的这个过程中,我们是不能购买的,只有当他把货物放好后,我们才能进行购买,所以这里不仅有同步关系,也有互斥关系。
- 生产者与生产者的关系。这个就看作是不同供货商之间的关系,他们卖出同样的东西,但消费者必定只选一方进行消费,所以他们的关系就是互斥关系。
清楚他们之间的关系后,我们就通过程序来进行模拟实现了。
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<pthread.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node* next;
}node,*pnode,**ppnode;
pthread_mutex_t mutex;
pthread_cond_t cond;
pnode buynode(int n)
{
pnode tmp = (pnode)malloc(sizeof(node));
tmp->num = n;
tmp->next=NULL;
return tmp;
}
void listinit(ppnode head)
{
pnode tmp = (pnode)malloc(sizeof(node));
tmp->num = 0;
tmp->next = NULL;
*head = tmp;
}
void pushfront(pnode head,int m)
{
int n = m;
pnode newhead = buynode(n);
newhead->next=head->next;
head->next=newhead;
}
void poplist(pnode head)
{
if(head->next==NULL)
perror("pop");
pnode tmp = head->next;
head->next=tmp->next;
//free(tmp);
}
void *runC(void* arg)//消费者
{
pnode head =* ((ppnode)arg);
while(1)
{
pthread_mutex_lock(&mutex);
while(head->next==NULL)
{
printf("consumer need waitting...\n");
pthread_cond_wait(&cond,&mutex);
}
int d = head->next->num;
printf("consumer get data:%d\n",d);
poplist(head);
//pthread_mutex_unlock(&mutex);
sleep(2);
}
}
void* runP(void* arg)//生产者
{
pnode head= *((ppnode)arg);
while(1)
{
sleep(2);
int d = rand()%100+1;
pushfront(head,d);
printf("product data:%d\n",d);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(2);
}
}
int main()
{
srand((unsigned long)time(NULL));
pnode head = NULL;
listinit(&head);
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
pthread_t p,c;
pthread_create(&c,NULL,runC,&head);
pthread_create(&p,NULL,runP,&head);
pthread_join(c,NULL);
pthread_join(p,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
通过结果我们不难看出他们之间的关系。这里就不在多说。
接下来我们要看的是环形队列实现生产者消费者模型,环形队列与链表不同的一点是如果生产者一直生产而消费者不消费的话,总有满的时候,一旦队列满了的话,生产者就不去消费了,同理,如果没有数据,消费者也不能去消费。
因此我们不难发现,在这里我们需要一个可以实现计数器功能的东西去协助我们进行数据量的和空白队列的表示,所以我们在这里就要了解一个东西,POSIX信号量。POSIX信号量可以用于进程也可以用于线程,由用户自己选择。
#include<semaphore.h>这是它需要引用的头文件。
信号量初始化函数:
int sem_init(sem_t* sem,int pshared,unsigned int value);
pshared:0表示线程间,1表示进程间。
value:初始值。
销毁信号量:
int sem_destroy(sem_t* sem);
等待信号量:
int sem_wait(sem_t* sem);等待信号量,会将信号量减1;
发布信号量:
int sem_post(sem_t* sem);发布信号量,将信号量加1;
接下来我们就利用信号量来完成利用环形队列模拟的生产者消费者模型。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#include<semaphore.h>
int arr[6];
pthread_mutex_t lock1;
pthread_mutex_t lock2;
sem_t sem_data;
sem_t sem_blank;
void* runP(void* arg)//生产者
{
static int i = 0;
while(1)
{
pthread_mutex_lock(&lock1);
sem_wait(&sem_data);
int n = arr[i];
printf("consumer get: %d\n",n);
sem_post(&sem_blank);
i++;
i=i%6;
pthread_mutex_unlock(&lock1);
sleep(2);
}
}
void* runC(void* arg)//消费者
{
static int i = 0;
while(1)
{
pthread_mutex_lock(&lock2);
sem_wait(&sem_blank);
int n = rand()%1000+1;
arr[i]=n;
printf("product data:%d\n",n);
i++;
i=i%6;
sem_post(&sem_data);
pthread_mutex_unlock(&lock2);
sleep(2);
}
}
int main()
{
srand((unsigned long)time(NULL));
pthread_t consumer[3];
pthread_t product[3];
pthread_mutex_init(&lock1,NULL);
pthread_mutex_init(&lock2,NULL);
sem_init(&sem_data,0,0);
sem_init(&sem_blank,0,6);
int i = 0;
for(i=0;i<3;i++)
{ pthread_create(&product[i],NULL,runC,NULL);}
for(i=0;i<3;i++)
{ pthread_create(&consumer[i],NULL,runP,NULL);}
for(i=0;i<3;i++)
{ pthread_join(consumer[i],NULL);}
for(i=0;i<3;i++)
{ pthread_join(product[i],NULL);}
sem_destroy(&sem_data);
sem_destroy(&sem_blank);
pthread_mutex_destroy(&lock1);
pthread_mutex_destroy(&lock2);
return 0;
}
从结果我们也能证明代码的正确性。