C语言调用库函数实现生产者消费者问题
#include<stdio.h>
#include<stdlib.h>
#include<semaphore.h>
#include<pthread.h>
#include<unistd.h> #define NumOf_Producer 5 //the max num of producer
#define NumOf_Consumer 10 //the max num of consumer
#define Maxnum 10 // the max num of product
sem_t Empty_sem; //the goal of whether the num of product is null
sem_t Full_sem; //the goal of whether the num of product is equal to Maxnum pthread_mutex_t Mutex; //the goal of whether someone use the buff int Producer_id = ;
int Consumer_id = ;
int NowNumOfProduce = ;
void *Producer(void *arg) //the thread of producer
{
int id = Producer_id++;
while()
{
sleep(0.1);
sem_wait(&Full_sem); //when it comes to zero ,it means that the num of product is equal to Maxnum
pthread_mutex_lock(&Mutex); //lock the buff
NowNumOfProduce++;
printf("Producerthread %d product one,the num is:%d \n",id%NumOf_Producer,NowNumOfProduce);
pthread_mutex_unlock(&Mutex);
sem_post(&Empty_sem); //when it comes to ten ,it means there are ten products can be used by consumer
} return ((void *));
} void *Consumer(void *arg)
{
int id = Consumer_id++;
while()
{
sleep(0.2);
sem_wait(&Empty_sem);
pthread_mutex_lock(&Mutex);
NowNumOfProduce--;
printf("Consumerthread %d use product one,the num is:%d \n",id%NumOf_Consumer,NowNumOfProduce);
pthread_mutex_unlock(&Mutex);
sem_post(&Full_sem);
}
return ((void *));
} int main()
{
pthread_t Con[NumOf_Consumer];
pthread_t Pro[NumOf_Producer]; int temp1 = sem_init(&Empty_sem,,);
int temp2 = sem_init(&Full_sem,,Maxnum);
if(temp1&&temp2!=)
{
printf("sem init failed \n");
exit();
} int temp3 = pthread_mutex_init(&Mutex,NULL); if(temp3!=)
{
printf("Mutex init failed \n");
exit();
} for(int i= ;i<NumOf_Producer;i++)
{
int temp4 = pthread_create(&Pro[i],NULL,Producer,(void *)&i);
if(temp4!=)
{
printf("thread create failed !\n");
exit();
}
} for(int i=;i<NumOf_Consumer;i++)
{
int temp5 = pthread_create(&Con[i],NULL,Consumer,(void *)&i);
if(temp5!=)
{
printf("thread create failed !\n");
}
exit();
}
//destroy the thread
for(int i=;i<NumOf_Consumer;i++)
{
pthread_join(Con[i],NULL);
} for(int i=;i<NumOf_Producer;i++)
{
pthread_join(Pro[i],NULL);
} return ;
}
说明:unisted.h是用来调用sleep,pthread.h是linux系统下线程编程的库,semaphore.h是使用信号灯的函数库,至于那些初始化方法,wait方法,post等可以去查查的。如果在编译程序时候出现类似sem_t未定义等问题,需要设置一下link链接,找到linux系统下安装的lib库中的libpthread.so导入进去就好了。
C语言调用库函数实现生产者消费者问题的更多相关文章
- Go语言协程并发---生产者消费者实例
package main import ( "fmt" "strconv" "time" ) /* 改进生产者消费者模型 ·生产者每秒生产一 ...
- python自动化--语言基础线程、生产者消费者示例
进程与线程的区别:进程不共享空间,线程共享地址空间 线程共享空间优缺点:优点:多线程给用户的体验好些,打开时占用的内存比进程少缺点:共享地址空间会相互干扰,甚至有影响 import threading ...
- go语言实现生产者-消费者
前言: 之前在学习操作系统的时候,就知道生产者-消费者,但是概念是模模糊糊的,好像是一直没搞明白. 其实很简单嘛,生产者生产,消费者进行消费,就是如此简单.了解了一下go语言的goroute,感觉实现 ...
- 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制
通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...
- 并发、并行、同步、异步、全局解释锁GIL、同步锁Lock、死锁、递归锁、同步对象/条件、信号量、队列、生产者消费者、多进程模块、进程的调用、Process类、
并发:是指系统具有处理多个任务/动作的能力. 并行:是指系统具有同时处理多个任务/动作的能力. 并行是并发的子集. 同步:当进程执行到一个IO(等待外部数据)的时候. 异步:当进程执行到一个IO不等到 ...
- Scala调用Kafka的生产者和消费者Demo,以及一些配置参数整理
kafka简介 Kafka是apache开源的一款用Scala编写的消息队列中间件,具有高吞吐量,低延时等特性. Kafka对消息保存时根据Topic进行归类,发送消息者称为Producer,消息接受 ...
- 生产者消费者问题c语言实现
#include <stdio.h> #include <process.h> #include <Windows.h> //信号量与关键段 CRITICAL_SE ...
- Linux线程编程之生产者消费者问题
前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注意的事项.文中涉及的代码运行环境如下: 本文假定读者已具备线程同步的基础知识. 一 顺序表循环队列 1.1 ...
- Linux线程编程之生产者消费者问题【转】
转自:http://www.cnblogs.com/clover-toeic/p/4029269.html 前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注 ...
随机推荐
- 【jquery插件】-网页下雪效果
又到了年底,从圣诞节开始,可以发现各大网站的活动是越来越多,都变的银装素裹,本人由于业务需求也需要做这么一个效果,所以就通过google大神找了一下相关资源,结果就出现了N种雪花效果的方式.种 ...
- LitJson处理Json
LitJSON是一个.NET平台下处理JSON格式数据的类库,小巧.快速.它的源代码使用C#编写,可以通过任何.Net平台上的语言进行调用,目前最新版本为LitJSON 0.9. 下载地址: http ...
- array模块
array模块定义了一种序列数据结构,看起来和list很相似,但是所有成员必须是相同基本类型. 2.1 array-固定类型数据序列 array作用是高效管理固定类型数值数据的序列. 2.2.1 初始 ...
- runtime的基本应用
1.什么是runtime? runtime是一套底层的C语言API,包含很多强大实用的C语言数据类型和C语言函数,平时我们编写的OC代码,底层都是基于runtime实现的. 2.runtime有什么作 ...
- 五分钟读懂UML类图
平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...
- 下拉框Html.DropDownList 和DropDownListFor 的经常用法
一.非强类型: Controller: ViewData["AreId"] = from a in rp.GetArea() ...
- StaggeredGridView+universal-image-loader载入网路图片实现瀑布流
StaggeredGridView 开源lib https://github.com/maurycyw/StaggeredGridView 文章demo下载地址 http://download.c ...
- android studio github 项目导入问题
在github上面看到一个比较好的项目,导入出现了一些问题,记录如下: 项目演示效果如图:下载地址:https://github.com/asijack/PagerSlidingTabStrip 如果 ...
- [Python学习笔记][第七章Python文件操作]
2016/1/30学习内容 第七章 Python文件操作 文本文件 文本文件存储的是常规字符串,通常每行以换行符'\n'结尾. 二进制文件 二进制文件把对象内容以字节串(bytes)进行存储,无法用笔 ...
- Js异步级联选择框实践方法
HTML: <li> <span>所在地区:</span> <select name="prov" id="ddl_prov&q ...