您的位置:首页 > 运维架构 > Linux

生产者与消费者模型实现

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息