典型的进程间通信IPC问题-生产者消费者问题

10 篇文章 0 订阅
5 篇文章 0 订阅

本实例详细解释了生产者消费者问题的简易模型,对于同步互斥以及多线程处理此问题提出了一个较好的解决方案。

#include <stdio.h>
#include <pthread.h>
#define  MAX 10000000000    //定义缓冲区数量,就是生产品数量
pthread_mutex_t  the_mutex;
pthread_cond_t  condc,condp;
int buffer=0;

void *product(void *ptr)
{
    int i;
    for(i=1;i<MAX;++i)
    {
        pthread_mutex_lock(&the_mutex);//互斥使用缓冲区
        while(buffer!=0)
            pthread_cond_wait(&condp,&the_mutex);
        buffer=i;//数据放入缓冲区
        pthread_cond_signal(&condc);//唤醒消费者
        pthread_mutex_unlock(&the_mutex);//释放缓冲区
    }
    pthread_exit;
}

void *consumer(void *ptr)
{
    int i;
    for(i=1;i<MAX;++i)
    {
        pthread_mutex_lock(&the_mutex);//互斥加锁
        while(buffer==0)
            pthread_cond_wait(&condc,&the_mutex);
        buffer=0;
        pthread_cond_signal(&condp);//唤醒唤醒生产者
        pthread_mutex_unlock(&the_mutex);
    }
    pthread_exit(0);

}
int main(int argc,char ** argv)
{
    pthread_t  pro,con;
    pthread_mutex_init(&the_mutex,0);
    pthread_cond_init(&condp,0);
    pthread_cond_init(&condc,0);
    pthread_create(&con,0,consumer,0);
    pthread_create(&pro,0,product,0);
    pthread_join(pro,0);
    pthread_join(con,0);
    pthread_cond_destroy(&condc);
    pthread_cond_destroy(&condp);
    pthread_mutex_destroy(&the_mutex);
}

在main函数里面包含了线程处理问题时的基本操作,包含线程创建,互斥量声明,线程注册的基本API函数。本实例最多可参考《Unix环境高级编程》第十二章线程控制。

  • 但是,有一点,这样的模型有极大地可能会发生死锁现象。死锁造成的四个条件是
    • 形成互斥条件。每个资源要么已经分配了一个进程,要么就是可用的。
    • 占有和等待条件。已经得到了某个资源的进程可以请求新的资源。
    • 不可抢占条件,就是已经分配给一个进程的资源不能强制被抢占,他只能被占有他的进程显示主动的释放,
    • 环路等待条件,就是死锁发生时,系统一定有一个或两个以上的进程组成的一个环路,在循环等待资源。但是却没有释放资源。
    • 死锁发生是,四个条件肯定都满足。如果任何一条件不成立,那么也就不会发生死锁。

死锁如何避免呢?
同样有四种方式
- 忽略该问题,如果你忽略他,那么他也会忽略你,非著名鸵鸟算法。然而,这种想法也就是想想、
- 检测死锁并恢复,检测死锁是否发生,一旦发生,采取措施补救,亡羊补牢。
- 仔细对资源进行分配,动态避免死锁。
- 破坏死锁的四个必要条件,任意一个条件不满足那么,死锁就不会发生。

管程是什么?
- 一个管程是一个由过程,变量,及数据结构组成的一个集合。他们组成一个特殊的模块和软件包,程序可以在任意时刻调用管程中的过程,但他们不能再管程之外声明的过程中直接使用管程中的数据结构。另外,管程是概念语言,C语言不支持管程。

monitor example
    integer i;
    condition c;
    produce producer();


    end;
    producer  consumer();
end monitor;

以下是采用java实现管程方式的生产者消费者问题,说到底,其实就是采用了一个辅助类,

public class ProducerConsumer{
    static final int N=100;
    static producer p=new producer();
    static consumer c=new consumer();
    static our_monitor mon =new our_monitor();//创建新的管程
    public static void main (String args[]){
        p.start();
        c.start();
    }
    static class producer extend Thread {
        public  void run(){
            int item;
            while(true){
                item=produce_item();
                mon.insert(item);
            }
        }
        private int produce_item(){//生产活动中。。。}
    }
    static class consumer extend Thread{
        public void run(){
            int item ;
            while(true){
                item=mon.remove();
                consumer_item(item);
            }
        }
        private void consumer_item(int item){//消费。。。。}
    }

    static class our_monitor{
        private int buffer[]=new int[N];
        private int count=0,io=0,hi=0;//计数器和索引
        public synchronized void insert (int val){
            if(count==N) 
                go_to_sleep();
            buffer[hi]=val;
            hi=(hi+1)%N;
            count++;
            if(count==1)
                notify();
        }
        public synchronized int remove(){
            int val;
            if(count==0)
                go_to_sleep();
            val=buffer[io];
            io=(io+1)%N;
            count--;
            if(count=N-1)
                notify();//如果生产者在休眠,唤醒生产者
            return val;
        }
        private void go_to_sleep(){
            try{
                wait();
            }
            catch(interruptedException exc){}
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值