标签:生产者与消费者模型
基于链表的,其空间可以动态分配
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
pthread_cond_t cond;
pthread_mutex_t lock;
typedef struct _node{
int data;
struct _node *next;
}node_t, *node_p, **node_pp;
node_p head=NULL;
static node_p alloc_node(int _data)
{
node_p tmp=(node_p)malloc(sizeof(node_t));
if(!tmp){
perror("malloc");
return NULL;
}
tmp->data=_data;
tmp->next=NULL;
return tmp;
}
static void del_node(node_p tmp)
{
if(tmp){
free(tmp);
tmp=NULL;
}
}
static void init(node_pp _phead)
{
*_phead=alloc_node(0);
}
static void push_front(node_p _list,int _data)
{
node_p tmp=alloc_node(_data);
tmp->next=_list->next;
_list->next=tmp;
}
int is_empty(node_p _list)
{
return _list->next==NULL?1:0;
}
static int pop_front(node_p _list, int *_data)
{
if(is_empty(_list)){
return -1;
}
node_p tmp=_list->next;
_list->next=tmp->next;
*_data=tmp->data;
del_node(tmp);
return 0;
}
static void show_list(node_p _list)
{
node_p start=_list->next;
while(start){
printf("%d ",start->data);
start=start->next;
}
printf("\n");
}
void *consum(void* arg)
{
int data=0;
while(1){
pthread_mutex_lock(&lock);
while(is_empty(head)){
printf("cond is not ready,wait...\n");
pthread_cond_wait(&cond,&lock);
printf("cond is ready,consumer...\n");
}
pop_front(head,&data);
printf("consuner data done... :%d\n",data);
pthread_mutex_unlock(&lock);
sleep(1);
}
}
void *product(void* arg)
{
int data=0;
int done=0;
while(!done){
pthread_mutex_lock(&lock);
data=rand()%1234;
push_front(head,data);
pthread_mutex_unlock(&lock);
printf("product data done.,, :%d, signal consumer...\n",data);
sleep(10);
pthread_cond_signal(&cond);
}
}
int main()
{
init(&head);
pthread_cond_init(&cond,NULL);
pthread_mutex_init(&lock,NULL);
pthread_t id1,id2;
pthread_create(&id1,NULL,consum,NULL);
pthread_create(&id2,NULL,product,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);
return 0;
}
Makefile文件:
pc:pc.c
gcc -o $@ $^ -lpthread
.PHIONY:clean
clean:
rm -f pc
基于固定大小的环形队列(POSIX信号量)
sem.c
#include<semaphore.h>
#define _SIZE_ 64
sem_t space_nums;
sem_t data_nums;
int buf[_SIZE_];
void *consumer(void *arg)
{
int index=0;
while(1){
sleep(1);
sem_wait(&data_nums);
int data=buf[index];
printf("consumer done ... data is:%d\n",data);
sem_post(&space_nums);
index++;
index%=_SIZE_;
}
}
void *producter(void *arg)
{
int index=0;
while(1){
sem_wait(&space_nums);
buf[index]=rand()%1234;
printf("producter done ... data is:%d\n",buf[index]);
sem_post(&data_nums);
index++;
index%=_SIZE_;
}
}
int main()
{
sem_init(&space_nums,0,_SIZE_);
sem_init(&data_nums,0,0);
pthread_t id1,id2;
pthread_create(&id1,NULL,consumer,NULL);
pthread_create(&id2,NULL,producter,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
sem_destroy(&space_nums);
sem_destroy(&data_nums);
return 0;
}
Makefile文件:
sem:sem.c
gcc -o $@ $^ -lpthread
.PHONY:clean
clean:
rm -f sem
起初生产者一直生产,当生产满时,消费者开始消费,并且消费一个生产一个
不可能出现消费者超过生产者,因为资源有限,资源申请完就不能再申请了
标签:生产者与消费者模型
原文地址:http://frankenstein.blog.51cto.com/10918184/1828724