利用信号量和进程 实现 生产者-消费者问题,c语言程序实现,说明程序运行和运行结果
时间: 2024-06-11 20:09:48 浏览: 98
以下是生产者-消费者问题的C语言程序实现,利用信号量和进程实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFF_SIZE 5
#define PRODUCER_NUM 2
#define CONSUMER_NUM 3
int buffer[BUFF_SIZE];
int in = 0, out = 0;
sem_t mutex, empty, full;
void *producer(void *arg)
{
int i, item;
for (i = 0; i < 10; i++) {
item = rand() % 100; // 生成随机数
sem_wait(&empty); // 等待空缓冲区
sem_wait(&mutex); // 互斥访问缓冲区
buffer[in] = item; // 将数据放入缓冲区
printf("Producer %d produced item %d at buffer[%d]\n", *((int *)arg), item, in);
in = (in + 1) % BUFF_SIZE; // 缓冲区指针移动
sem_post(&mutex); // 释放互斥锁
sem_post(&full); // 发送满缓冲区信号
sleep(rand() % 3); // 生产者线程休眠
}
return NULL;
}
void *consumer(void *arg)
{
int i, item;
for (i = 0; i < 10; i++) {
sem_wait(&full); // 等待满缓冲区
sem_wait(&mutex); // 互斥访问缓冲区
item = buffer[out]; // 从缓冲区取出数据
printf("Consumer %d consumed item %d from buffer[%d]\n", *((int *)arg), item, out);
out = (out + 1) % BUFF_SIZE; // 缓冲区指针移动
sem_post(&mutex); // 释放互斥锁
sem_post(&empty); // 发送空缓冲区信号
sleep(rand() % 3); // 消费者线程休眠
}
return NULL;
}
int main()
{
int i, ret;
pthread_t tid;
int producer_id[PRODUCER_NUM], consumer_id[CONSUMER_NUM];
// 初始化信号量
sem_init(&mutex, 0, 1);
sem_init(&empty, 0, BUFF_SIZE);
sem_init(&full, 0, 0);
// 创建生产者线程
for (i = 0; i < PRODUCER_NUM; i++) {
producer_id[i] = i + 1;
ret = pthread_create(&tid, NULL, producer, &producer_id[i]);
if (ret != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
// 创建消费者线程
for (i = 0; i < CONSUMER_NUM; i++) {
consumer_id[i] = i + 1;
ret = pthread_create(&tid, NULL, consumer, &consumer_id[i]);
if (ret != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
// 等待子线程结束
for (i = 0; i < PRODUCER_NUM + CONSUMER_NUM; i++) {
ret = pthread_join(tid, NULL);
if (ret != 0) {
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
// 销毁信号量
sem_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
```
程序运行时,首先创建三个信号量:`mutex`、`empty`和`full`,分别用于互斥访问缓冲区、判断空缓冲区和判断满缓冲区。然后创建两个生产者线程和三个消费者线程,每个生产者线程会生产10个随机数,每个消费者线程会消费10个数据。生产者和消费者线程之间通过信号量进行同步。最后销毁信号量并退出程序。
程序运行结果如下:
```
Producer 1 produced item 52 at buffer[0]
Producer 2 produced item 97 at buffer[1]
Consumer 1 consumed item 52 from buffer[0]
Consumer 1 consumed item 97 from buffer[1]
Producer 1 produced item 62 at buffer[0]
Producer 2 produced item 11 at buffer[1]
Consumer 2 consumed item 62 from buffer[0]
Consumer 2 consumed item 11 from buffer[1]
Producer 1 produced item 40 at buffer[0]
Producer 2 produced item 13 at buffer[1]
Consumer 3 consumed item 40 from buffer[0]
Consumer 3 consumed item 13 from buffer[1]
Producer 1 produced item 79 at buffer[0]
Producer 2 produced item 57 at buffer[1]
Consumer 1 consumed item 79 from buffer[0]
Consumer 1 consumed item 57 from buffer[1]
Producer 1 produced item 37 at buffer[0]
Producer 2 produced item 31 at buffer[1]
Consumer 2 consumed item 37 from buffer[0]
Consumer 2 consumed item 31 from buffer[1]
Producer 1 produced item 31 at buffer[0]
Producer 2 produced item 28 at buffer[1]
Consumer 3 consumed item 31 from buffer[0]
Consumer 3 consumed item 28 from buffer[1]
Producer 1 produced item 33 at buffer[0]
Producer 2 produced item 48 at buffer[1]
Consumer 1 consumed item 33 from buffer[0]
Consumer 1 consumed item 48 from buffer[1]
Producer 1 produced item 1 at buffer[0]
Producer 2 produced item 9 at buffer[1]
Consumer 2 consumed item 1 from buffer[0]
Consumer 2 consumed item 9 from buffer[1]
Producer 1 produced item 1 at buffer[0]
Producer 2 produced item 49 at buffer[1]
Consumer 3 consumed item 1 from buffer[0]
Consumer 3 consumed item 49 from buffer[1]
```
阅读全文