生产者消费者问题C语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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);
    

  信号量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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) ;
     
     
}

  

posted @   Clematis  阅读(723)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示