生产者消费者问题C语言
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#include<semaphore.h>
#include<unistd.h>
#define N 4
int buf[N]={0};
int putin=0;
int takeout=0;
//时延函数
void delay(int len){
int i=rand()%len;
int x;
while(i>0){
x=rand()%len;
while(x>0){
x--;
}
i--;
}
}
//生产者
void producer(){
while(1){
delay(50000);//模拟正在生产
int d=1+rand()%100;
buf[putin]=d; //入库
printf("Put %d the buffer at %d.\n",d,putin);
putin++;
if(putin==N){ // 超出范围
putin=0;
}
}
}
//消费者
void consumer(){
while(1){
delay(50000);//模拟活动
printf("Take out %d from the buffer at %d.\n",buf[takeout],takeout);
buf[takeout]=-1; // 此处产品已取出
takeout++;
if(takeout==N){ // 超出范围
takeout=0;
}
}
}
int main(){
pthread_t manufacturer;
pthread_t customer;
//创建线程
pthread_create(&manufacturer,NULL,(void*)producer,NULL);
pthread_create(&customer,NULL,(void*)consumer,NULL);
//阻塞当前线程直至t1,t2执行结束
pthread_join(manufacturer,NULL);
pthread_join(customer,NULL);
信号量
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#include<semaphore.h>
#include<unistd.h>
/*semaphore 函数库
设置 sem_t sign;
初始化 sem_init int sem_init(sem_t*sem,int pshared,int value) 类型,初值;
操作 sem_post:释放时+1,唤醒线程
sem_wait :使用时-1为0调用 ,则线程停止 —原子操作,不能被中断
释放 sem_destory
*/
#define N 4
int buf[N]={0};
int putin=0;
int takeout=0;
//设置信号量
sem_t unoccupied; //空位个数,满缓冲区时 ,其值0,阻止生产者存入商品
sem_t occupied; //产品个数 ,空 0,消费者取
//时延函数
void delay(int len){
int i=rand()%len;
int x;
while(i>0){
x=rand()%len;
while(x>0){
x--;
}
i--;
}
}
//生产者
void producer(){
while(1){
int d=1+rand()%100;
delay(50000);//模拟正在生产
sem_wait(&unoccupied) ; //可用空位的信号量-1
buf[putin]=d; //入库
printf("Put %d the buffer at %d.\n",d,putin);
putin++;
if(putin==N){ // 超出范围
putin=0;
}
sem_post(&occupied) ; //可取数据的信号量+1
}
}
//消费者
void consumer(){
while(1){
delay(50000);//模拟活动
sem_wait(&occupied) ; //可取空位的信号量-1
printf("Take out %d from the buffer at %d.\n",buf[takeout],takeout);
buf[takeout]=-1; // 此处产品已取出
takeout++;
if(takeout==N){ // 超出范围
takeout=0;
}
sem_post(&unoccupied) ; //可用空位的信号量+1
}
}
int main(){
srand(time(NULL)) ; //初始化随机函数
pthread_t manufacturer;
pthread_t customer;
//初始化信号量
sem_init(&unoccupied,0,N) ;
sem_init(&occupied,0,0) ;
//创建线程
pthread_create(&manufacturer,NULL,(void*)producer,NULL);
pthread_create(&customer,NULL,(void*)consumer,NULL);
//阻塞当前线程直至t1,t2执行结束
pthread_join(manufacturer,NULL);
pthread_join(customer,NULL);
//撤销信号量
sem_destroy(&unoccupied) ;
sem_destroy(&occupied) ;
}

浙公网安备 33010602011771号