#include<pthread.h>
#include<stdio.h>
/*
设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制.
说明:有界缓冲区内设有20个存储单元,放入/取出的数据项设定为1-20这20个整型数.
设计要求:
(1)每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓冲区的全部内容,当前指针位置和生产者/消费者线程的标识符.
(2)生产者和消费者各有两个以上.
(3)多个生产者或多个消费者之间须有共享对缓冲区进行操作的函数代码.
提示:(1) 有界缓冲区可用数组实现.
*/
pthread_mutex_t mu;
pthread_cond_t cond;
//pthread_cond_wait(&clifd_cond, &clifd_mutex); pthread_cond_signal(&clifd_cond);
int g=0;
int i=0;
char gc[21];
void* create1(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=0&&g<20)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
printf("%lu生产1:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create2(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=0&&g<20)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
printf("%lu生产2:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create3(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=0&&g<20)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
printf("%lu生产3:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create4(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=1)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
printf("%lu消费1:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create5(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=1)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
printf("%lu消费2:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create6(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=1)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
printf("%lu消费3:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
int main(){
for(i=0;i<21;i++){gc[i]='0';}
pthread_t produce1,produce2,produce3,consumer1,consumer2,consumer3;
pthread_create(&consumer1,NULL,create4,NULL);
pthread_create(&consumer2,NULL,create5,NULL);
pthread_create(&consumer3,NULL,create6,NULL);
pthread_create(&produce1,NULL,create1,NULL);
pthread_create(&produce2,NULL,create2,NULL);
pthread_create(&produce3,NULL,create3,NULL);
pthread_cond_wait(&cond, &mu);
pthread_join(consumer2,NULL);
pthread_join(produce1,NULL);
pthread_join(produce2,NULL);
pthread_join(consumer1,NULL);
pthread_join(produce3,NULL);
pthread_join(consumer3,NULL);
return 0;
}
#include<stdio.h>
/*
设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制.
说明:有界缓冲区内设有20个存储单元,放入/取出的数据项设定为1-20这20个整型数.
设计要求:
(1)每个生产者和消费者对有界缓冲区进行操作后,即时显示有界缓冲区的全部内容,当前指针位置和生产者/消费者线程的标识符.
(2)生产者和消费者各有两个以上.
(3)多个生产者或多个消费者之间须有共享对缓冲区进行操作的函数代码.
提示:(1) 有界缓冲区可用数组实现.
*/
pthread_mutex_t mu;
pthread_cond_t cond;
//pthread_cond_wait(&clifd_cond, &clifd_mutex); pthread_cond_signal(&clifd_cond);
int g=0;
int i=0;
char gc[21];
void* create1(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=0&&g<20)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
printf("%lu生产1:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create2(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=0&&g<20)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
printf("%lu生产2:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create3(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=0&&g<20)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0')
{
printf("sc%d\n",i);
gc[i+1]='1';
j=1;break;
}
}
if(j==0){g=1;gc[1]='1';}else{g=i+1;}
printf("%lu生产3:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create4(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=1)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
printf("%lu消费1:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create5(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=1)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
printf("%lu消费2:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
void* create6(void *arg)//huoqu
{
while(1){
pthread_mutex_lock(&mu);
if(g>=1)
{
int j=0;
for(i=1;i<=19;i++)
{
if(gc[i]=='1'&&gc[i+1]=='0'){j=1;gc[i]='0';break;}//
}
if(j!=1 && gc[20]!='1'){gc[1]='0';g = 0;}else if(j==0 && gc[20]=='1'){gc[20]='0';}else{g = i-1;}
printf("%lu消费3:%s g is:%d\n",pthread_self(),gc,g);
pthread_cond_signal(&cond);
}else{pthread_cond_wait(&cond, &mu);}
pthread_mutex_unlock(&mu);
}
}
int main(){
for(i=0;i<21;i++){gc[i]='0';}
pthread_t produce1,produce2,produce3,consumer1,consumer2,consumer3;
pthread_create(&consumer1,NULL,create4,NULL);
pthread_create(&consumer2,NULL,create5,NULL);
pthread_create(&consumer3,NULL,create6,NULL);
pthread_create(&produce1,NULL,create1,NULL);
pthread_create(&produce2,NULL,create2,NULL);
pthread_create(&produce3,NULL,create3,NULL);
pthread_cond_wait(&cond, &mu);
pthread_join(consumer2,NULL);
pthread_join(produce1,NULL);
pthread_join(produce2,NULL);
pthread_join(consumer1,NULL);
pthread_join(produce3,NULL);
pthread_join(consumer3,NULL);
return 0;
}