`

(第四章 2)生产者-消费者模型

 
阅读更多

输出大致像这样:

 

[hadoop@sam1 test]$ ./producer_consumer 
[P0] Producing 0 ...
[P1] Producing 0 ...
[P1] Producing 1 ...
[P0] Producing 1 ...
------> [C1] Comsuming 0 ...
------> [C1] Comsuming 0 ...
[P2] Producing 0 ...
[P2] Producing 1 ...
------> [C2] Comsuming 1 ...
------> [C0] Comsuming 1 ...
------> [C0] Comsuming 0 ...
------> [C1] Comsuming 1 ...
[P1] Producing 2 ...
[P1] Producing 3 ...
------> [C0] Comsuming 2 ...
------> [C0] Comsuming 3 ...
[P0] Producing 2 ...
[P2] Producing 2 ...
------> [C2] Comsuming 2 ...
[P2] Producing 3 ...
[P0] Producing 3 ...
------> [C1] Comsuming 2 ...
------> [C2] Comsuming 3 ...
------> [C2] Comsuming 3 ...

 

代码:

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include </usr/include/semaphore.h>

#define BUFF_SIZE   5		/* total number of slots */
#define NP          3		/* total number of producers */
#define NC          3		/* total number of consumers */
#define NITERS      4		/* number of items produced/consumed */

typedef struct {
    int buf[BUFF_SIZE];   /* shared var */
    int in;         	  /* buf[in%BUFF_SIZE] is the first empty slot */
    int out;        	  /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;     	  /* keep track of the number of full spots */
    sem_t empty;    	  /* keep track of the number of empty spots */
    sem_t mutex;    	  /* enforce mutual exclusion to shared data */
} sbuf_t;

sbuf_t shared;

void *Producer(void *arg)
{
    int i, item, index;

    //index是线程编号
    index = (int)arg;

    for (i=0; i < NITERS; i++) {

        /* Produce item */
        item = i;	

        /* Prepare to write item to buf ??*/

        /* If there are no empty slots, wait */
        sem_wait(&shared.empty); 
        /* If another thread uses the buffer, wait */
        sem_wait(&shared.mutex);

        shared.buf[shared.in] = item;
        shared.in = (shared.in+1)%BUFF_SIZE;
        printf("[P%d] Producing %d ...\n", index, item);
	fflush(stdout);

        /* Release the buffer */
        sem_post(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.full);

        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

void *Consumer(void *arg)
{
    /* Fill in the code here */
	int i,item,index;
	index=(int)arg;

	for(i=0;i<NITERS;i++){
		item=i;
		sem_wait(&shared.full);
		sem_wait(&shared.mutex);

		printf("------> [C%d] Comsuming %d ...\n",index,shared.buf[shared.out]);
		//fflush(stdout);	
		shared.out=(shared.out+1)%BUFF_SIZE;

		sem_post(&shared.mutex);
		sem_post(&shared.empty);
	}
	return NULL;
}

int main()
{
    pthread_t idP, idC;
    int index;

    //注意:
    //1. 信号量一定要初始化,否则程序卡住,而且不报错
    //2. value=1的信号量等同于互斥锁??
    sem_init(&shared.mutex,0,1);
    sem_init(&shared.full, 0, 0);
    sem_init(&shared.empty, 0, BUFF_SIZE);

    /* Insert code here to initialize mutex*/

    for (index = 0; index < NP; index++)
    {  
       /* Create a new producer */
       pthread_create(&idP, NULL, Producer, (void*)index);
    }

    /* Insert code here to create NC consumers */
	for(index=0;index<NC;index++){
		pthread_create(&idC,NULL,Consumer,(void*)index);
	}

    pthread_exit(NULL);//仅仅结束主线程,进程不结束(对其他子线程不影响)
}

 

分享到:
评论

相关推荐

    操作系统课程设计生产者和消费者问题源代码

    本实验所使用的生产者和消费者模型具有如下特点: 本实验的多个缓冲区不是环形循环的,也不要求按顺序访问。生产者可以把产品放到目前某一个空缓冲区中。 消费者只消费指定生产者的产品。 在测试用例文件中指定...

    操作系统实验报告-消费者生产者

    操作下系统的实验报告,第4个实验选的是那个设计一个消费者 生产者

    生产者和消费者问题程序

    本实验所使用的生产者和消费者模型具有如下特点: 本实验的多个缓冲区不是环形循环的,也不要求按顺序访问。生产者可以把产品放到目前某一个空缓冲区中。 消费者只消费指定生产者的产品。 在测试用例文件中指定...

    多核编程第6章-实例代码

    多核编程第6章-实例代码: 包含四个并行模型的实例代码,委托(boss-worker) 对等(peer to peer) 流水线(pipeline) 生产者-消费者(producer-consumer),仅是框架的实例代码不包含异常处理,容错等其他考虑。

    JAVA并发编程实践_中文版(1-16章全)_1/4

    5.3 阻塞队列和生产者一消费者模式 5.4 阻塞和可中断的方法 5.5 synchronizer 5.6 为计算结果建立高效、可伸缩的高速缓存 第2部分 构建并发应用程序 第6章 任务执行 6.1 在线程中执行任务 6.2 executor 框架 6.3 ...

    Java并发编程part2

    5.3 阻塞队列和生产者一消费者模式 5.4 阻塞和可中断的方法 5.5 synchronizer 5.6 为计算结果建立高效、可伸缩的高速缓存 第2部分 构建并发应用程序 第6章 任务执行 6.1 在线程中执行任务 6.2 executor 框架 6.3 ...

    实战Java高并发程序设计(第2版)PPT模板.pptx

    5并行模式与算法 5.1探讨单例模式 5.3生产者-消费者模式 5.5future模式 5.2不变模式 5.4高性能的生产者-消费者模式:无锁的实现 5.6并行流水线 01 02 03 04 05 06 实战Java高并发程序设计(第2版)PPT模板全文共25...

    Java并发编程实践part1

    5.3 阻塞队列和生产者一消费者模式 5.4 阻塞和可中断的方法 5.5 synchronizer 5.6 为计算结果建立高效、可伸缩的高速缓存 第2部分 构建并发应用程序 第6章 任务执行 6.1 在线程中执行任务 6.2 executor 框架 6.3 ...

    Java并发编程实战

    5.3 阻塞队列和生产者-消费者模式 5.3.1 示例:桌面搜索 5.3.2 串行线程封闭 5.3.3 双端队列与工作密取 5.4 阻塞方法与中断方法 5.5 同步工具类 5.5.1 闭锁 5.5.2 FutureTask 5.5.3 信号量 5.5.4 栅栏 5.6...

    Java 并发编程实战

    5.3 阻塞队列和生产者-消费者模式 5.3.1 示例:桌面搜索 5.3.2 串行线程封闭 5.3.3 双端队列与工作密取 5.4 阻塞方法与中断方法 5.5 同步工具类 5.5.1 闭锁 5.5.2 FutureTask 5.5.3 信号量 5.5.4 栅栏 5.6...

    Java并发编程实践 PDF 高清版

    5.3 阻塞队列和生产者一消费者模式 5.4 阻塞和可中断的方法 5.5 Synchronizer 5.6 为计算结果建立高效、可伸缩的高速缓存 第2部分 构建并发应用程序 第6章 任务执行 6.1 在线程中执行任务 6.2 Executor 框架 6.3 ...

    C#并行编程高级教程:精通.NET 4 Parallel Extensions中文(第2部分)

    4.1.3 理解并行的生产者-消费者模式 4.1.4 ConcurrentStack 4.1.5 将使用数组和不安全集合的代码转换为使用并发集合的代码 4.1.6 ConcurrentBag 4.1.7 IProducerConsumerCollection 4.1.8 BlockingCollection ...

    嵌入式Linux应用程序开发标准教程(第2版全)

    9.3 实验内容——“生产者消费者”实验 9.4 本章小结 9.5 思考与练习 第10章 嵌入式Linux网络编程 10.1 TCP/IP概述 10.1.1 OSI参考模型及TCP/IP参考模型 10.1.2 TCP/IP协议族 10.1.3 TCP和UDP 10.2 网络基础编程 ...

    并行计算导论(原书第2版).[美]Ananth Grama(带详细书签).pdf

    3.6.5 流水线模型或生产者-消费者模型 3.6.6 混合模型 3.7 书目评注 习题 第4章 基本通信操作 4.1 一对多广播以及多对一归约 4.1.1 环或线性阵列 4.1.2 格网 4.1.3 超立方体 4.1.4 平衡二叉树 4.1.5 算法...

    嵌入式Linux程序设计案例与实验教程-实例代码

    2.3.3 生产者-消费者模型简述22 2.3.4 缓冲区操作概述22 2.3.5 几个线程API23 实验2.3 Linux多线程使用实例——生产者-消费者协议24 2.4 进程创建以及进程间通信25 2.4.1 进程概述25 2.4.2 进程的相关...

    python入门到高级全栈工程师培训 第3期 附课件代码

    05 生产者消费者模型 06 第三次作业讲解 第20章 01 上节课回顾 02 装饰器基本理论 03 高阶函数使用 04 函数闭包 05 函数闭包装饰器基本实现 06 函数闭包加上返回值 07 函数闭包加上参数 08 函数闭包补充:解压序列...

    c语言课件ppt数据应用

    第7章 消费者、生产者与市场效率 第8章 应用:赋税的代价 第9章 应用:国际贸易 第4篇 公共部门经济学 第10章 外部性 第11章 公共物品和公共资源 第12章 税制的设计 第5篇 企业行为与产业组织 第13章 生产成本 第14...

    C#并行编程高级教程:精通.NET 4 Parallel Extensions中文(第一部分)

    4.1.3 理解并行的生产者-消费者模式 4.1.4 ConcurrentStack 4.1.5 将使用数组和不安全集合的代码转换为使用并发集合的代码 4.1.6 ConcurrentBag 4.1.7 IProducerConsumerCollection 4.1.8 BlockingCollection 4.1.9 ...

Global site tag (gtag.js) - Google Analytics