生产者消费者问题c语言_C中的生产者消费者问题

2023-05-16

生产者消费者问题c语言

Here you will learn about producer consumer problem in C.

在这里,您将了解C语言中的生产者消费者问题。

Producer consumer problem is also known as bounded buffer problem. In this problem we have two processes, producer and consumer, who share a fixed size buffer. Producer work is to produce data or items and put in buffer. Consumer work is to remove data from buffer and consume it. We have to make sure that producer do not produce data when buffer is full and consumer do not remove data when buffer is empty.

生产者使用者问题也称为有界缓冲区问题。 在这个问题中,我们有两个过程,生产者和消费者,它们共享一个固定大小的缓冲区。 生产者的工作是生产数据或项目并放入缓冲区。 消费者的工作是从缓冲区中删除数据并使用它。 我们必须确保当缓冲区已满时生产者不会产生数据,而当缓冲区为空时使用者不会删除数据。

Also Read: Banker’s Algorithm in C

另请参阅: C语言中的银行家算法

The producer should go to sleep when buffer is full. Next time when consumer removes data it notifies the producer and producer starts producing data again. The consumer should go to sleep when buffer is empty. Next time when producer add data it notifies the consumer and consumer starts consuming data. This solution can be achieved using semaphores.

当缓冲区已满时,生产者应进入睡眠状态。 下次消费者删除数据时,它将通知生产者,然后生产者再次开始生产数据。 当缓冲区为空时,消费者应入睡。 生产者下一次添加数据时,会通知消费者,消费者开始使用数据。 可以使用信号量实现此解决方案。

Image Source

图片来源

C中的生产者消费者问题 (Producer Consumer Problem in C)

Below is the program to implement this problem.

下面是实现此问题的程序。

#include<stdio.h>
#include<stdlib.h>
 
int mutex=1,full=0,empty=3,x=0;
 
int main()
{
	int n;
	void producer();
	void consumer();
	int wait(int);
	int signal(int);
	printf("\n1.Producer\n2.Consumer\n3.Exit");
	while(1)
	{
		printf("\nEnter your choice:");
		scanf("%d",&n);
		switch(n)
		{
			case 1:	if((mutex==1)&&(empty!=0))
						producer();
					else
						printf("Buffer is full!!");
					break;
			case 2:	if((mutex==1)&&(full!=0))
						consumer();
					else
						printf("Buffer is empty!!");
					break;
			case 3:
					exit(0);
					break;
		}
	}
	
	return 0;
}
 
int wait(int s)
{
	return (--s);
}
 
int signal(int s)
{
	return(++s);
}
 
void producer()
{
	mutex=wait(mutex);
	full=signal(full);
	empty=wait(empty);
	x++;
	printf("\nProducer produces the item %d",x);
	mutex=signal(mutex);
}
 
void consumer()
{
	mutex=wait(mutex);
	full=wait(full);
	empty=signal(empty);
	printf("\nConsumer consumes item %d",x);
	x--;
	mutex=signal(mutex);
}

Output

输出量

1.Producer 2.Consumer 3.Exit Enter your choice:1

1. 生产者2. 消费者 3. 退出 输入您的选择:1

Producer produces the item 1 Enter your choice:2

生产者生产项目1 输入您的选择:2

Consumer consumes item 1 Enter your choice:2 Buffer is empty!! Enter your choice:1

消费者消费项目1 输入您的选择:2 缓冲区为空! 输入您的选择:1

Producer produces the item 1 Enter your choice:1

生产者生产项目1 输入您的选择:1

Producer produces the item 2 Enter your choice:1

生产者生产项目2 输入您的选择:1

Producer produces the item 3 Enter your choice:1 Buffer is full!! Enter your choice:3

生产者生产项目3 输入您的选择:1 缓冲区已满! 输入您的选择:3

翻译自: https://www.thecrazyprogrammer.com/2016/09/producer-consumer-problem-c.html

生产者消费者问题c语言

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

生产者消费者问题c语言_C中的生产者消费者问题 的相关文章

  • mac下Tomcat启动成功后浏览器输入localhost:8080拒绝连接的解决办法

    今天根据大佬们的安装教程将Tomcat安装并成功启动 xff0c 而在浏览器中输入localhost xff1a 8080后却出现了 localhost拒绝了我们的连接请求 的字样 xff0c 网上的解决方案五花八门 xff0c 水平也是参
  • Linux中的软件管理

    Linux中的软件管理 yum源头 定义 xff1a yum源是一个软件集合地 xff0c 只需要搜索并安装你想要的软件 yum的全称是Yellowdog Updater Modified xff0c 是一个shell前端软件包管理器 xf
  • Spring拦截器HandlerInterceptor和HandlerInterceptorAdapter

    参考 https blog csdn net zhibo lv article details 81699360 https www cnblogs com jing99 p 11147152 html HandlerInterceptor
  • Java多线程超详解

    引言 随着计算机的配置越来越高 xff0c 我们需要将进程进一步优化 xff0c 细分为线程 xff0c 充分提高图形化界面的多线程的开发 这就要求对线程的掌握很彻底 那么话不多说 xff0c 今天本帅将记录自己线程的学习 程序 xff0c
  • 如何在Ubuntu 20.04上使用UFW设置防火墙

    介绍 Introduction UFW or Uncomplicated Firewall is a simplified firewall management interface that hides the complexity of
  • 人工智能主要分支

    人工智能主要分支 1 主要分支介绍 通讯 感知与行动是现代人工智能的三个关键能力 xff0c 在这里我们将根据这些能力 应用对这三个技术领域进行介绍 xff1a 计算机视觉 CV 人脸识别 自然语言处理 NLP 语音识别 语义识别 在 NL
  • 解决文件上传过大报错The field file exceeds its maximum permitted size of 1048576 bytes.问题

    报错内容如下 只需要在相应的yml配置文件spring下增加相关配置即可
  • 我有一个IT梦

    介绍 作为一名大二的学生 xff0c 接触计算机基础技术近乎俩年 xff0c 俩年来我愈加发觉计算机是一门发展力很强的学科 它多式多样 xff0c 更像是一种挑战 xff0c 对于好强的我来说 xff0c 越来越着迷计算机的世界 纵然未知的
  • Nginx安装教程

    前言 xff1a 同步文章图片有问题想看带有图片版的请移步 xff1a https www yuque com docs share 3fbd7d5a 639c 4ca8 8500 00071b7cb23d BvpWF 本篇文章涉及ngin
  • Kali-Linux-2020.1 设置中文,汉化。

    Kali Linux 2020 1 设置中文 xff0c 汉化 Kali Linux团队在Twitter上宣布 xff1a 新的一年是进行重大改变的好时机 xff0c 因此 xff0c 我们宣布在 即将发布的2020 1版本中 xff0c
  • PowerShell压缩和解压ZIP文件

    压缩 Compress Archive Path D File DestinationPath E File zip 解压 Expand Archive Path E File zip DestinationPath D File
  • python实现基本算法之归并排序(Merge sort)

    基本算法之归并排序 Merge sort 基本算法 04 归并排序 Merge sort 算法 往期请看选择排序 xff0c 插入排序 xff0c 归并排序 xff0c 快速排序等等都发布的 xff01 欢迎大家批评指正 xff01 文章目
  • mysql 异步复制VS半同步复制

    MySQL数据复制原理 异步复制 xff1a 默认情况下 xff0c MySQL的复制是异步复制 xff0c 主服务器及其从服务器是独立的 异步复制可以提供最佳的性能 xff0c 主服务器将更新的数据写入二进制日志 xff08 Binlog
  • mysql读写分离

    读写分离 master xff1a 192 span class token punctuation span 168 span class token punctuation span 2 span class token punctua
  • 在linux中关闭防火墙

    在linux中关闭防火墙 selinux xff08 secure linux 安全的linux xff09 selinux 是linux下的安全措施机制 xff0c 用来保护linux系统的安全 相当于另外一个安全工具 span clas
  • Google Payments?

    The news broke late last week by way of the Wall Street Journal with rumors of a payments service akin to PayPal forthco
  • 数据库中某个表中的某个字段的值是用逗号隔开的多个值,根据逗号拆分并从另一个表中查出数据返回

    两个表的结构如下 a表 b 表 关系说明 b teacherid 61 a user id 查询思路 xff1a FIND IN SET str strlist xff0c 该函数用于判断 str 是否在 strlist 中 xff0c 如
  • 一个玩游戏的失足青年,转行做游戏开发到教育的挣扎过程

    14年的IT从业经历 xff0c 中专毕业后在小镇上开过网吧 在网吧一年多的时间里 xff0c 天天陪人玩游戏 xff0c 后来去读了一个三流计算机专业 xff0c 毕业后转做软件开发 xff0c 最近五年转入游戏开发行业 xff01 从网
  • minikube的部署和安装,排错

    minikube的部署和安装 安装minikube的步骤 环境准备 xff1a 虚拟机至少2个cpu核心 xff0c 2G内存 xff0c 磁盘20G 推荐使用2个cpu核心 xff0c 4G的内存 xff0c 100G的磁盘空间 前期 x
  • lvm逻辑卷

    lvm示例应用 案例描述 xff1a 增加四块scsi硬盘 xff0c 每块100G xff0c 并构建lvm逻辑卷 xff0c 挂载到相应目录下 传统的分区方式 常用命令 临时添加IP地址 xff1a ip add add 192 168

随机推荐