Giter Club home page Giter Club logo

bounded-buffer-problem's Introduction

实现简单的消费者生产者

主要功能:生成produce()、消费consume()

先定义三个信号量empt(0)、full(100)、mtx(1)
mtx相当于是一把mutex锁,用于实现资源互斥,只允许一个进程访问资源,1表示可进入
empt表示的是生产者进程可使用的资源数
full表示的是生产者进程可使用的资源空位数

有两个很重要的细节
1、生产者和消费者必须先检查资源数再获得锁、在代码中就是先写full.wait()再写mtx.wait()或者先写empt.wait()再写mtx.wait()。因为如果先获得锁再检查资源数就有可能出现获得锁之后发现资源数小于0了,然后进程就挂起或睡眠,导致其他进程获得不了锁进不去生产资源唤醒该进程,而该进程又不释放锁,导致死锁
2、wait()必须先count减1再判断小于0然后再睡眠,signal()必须判断小于0再count加1然后再唤醒


bounded-buffer-problem's People

Contributors

zerohou avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.