#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
#include<sys/mman.h>
#include<sys/types.h>
#define BSIZE 10
typedef struct buffer_s {
char buf[BSIZE];
int occupied;
int nextin;
int nextout;
pthread_mutex_t mutex;
pthread_cond_t more;
pthread_cond_t less;
}buffer_t;
void producer(buffer_t *b, char item) {
pthread_mutex_lock(&b->mutex);
while (b->occupied >= BSIZE)
pthread_cond_wait(&b->less, &b->mutex);
b->buf[b->nextin] = item;
b->nextin++;
b->nextin %= BSIZE;
b->occupied++;
pthread_cond_signal(&b->more);
pthread_mutex_unlock(&b->mutex);
}
char consumer(buffer_t *b) {
char item;
pthread_mutex_lock(&b->mutex);
while (b->occupied <= 0)
pthread_cond_wait(&b->more, &b->mutex);
item = b->buf[b->nextout];
b->nextout++;
b->nextout %= BSIZE;
b->occupied--;
pthread_cond_signal(&b->less);
pthread_mutex_unlock(&b->mutex);
return item;
}
void produce_driver(buffer_t *b) {
char item;
while (1) {
item = getchar();
if ('#' == item) {
producer(b, '\0');
break;
} else {
producer(b, (char)item);
}
}
}
void consume_driver(buffer_t *b) {
char item;
while (1) {
item = consumer(b);
if ('\0' == item)
break;
putchar(item);
}
}
int main(int argc, char *argv[]) {
int zfd;
buffer_t *buffer;
pthread_mutexattr_t mattr;
pthread_condattr_t cattr_less, cattr_more;
zfd = open("/dev/zero", O_RDWR);
buffer = (buffer_t*)mmap(NULL, sizeof(buffer_t), PROT_READ|PROT_WRITE, MAP_SHARED, zfd, 0);
buffer->occupied = buffer->nextin = buffer->nextout = 0;
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&buffer->mutex, &mattr);
pthread_condattr_init(&cattr_less);
pthread_condattr_setpshared(&cattr_less, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&buffer->less, &cattr_less);
pthread_condattr_init(&cattr_more);
pthread_condattr_setpshared(&cattr_more, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&buffer->more, &cattr_more);
if (fork() == 0) {
printf("this is child process:");
consume_driver(buffer);
} else {
printf("this is parent process:");
produce_driver(buffer);
}
return 0;
}
生产者消费者
最新推荐文章于 2024-04-24 00:06:45 发布