#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. POJ 2392 Space Elevator DP

    该题与POJ 1742的思路基本一致:http://www.cnblogs.com/sevenun/p/5442279.html(多重背包) 题意:给你n个电梯,第i个电梯高h[i],数量有c[i]个 ...

  2. ubuntu 右键新建文档

    第一步.创建一个空白doc文档. 一定要是空白的,然后保存(另存为)到桌面,注意细节:1.保存到你能找到的地方,最好是桌面,以方便第二步拖动 2.注意下方的 “文件类型”,要选择doc格式. 3.注意 ...

  3. [bzoj1003][ZJOI2006][物流运输] (最短路+dp)

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  4. python之路-pip安装

    pip类似RedHat里面的yum,安装Python包非常方便   安装pip方法: 1.安装环境:ubuntu-14.04.2 sudo apt-get install python-pip pyt ...

  5. java与.net比较学习系列(1) 开发环境和常用调试技巧

    最近因为公司项目要由.net平台转到java平台的原因,之前一直用.net的我不得不开始学习java了,刚开始听到说要转java的时候很抗拒,因为我想专注在.net平台上,不过这样也并不完全是坏事,通 ...

  6. Golang性能调优入门

    如何利用golang自带的profile工具进行应用程序的性能调优,前一段时间我做的日志分析系统在线上遇到了一个问题,就是分任务的系统down机了,日志处理延迟了10几个小时,这个时候任务分发系统重启 ...

  7. JSON 学习笔记

    学习使用json过程随笔: json数组格式 var employees = [ { "firstName":"Bill" , "lastName&q ...

  8. JQuery 获取指定url对应的html内容

    用jquery的ajax类似的请求就可以了:比如: $.get("test.php", function(data){ alert("Data Loaded: " ...

  9. jquery模仿css3延迟效果

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  10. 关于scrollTop

    如下图