注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

C语言生产者/消费者Producer/Consumer设计模式  

2013-04-05 22:57:09|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
/*
 * File : pc.c
 *
 * Title : Demo Producer/Consumer.
 *
 * Short : A solution to the producer consumer problem using
 * pthreads.
 *
 * Long :
 *
 * Author : Andrae Muys
 *
 * Date : 18 September 1997
 *
 * Revised :
 */

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define QUEUESIZE 10
#define LOOP 20

void *producer (void *args);
void *consumer (void *args);

typedef struct {
int buf[QUEUESIZE];
long head, tail;
int full, empty;
pthread_mutex_t *mut;
pthread_cond_t *notFull, *notEmpty;
} queue;

queue *queueInit (void);
void queueDelete (queue *q);
void queueAdd (queue *q, int in);
void queueDel (queue *q, int *out);

int main ()
{
queue *fifo;
pthread_t pro, con;

fifo = queueInit ();
if (fifo ==  NULL) {
fprintf (stderr, "main: Queue Init failed.\n");
exit (1);
}
pthread_create (&pro, NULL, producer, fifo);
pthread_create (&con, NULL, consumer, fifo);
pthread_join (pro, NULL);
pthread_join (con, NULL);
queueDelete (fifo);

return 0;
}

void *producer (void *q)
{
queue *fifo;
int i;

fifo = (queue *)q;

for (i = 0; i < LOOP; i++) {
pthread_mutex_lock (fifo->mut);
while (fifo->full) {
printf ("producer: queue FULL.\n");
pthread_cond_wait (fifo->notFull, fifo->mut);
}
queueAdd (fifo, i);
pthread_mutex_unlock (fifo->mut);
pthread_cond_signal (fifo->notEmpty);
usleep (100000);
}
for (i = 0; i < LOOP; i++) {
pthread_mutex_lock (fifo->mut);
while (fifo->full) {
printf ("producer: queue FULL.\n");
pthread_cond_wait (fifo->notFull, fifo->mut);
}
queueAdd (fifo, i);
pthread_mutex_unlock (fifo->mut);
pthread_cond_signal (fifo->notEmpty);
usleep (200000);
}
return (NULL);
}

void *consumer (void *q)
{
queue *fifo;
int i, d;

fifo = (queue *)q;

for (i = 0; i < LOOP; i++) {
pthread_mutex_lock (fifo->mut);
while (fifo->empty) {
printf ("consumer: queue EMPTY.\n");
pthread_cond_wait (fifo->notEmpty, fifo->mut);
}
queueDel (fifo, &d);
pthread_mutex_unlock (fifo->mut);
pthread_cond_signal (fifo->notFull);
printf ("consumer: recieved %d.\n", d);
usleep(200000);
}
for (i = 0; i < LOOP; i++) {
pthread_mutex_lock (fifo->mut);
while (fifo->empty) {
printf ("consumer: queue EMPTY.\n");
pthread_cond_wait (fifo->notEmpty, fifo->mut);
}
queueDel (fifo, &d);
pthread_mutex_unlock (fifo->mut);
pthread_cond_signal (fifo->notFull);
printf ("consumer: recieved %d.\n", d);
usleep (50000);
}
return (NULL);
}

#ifdef 0
typedef struct {
int buf[QUEUESIZE];
long head, tail;
int full, empty;
pthread_mutex_t *mut;
pthread_cond_t *notFull, *notEmpty;
} queue;
#endif

queue *queueInit (void)
{
queue *q;

q = (queue *)malloc (sizeof (queue));
if (q == NULL) return (NULL);

q->empty = 1;
q->full = 0;
q->head = 0;
q->tail = 0;
q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
pthread_mutex_init (q->mut, NULL);
q->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
pthread_cond_init (q->notFull, NULL);
q->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
pthread_cond_init (q->notEmpty, NULL);
return (q);
}

void queueDelete (queue *q)
{
pthread_mutex_destroy (q->mut);
free (q->mut);
pthread_cond_destroy (q->notFull);
free (q->notFull);
pthread_cond_destroy (q->notEmpty);
free (q->notEmpty);
free (q);
}

void queueAdd (queue *q, int in)
{
q->buf[q->tail] = in;
q->tail++;
if (q->tail == QUEUESIZE)
q->tail = 0;
if (q->tail == q->head)
q->full = 1;
q->empty = 0;

return;
}

void queueDel (queue *q, int *out)
{
*out = q->buf[q->head];

q->head++;
if (q->head == QUEUESIZE)
q->head = 0;
if (q->head == q->tail)
q->empty = 1;
q->full = 0;

return;
}





---------------------------------------------------------------------------
/* a simple producer/consumer using semaphores and threads

   usage on Solaris:
     gcc thisfile.c -lpthread -lposix4
     a.out numIters

*/
 
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#define SHARED 1

void *Producer(void *);  /* the two threads */
void *Consumer(void *);

sem_t empty, full;    /* the global semaphores */
int data;             /* shared buffer         */
int numIters;

/* main() -- read command line and create threads, then
             print result when the threads have quit */

int main(int argc, char *argv[]) {
  /* thread ids and attributes */
  pthread_t pid, cid;  
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

  numIters = atoi(argv[1]);
  sem_init(&empty, SHARED, 1);  /* sem empty = 1 */
  sem_init(&full, SHARED, 0);   /* sem full = 0  */

  printf("main started\n");
  pthread_create(&pid, &attr, Producer, NULL);
  pthread_create(&cid, &attr, Consumer, NULL);
  pthread_join(pid, NULL);
  pthread_join(cid, NULL);
  printf("main done\n");
}

/* deposit 1, ..., numIters into the data buffer */
void *Producer(void *arg) {
  int produced;
  printf("Producer created\n");
  for (produced = 0; produced < numIters; produced++) {
    sem_wait(&empty);
    data = produced;
    sem_post(&full);
  }
}

/* fetch numIters items from the buffer and sum them */
void *Consumer(void *arg) {
  int total = 0, consumed;
  printf("Consumer created\n");
  for (consumed = 0; consumed < numIters; consumed++) {
    sem_wait(&full);
    total = total+data;
    sem_post(&empty);
  }
  printf("for %d iterations, the total is %d\n", numIters, total);
}

  评论这张
 
阅读(1006)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016