生產者消費者模型:
兩個進程共享一個緩衝區,一個進程稱爲生產者,它向緩衝區裏放數據,另一個進程稱爲消費者,他向緩衝區裏取數據,當生產者向緩衝區裏放數據是就必須進入掛起狀態,
知道消費者從緩衝區中取走數據後生產者才能繼續向緩衝區裏放數據,同樣當緩衝區中沒有數據時,消費者進程就必須進入掛起休眠狀態,直到生產者向緩衝區中放入數據時,消
費者才能喚醒繼續從緩衝區中取數據。
生產者與消費者是兩種角色,存在於一個交易場所,彼此之間存在三種關係。
一個交易場所:內存中具有存儲數據的一段有界的緩衝區;
兩種角色:生產者就是向交易場(緩衝區)所放東西(數據)的人,消費者就是從交易場所取東西的人。
三種關係:生產者與生產者之間存在競爭(或互斥)關係
生產者與消費者之間存在互斥與同步關係
消費者與消費者之間存在互斥關係
基於單鏈表的生產者消費者模型
當使用鏈表模擬生產者消費者模型時,我們可以藉助鏈表的插入來扮演生產者的角色,用鏈表的刪除來充當消費者的角色,接下來我們採用鏈表的頭插和頭刪操作來模擬
放數據和取數據的過程
代碼如下:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<pthread.h>
4 typedef struct _list{
5 struct _list *next;
6 int _val;
7 }product_list;
8 product_list *head=NULL;
9 static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
10 static pthread_cond_t need_product=PTHREAD_COND_INITIALIZER;
11 void init_list(product_list *list)
12 {
13 if(NULL!=list)
14 {
15 list->next=NULL;
16 list->_val=8;
17 }
18 }
19 void *consumer(void *_val)
20 {
21 product_list *p=NULL;
22 for(;;)
23 {
24 pthread_mutex_lock(&lock);
25 while(NULL==head)
26 {
27 pthread_cond_wait(&need_product,&lock);
28 }
29 p=head;
30 head=head->next;
31 p->next=NULL;
32 pthread_mutex_unlock(&lock);
33 printf("consum success,val is:%d\n",p->_val);
34 free(p);
35 p=NULL;
36 }
37 return NULL;
38 }
39 void *product(void *_val)
40 {
41 for(;;)
42 {
43 sleep(rand()%2);
44 product_list *p=malloc(sizeof(product_list));
45 pthread_mutex_lock(&lock);
46 init_list(p);
47 p->_val=rand()%1000;
48 p->next=head;
49 head=p;
50 pthread_mutex_unlock(&lock);
51 printf("call consumer! product success,val is:%d\n",p->_val);
52 pthread_cond_signal(&need_product);
53 }
54 }
55 int main()
56 {
57 pthread_t t_product;
58 pthread_t t_consumer;
59 pthread_create(&t_product,NULL,product,NULL);
60 pthread_create(&t_consumer,NULL,consumer,NULL);
61 pthread_join(t_product,NULL);
62 pthread_join(t_consumer,NULL);
63 return 0;
64 }
運行結果:
可以看出這是基於鏈表的後進先出的生產者消費者模型。
基於循環隊列的生產者消費者模型
先用一幅圖大概描述一下該模型:
這其中涉及到信號量(semaphore)的有關知識,首先我們需要掌握什麼是pv操作,pv操作時由p操作和v操作構成,他們都是原子操作(原子操作就是這個動
作要麼全做,要麼全不做),對於信號量的具體操作如下所示:
p(s):
(1)將信號量s的值進行減1操作;
(2)如果s>=0則該信號繼續執行,否則該進程等待掛起,直到有新進程釋放了,才能被喚醒。
v(s):
(1)將信號量的值進行加1操作;
(2)如果s>0,則該進程繼續執行,否則釋放隊列中第一個等待信號量的進程。
我們藉助信號量來實現環形隊列的生產者消費者者兩種角色之間的對資源的存取,其實隊列的底層數據存儲的方法就是一個數組,只要控制好對隊頭和隊尾得相關計算就可以實現循環隊列。
要實現這種生產模式,這裏還必須遵循以下規則:
1、生產者先運行
2、消費者永遠不能追上消費者
3、生產者不能將消費者超過一圈
代碼如下:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<pthread.h>
4 #include<semaphore.h>
5 #define _SEM_PRO_ 10
6 #define _SEM_COM_ 0
7 sem_t sem_product;
8 sem_t sem_consume;
9 int bank[_SEM_PRO_];
10 void* consumer(void *_val)
11 {
12 int c=0;
13 while(1)
14 {
15 sem_wait(&sem_consume);
16 int _consume=bank[c];
17 printf("consume done...,val is:%d\n",_consume);
18 sem_post(&sem_product);
19 c=(c+1)%_SEM_PRO_;
20 sleep(rand()%5);
21 }
22 }
23 void* producter(void *_val)
24 {
25 int p=0;
26 while(1)
27 {
28 sem_wait(&sem_product);
29 int _product=rand()%100;
30 bank[p]=_product;
31 printf("product done...,val is:%d\n,_product");
32 sem_post(&sem_consume);
33 p=(p+1)%_SEM_PRO_;
34 sleep(rand()%3);
35 }
36 }
37 void run_product_consume()
38 {
39 pthread_t tid_consumer;
40 pthread_t tid_producter;
41 pthread_create(&tid_consumer,NULL,consumer,NULL);
42 pthread_create(&tid_producter,NULL,producter,NULL);
43 pthread_join(tid_consumer,NULL);
44 pthread_join(tid_producter,NULL);
45 }
46 void destroy_all_sem(int val)
47 {
48 printf("process done...\n");
49 sem_destroy(&sem_product);
50 sem_destroy(&sem_consume);
51 exit(0);
52 }
53 void init_all_sem()
54 {
55 signal(2,destroy_all_sem);
56 int bank[_SEM_PRO_];
57 int _num=sizeof(bank)/sizeof(bank[0]);
58 int i=0;
59 for(;i<_num;i++)
60 {
61 bank[i]=0;
62 }
63 sem_init(&sem_product,0,_SEM_PRO_);
64 sem_init(&sem_consume,0,_SEM_COM_);
65 }
66 int main()
67{
68 init_all_sem();
69 run_product_consume();
70 return 0;
71 }