#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语言调用库函数实现生产者消费者问题的更多相关文章

  1. Go语言协程并发---生产者消费者实例

    package main import ( "fmt" "strconv" "time" ) /* 改进生产者消费者模型 ·生产者每秒生产一 ...

  2. python自动化--语言基础线程、生产者消费者示例

    进程与线程的区别:进程不共享空间,线程共享地址空间 线程共享空间优缺点:优点:多线程给用户的体验好些,打开时占用的内存比进程少缺点:共享地址空间会相互干扰,甚至有影响 import threading ...

  3. go语言实现生产者-消费者

    前言: 之前在学习操作系统的时候,就知道生产者-消费者,但是概念是模模糊糊的,好像是一直没搞明白. 其实很简单嘛,生产者生产,消费者进行消费,就是如此简单.了解了一下go语言的goroute,感觉实现 ...

  4. 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制

    通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...

  5. 并发、并行、同步、异步、全局解释锁GIL、同步锁Lock、死锁、递归锁、同步对象/条件、信号量、队列、生产者消费者、多进程模块、进程的调用、Process类、

    并发:是指系统具有处理多个任务/动作的能力. 并行:是指系统具有同时处理多个任务/动作的能力. 并行是并发的子集. 同步:当进程执行到一个IO(等待外部数据)的时候. 异步:当进程执行到一个IO不等到 ...

  6. Scala调用Kafka的生产者和消费者Demo,以及一些配置参数整理

    kafka简介 Kafka是apache开源的一款用Scala编写的消息队列中间件,具有高吞吐量,低延时等特性. Kafka对消息保存时根据Topic进行归类,发送消息者称为Producer,消息接受 ...

  7. 生产者消费者问题c语言实现

    #include <stdio.h> #include <process.h> #include <Windows.h> //信号量与关键段 CRITICAL_SE ...

  8. Linux线程编程之生产者消费者问题

    前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注意的事项.文中涉及的代码运行环境如下: 本文假定读者已具备线程同步的基础知识. 一  顺序表循环队列 1.1 ...

  9. Linux线程编程之生产者消费者问题【转】

    转自:http://www.cnblogs.com/clover-toeic/p/4029269.html 前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注 ...

随机推荐

  1. yum 配置详解(转发)

    一.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由yellow dog 这一发行版的 ...

  2. TypeScript 素描 - 模块解析、声明合并

    模块解析 模块解析有两种方式 相对方式  也就是以/或 ./或-/开头的,比如import jq  from "/jq" 非相对方式  比如 import model  from ...

  3. [WebGL入门]十九,遮挡剔除和深度測试

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外.鄙人webgl研究还不够深入,一些专业词语,假设翻译有误.欢迎大家指 ...

  4. (3)选择元素——(6)属性选择器(Attribute selectors)

    Attribute selectors are a particularly helpful subset of CSS selectors. They allow us to specify an ...

  5. java实现点名,并记录被点次数

    java实现点名,并记录被点次数 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStrea ...

  6. T-SQL和PL/SQL 区别

    结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统:同时也是数据库 ...

  7. ckplayer,超酷网页播放器,用于集成在网站中的播放器

    自己在工作中做了一个教学网站,点击左边课程,右边播放视频,经过源代码分析,用的就是这个播放器 网址:http://www.ckplayer.com/ 具体使用播放器网站上说的比较明白 div id=& ...

  8. sqlserver 执行远程数据库代码

    1.启用Ad Hoc Distributed Queries: exec sp_configure 'show advanced options',1reconfigureexec sp_config ...

  9. css链接,列表,表格

    1.css链接 a:link - 正常,未访问过的链接 a:visited - 用户已访问过的链接 a:hover - 当用户鼠标放在链接上时 a:active - 链接被点击的那一刻 注意: a:h ...

  10. 值栈和OGNL 之 7.1 值栈

    7.1  值栈 7.1.1  值栈是什么 简单的说:值栈是对应每一个请求对象的轻量级的内存数据中心. Struts2中一个很激动人心的特性就是引入了值栈,在这里统一管理着数据,供Action.Resu ...