生产者与消费者模型实现
2013-06-16 00:00
302 查看
采用linux多线程实现生产者与消费者模型
/** * 生产者消费者模型 * 只有一个生产者,一个消费者,一个缓冲区 **/ #include <pthread.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <semaphore.h> int pro; ///产品 int buf; ///缓冲区 sem_t empty; ///缓冲区可用资源数,对于生产者 sem_t full; ///产品可用资源数,对于消费者 void *producer(void *arg) { int i; for (i=0; i<10; i++) { sem_wait(&empty); sleep(1); pro++; ///生产一个产品 buf = pro; ///产品送往buf printf ("produce %d\n", buf); sem_post(&full); } return NULL; } void *customer(void *arg) { int i; for (i=0; i<10; i++) { sem_wait(&full); sleep(1); printf("customer %d\n", buf); ///从buf取出一个产品 sem_post(&empty); } return NULL; } int main(void) { pthread_t tid_prod, tid_cust; buf = 0; sem_init(&empty, 0, 1); ///初始化只有一个缓冲区可用 sem_init(&full, 0, 0); ///初始化为不可用 if (pthread_create(&tid_prod, NULL, producer, NULL) != 0) { perror("create producer pthread error!\n"); exit(1); } if (pthread_create(&tid_cust, NULL, customer, NULL) != 0) { perror("create customer pthread error!\n"); exit(1); } if (pthread_join(tid_prod, NULL) != 0) { perror("join producer pthread error!\n"); exit(1); } if (pthread_join(tid_cust, NULL) != 0) { perror("join customer pthread error!\n"); exit(1); } sem_destroy(&empty); sem_destroy(&full); return 0; }
/** * 生产者消费者模型 * 只有一个生产者,一个消费者,BUF_SIZE个缓冲区 **/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #define BUF_SIZE 5 int in, out; int buf[BUF_SIZE]; pthread_mutex_t mutex; ///生产者与消费者互斥访问缓冲区 sem_t empty; ///缓冲区可用资源数,对于生产者 sem_t full; ///产品可用资源数,对于消费者 void *producer(void *arg) { int i; for (i=1; i<=20; i++) { sem_wait(&empty); pthread_mutex_lock(&mutex); in = (in + 1) % BUF_SIZE; buf[in] = i; printf("produce %d\n", buf[in]); pthread_mutex_unlock(&mutex); sem_post(&full); } return NULL; } void *customer(void *arg) { int i; for (i=1; i<=20; i++) { sem_wait(&full); pthread_mutex_lock(&mutex); out = (out + 1) % BUF_SIZE; printf("customer %d\n", buf[out]); pthread_mutex_unlock(&mutex); sem_post(&empty); } return NULL; } int main(void) { pthread_t tid_prod, tid_cust; in = out = -1; sem_init(&empty, 0, BUF_SIZE); ///初始化为BUF_SIZE个缓冲可用 sem_init(&full, 0, 0); ///初始化为不可用 pthread_mutex_init(&mutex, NULL); if (pthread_create(&tid_prod, NULL, producer, NULL) != 0) { perror("create producer pthread error!\n"); exit(1); } if (pthread_create(&tid_cust, NULL, customer, NULL) != 0) { perror("create customer pthread error!\n"); exit(1); } if (pthread_join(tid_prod, NULL) != 0) { perror("join producer pthread error!\n"); exit(1); } if (pthread_join(tid_cust, NULL) != 0) { perror("join customer pthread error!\n"); exit(1); } sem_destroy(&empty); sem_destroy(&full); pthread_mutex_destroy(&mutex); return 0; }
/** * 生产者与消费者模型 * 一组生产者,一组消费者,BUF_SIZE个缓冲区 **/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #define BUF_SIZE 5 int pro; int in, out; int buf[BUF_SIZE]; pthread_mutex_t mutex; ///生产者与消费者互斥访问缓冲区 sem_t empty; ///缓冲区可用资源数,对于生产者 sem_t full; ///产品可用数,对于消费者 void *producer(void *arg) { int i; for (i=0; i<10; i++) { sem_wait(&empty); pthread_mutex_lock(&mutex); sleep(1); pro++; in = (in + 1) % BUF_SIZE; buf[in] = pro; printf("producer %d produce %d\n", *(int *)arg, buf[in]); pthread_mutex_unlock(&mutex); sem_post(&full); } return NULL; } void *customer(void *arg) { int i; for (i=0; i<10; i++) { sem_wait(&full); pthread_mutex_lock(&mutex); sleep(1); out = (out + 1) % BUF_SIZE; printf("customer %d custom %d\n", *(int *)arg, buf[out]); pthread_mutex_unlock(&mutex); sem_post(&empty); } return NULL; } int main(void) { int i; int no_p[3] = {1, 2, 3}; int no_c[3] = {1, 2, 3}; pthread_t tid_prod[3]; pthread_t tid_cust[3]; in = out = -1; pro = 0; sem_init(&empty, 0, BUF_SIZE); sem_init(&full, 0, 0); pthread_mutex_init(&mutex, NULL); for (i=0; i<3; i++) { if (pthread_create(&tid_prod[i], NULL, producer, &no_p[i]) != 0) { perror("create producer pthread error!\n"); exit(1); } } for (i=0; i<3; i++) { if (pthread_create(&tid_cust[i], NULL, customer, &no_c[i]) != 0 ) { perror("create customer pthread error!\n"); exit(1); } } for (i=0; i<3; i++) { if (pthread_join(tid_prod[i], NULL) != 0) { perror("join producer error!\n"); exit(1); } } for (i=0; i<3; i++) { if (pthread_join(tid_cust[i], NULL) != 0) { perror("join customer error!\n"); exit(1); } } sem_destroy(&empty); sem_destroy(&full); pthread_mutex_destroy(&mutex); return 0; }
相关文章推荐
- 生产者消费者模型实现<一>模拟实现
- 多进程 队列 实现生产者消费者模型 python 笔记
- 使用swoole实现生产者消费者模型(2)
- 使用swoole的Process实现生产者消费者模型
- 生产者消费者模型(多个生产者和多个消费者)JDK1.5之前实现版
- 利用生产者消费者模型实现大文件的拷贝
- 浅谈生产者消费者模型(Linux系统下的两种实现方法)
- Linux互斥与同步应用(三):posix线程实现单个生产者和单个消费者模型
- 简单生产者消费者模型(java实现)
- 简单的生产者和消费者模型——互斥锁和条件变量实现
- 生产者消费者模型实现<二>真实实现BlockingQueue
- 生产者-消费者模型的Java实现
- 互斥量和条件变量实现生产者消费者模型
- 使用swoole的Process实现生产者消费者模型
- TransferQueue实现生产者、消费者模型
- Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型
- 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)
- Java多线程学习之 BlockingQueue实现生产者和消费者模型
- 利用两个线程实现生产者消费者模型
- 传统消费者、生产者模型 syncrinized和Object的wait,notifyAll实现