生产者-消费者问题(The proceducer-consumer problem)的新手代码实现

生产者-消费者问题(The proceducer-consumer problem)的新手代码实现

作者注:
1.C语言实现;
2.只有分开的代码块,未编写进程创建代码;
3.新手代码可能会有问题,请读者在参考时也结合自己的分析进行阅读;
4.有问题在评论区说出你的看法~大佬都在评论区噢 ~

代码部分:

	#include <stdio.h>
	#include <stdlib.h>
	#include <math.h>
	#define num 3
	int mutex = 1;  //信号量
	int full = 0;   //满
	int empty = num;  //空
	int buffer[num] = { 0 };    //buffer
	int in = 0;     //producer-input
	int out = 0;    //consumer-output
	
	int main(void) {
	    
	    return 0;
	};
	
	/*   P、V操作   */
	void wait(int *parameter) {
	    (*parameter)--;
	}
	void signal(int* parameter) {
	    (*parameter)++;
	}
	
	/*  生产者   */
	void producer() {
	    do
	    {
	        wait(empty);    //取空间
	        wait(mutex);    //取互斥
	        //互斥mutex判断
	        while (1)
	        {
	            if (mutex<0)
	            {
	                //互斥等待
	                signal(mutex);
	            }
	            if (mutex>0)
	            {
	                wait(mutex);
	                break;
	            }
	        }
	        //空间剩余量判断:
	        while (1)
	        {
	            if (empty <= 0 && mutex <0)
	            {
	                signal(mutex);  //空间不够,让权等待
	            }
	            if (empty>0)
	            {
	                //空间够,重新获取cs区权限
	                while (1)
	                {
	                    if (mutex < 0)
	                    {
	                        //互斥等待
	                        signal(mutex);
	                    }
	                    if (mutex > 0)
	                    {
	                        wait(mutex);
	                        break;
	                    }
	                }
	                break;
	            }
	        }
	        
	        //执行produce操作:
	        puts("生产数字:");
	        scanf("%d", buffer[in]);
	        in = (in + 1) & num;
	        signal(mutex);
	        signal(full);
	    } while (1);
	}
	
	/*  消费者   */
	void consumer() {
	    do
	    {
	        wait(full);
	        wait(mutex);
	        //互斥mutex判断
	        while (1)
	        {
	            if (mutex < 0)
	            {
	                //互斥等待
	                signal(mutex);
	            }
	            if (mutex > 0)
	            {
	                wait(mutex);
	                break;
	            }
	        }
	        //空间剩余量判断:
	        while (1)
	        {
	            if (full <= 0 && mutex < 0)
	            {
	                signal(mutex);  //物品不够,让权等待
	            }
	            if (full > 0)
	            {
	                //空间够,重新获取cs区权限
	                while (1)
	                {
	                    if (mutex < 0)
	                    {
	                        //互斥等待
	                        signal(mutex);
	                    }
	                    if (mutex > 0)
	                    {
	                        wait(mutex);
	                        break;
	                    }
	                }
	                break;
	            }
	        }
	
	        //执行consume操作:
	        printf("%d's num = %d\n", out, buffer[out]);
	        out = (out + 1) % num;
	        signal(mutex);
	        signal(empty);
	    } while (1);
	}
	
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页