生产者消费者实验.doc

生产者消费者实验.doc

生产者消费者实验 程序实现了两个生产者和两个消费者的生产者消费者问题(当生产者消费者数目再多时加上几个子进程就可以了)为了防止程序陷入死循环,给生产者限定了生产次数,同时为了方便截图,将次数限定的比较少。下面是我的程序: #include semaphore.h #include sys/types.h #include unistd.h #include fcntl.h #include stdio.h #include stdlib.h #include string.h #include errno.h #include sys/mman.h #include time.h #define BSIZE 8 typedef struct { int buffer[BSIZE]; sem_t lock; sem_t nready; sem_t nempty; int index; //next to put, next get is index -1 }shared_t; int get_one_item(shared_t *ptr) { int rval; sem_wait(ptr-nready); sem_wait(ptr-lock); rval = ptr-buffer[--ptr-index]; sem_post(ptr-lock); se

文档评论(0)

1亿VIP精品文档

相关文档