采用多生产者,多消费者模型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 生产者
 */
P(nempty);
P(mutex);
// 写入一个空闲位置
V(mutex);
V(nstored);
 
/**
 * 消费者
 */
P(nstored);
P(mutex):
// 清空一个非空闲位置
V(mutex);
V(nempty);

全局性说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include    "unpipc.h"
 
#define NBUFF        10
#define MAXNTHREADS 100
 
int     nitems, nproducers, nconsumers;     /* read-only */
 
struct      /* data shared by producers and consumers */
{
    int   buff[NBUFF];
    int   nput;           /* item number: 0, 1, 2, ... */
    int   nputval;        /* value to store in buff[] */
    int   nget;           /* item number: 0, 1, 2, ... */
    int   ngetval;        /* value fetched from buff[] */
    sem_t mutex, nempty, nstored;     /* semaphores, not pointers */
} shared;
 
void    *produce(void *);
void    *consume(void *);
/* end globals */

主函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* include main */
int
main(int argc, char **argv)
{
    int     i, prodcount[MAXNTHREADS], conscount[MAXNTHREADS];
    pthread_t   tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS]; 
 
    if (argc != 4)
        err_quit("usage: prodcons4 <#items> <#producers> <#consumers>");
    nitems = atoi(argv[1]);
    nproducers = min(atoi(argv[2]), MAXNTHREADS);
    nconsumers = min(atoi(argv[3]), MAXNTHREADS);
 
    /* 4initialize three semaphores */
    Sem_init(&shared.mutex, 0, 1);
    Sem_init(&shared.nempty, 0, NBUFF);
    Sem_init(&shared.nstored, 0, 0);
 
    /* 4create all producers and all consumers */
    Set_concurrency(nproducers + nconsumers);
    for (i = 0; i < nproducers; i++)
    {
        prodcount[i] = 0;
        Pthread_create(&tid_produce[i], NULL, produce, &prodcount[i]);
    }
    for (i = 0; i < nconsumers; i++)
    {
        conscount[i] = 0;
        Pthread_create(&tid_consume[i], NULL, consume, &conscount[i]);
    }
 
    /* 4wait for all producers and all consumers */
    for (i = 0; i < nproducers; i++)
    {
        Pthread_join(tid_produce[i], NULL);
        printf("producer count[%d] = %d\n", i, prodcount[i]);
    }
    for (i = 0; i < nconsumers; i++)
    {
        Pthread_join(tid_consume[i], NULL);
        printf("consumer count[%d] = %d\n", i, conscount[i]);
    }
 
    Sem_destroy(&shared.mutex);
    Sem_destroy(&shared.nempty);
    Sem_destroy(&shared.nstored);
    exit(0);
}
/* end main */

生产者线程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* include produce */
void *
produce(void *arg)
{
    for ( ; ; )
    {
        Sem_wait(&shared.nempty);   /* wait for at least 1 empty slot */
        Sem_wait(&shared.mutex);
 
        if (shared.nput >= nitems)
        {
            Sem_post(&shared.nstored);  /* let consumers terminate */
            Sem_post(&shared.nempty);
            Sem_post(&shared.mutex);
            return(NULL);           /* all done */
        }
 
        shared.buff[shared.nput % NBUFF] = shared.nputval;
        shared.nput++;
        shared.nputval++;
 
        Sem_post(&shared.mutex);
        Sem_post(&shared.nstored);  /* 1 more stored item */
        *((int *) arg) += 1;
    }
}
/* end produce */

消费者线程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* include consume */
void *
consume(void *arg)
{
    int     i;
 
    for ( ; ; )
    {
        Sem_wait(&shared.nstored);  /* wait for at least 1 stored item */
        Sem_wait(&shared.mutex);
 
        if (shared.nget >= nitems)
        {
            Sem_post(&shared.nstored);
            Sem_post(&shared.mutex);
            return(NULL);           /* all done */
        }
 
        i = shared.nget % NBUFF;
        if (shared.buff[i] != shared.ngetval)
            printf("error: buff[%d] = %d\n", i, shared.buff[i]);
        shared.nget++;
        shared.ngetval++;
 
        Sem_post(&shared.mutex);
        Sem_post(&shared.nempty);   /* 1 more empty slot */
        *((int *) arg) += 1;
    }
}
/* end consume */
 

Unix IPC之Posix信号量实现生产者消费者的更多相关文章

  1. 【Windows】用信号量实现生产者-消费者模型

    线程并发的生产者-消费者模型: 1.两个进程对同一个内存资源进行操作,一个是生产者,一个是消费者. 2.生产者往共享内存资源填充数据,如果区域满,则等待消费者消费数据. 3.消费者从共享内存资源取数据 ...

  2. day34 python学习 守护进程,线程,互斥锁,信号量,生产者消费者模型,

    六 守护线程 无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁 需要强调的是:运行完毕并非终止运行 #1.对主进程来说,运行完毕指的是主进程代码运行完毕 #2.对主线程来说,运行完 ...

  3. Unix IPC之Posix消息队列(1)

    部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...

  4. Unix IPC之Posix消息队列(3)

    struct mq_attr { long mq_flags; /* message queue flag : 0, O_NONBLOCK */ long mq_maxmsg; /* max numb ...

  5. Unix IPC之Posix消息队列(2)

    /* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...

  6. 课程设计——利用信号量实现生产者-消费者问题(java)

    package cn.Douzi.ProductConsume; import java.util.LinkedList; import java.util.Queue; import java.ut ...

  7. Linux多线程实践(六)使用Posix条件变量解决生产者消费者问题

    前面的一片文章我们已经讲过使用信号量解决生产者消费者问题.那么什么情况下我们须要引入条件变量呢? 这里借用  http://www.cnblogs.com/ngnetboy/p/3521547.htm ...

  8. Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)

    本文主要内容: 信号量的实现 利用信号量解决哲学家用餐问题 利用信号量解决生产者消费者问题 一.信号量的实现 1.1 信号量结构 typedef struct { int value; struct ...

  9. Linux多线程实践(5) --Posix信号量与互斥量解决生产者消费者问题

    Posix信号量 Posix 信号量 有名信号量 无名信号量 sem_open sem_init sem_close sem_destroy sem_unlink sem_wait sem_post ...

随机推荐

  1. WEB下面路径的问题

    web 中的  /  到底代表什么? 绝对路径-以Web站点根目录为参考基础的目录路径.之所以称为绝对,意指当所有网页引用同一个文件时,所使用的路径都是一样的.相对路径-以引用文件之网页所在位置为参考 ...

  2. java中的悲观锁和乐观锁实现

    悲观锁就是认为并发时一定会有冲突发生,采用互斥的策略.比如java中的synchronized. 而乐观锁是假设并发时不会有冲突发生,如果发生冲突,则操作失败,并不断重试.乐观锁的机制就是CAS(Co ...

  3. 自定义UITableViewCell的方法

    1.纯XIB/storyboard自定义.对应一个Controller的storyboard上拖拽出一个自定义Cell,并加上ReuseIdentifitor 2.纯代码自定义,通过在contentV ...

  4. JS中的异步与回调

    问题的引出:在js中使用异步调用时,有可能会出现在异步的回调函数中设置调用之外的变量值,但在异步调用完成后去使用变量,却发现这些变量值并没有被成功设置的情况.如: google map中的地理编码,地 ...

  5. 内核:为了fan的健康,我的重新编译记录

    email: jiqingwu@gmail.com date: 2008-02-13 关键词:ubuntu cpu cpufreqd cpufrequtils 编译 内核 装上ubuntu7.10后, ...

  6. 20155212 2016-2017-2《Java程序设计》课程总结

    每周博客 每周作业链接汇总 预备作业一:专业理解.未来展望.期望的师生关系. 预备作业二:HOMEWORK-2 预备作业三:HOMEWORK-3 第一周作业:学习教材Chapter 1 Java平台概 ...

  7. R的农场 chebnear

    评测传送门 Description最近,R 终于获得了一片他梦寐以求的农场,但如此大的一片农场,想要做好防卫工作可不是一件容易的事.所以 R 购买了 N 个守卫,分别让他们站在一定的位置上(守卫不可移 ...

  8. 24、List三个子类的特点

    List的三个子类的特点 因为三个类都实现了List接口,所以里面的方法都差不多,那这三个类都有什么特点呢? ArrayList:底层数据结构是数组,查询快,增删慢.线程不安全,效率高. Vector ...

  9. Linux服务-nginx+nfs实现共享存储

    任务目标:一台服务器进行更改,其他两台服务器访问均同步 现在的情况是: web1.html文件访问的结果是web1 现在我在Web1这台机器上更改web1.html,内容为change in web1 ...

  10. 2016.5.19——Excel Sheet Column Title

    Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[ ...