(1)编程实现生产者-消费者问题的模拟(建议用C语言编写源代码)。 基本要求: (1)生产者消费者对缓冲区进行互斥操作。 (2)缓冲区大小为10,缓冲区满则不允许生产者生产数据,缓冲区空则不允许消费者消费数据。 (3)生产者消费者各循环操作10次。
时间: 2024-05-10 18:14:50 浏览: 14
以下是使用C语言实现生产者-消费者问题的模拟的源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 10
#define PRODUCER_NUM 1
#define CONSUMER_NUM 1
int buffer[BUFFER_SIZE];
int count = 0;
int in = 0;
int out = 0;
pthread_mutex_t mutex;
sem_t empty;
sem_t full;
void *producer(void *arg)
{
int id = *(int *)arg;
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < BUFFER_SIZE; j++)
{
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = rand() % 100;
printf("Producer %d produces %d at buffer[%d]\n", id, buffer[in], in);
in = (in + 1) % BUFFER_SIZE;
count++;
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
pthread_exit(NULL);
}
void *consumer(void *arg)
{
int id = *(int *)arg;
int i, j, item;
for (i = 0; i < 10; i++)
{
for (j = 0; j < BUFFER_SIZE; j++)
{
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
printf("Consumer %d consumes %d from buffer[%d]\n", id, item, out);
out = (out + 1) % BUFFER_SIZE;
count--;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
pthread_exit(NULL);
}
int main()
{
int i, j;
int producer_id[PRODUCER_NUM], consumer_id[CONSUMER_NUM];
pthread_t producer_thread[PRODUCER_NUM], consumer_thread[CONSUMER_NUM];
pthread_mutex_init(&mutex, NULL);
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
for (i = 0; i < PRODUCER_NUM; i++)
{
producer_id[i] = i;
pthread_create(&producer_thread[i], NULL, producer, &producer_id[i]);
}
for (i = 0; i < CONSUMER_NUM; i++)
{
consumer_id[i] = i;
pthread_create(&consumer_thread[i], NULL, consumer, &consumer_id[i]);
}
for (i = 0; i < PRODUCER_NUM; i++)
{
pthread_join(producer_thread[i], NULL);
}
for (i = 0; i < CONSUMER_NUM; i++)
{
pthread_join(consumer_thread[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
```
此代码使用了两个信号量`empty`和`full`分别表示缓冲区空和满,使用互斥锁`mutex`进行对缓冲区的互斥操作。生产者在缓冲区满之前不断往里面放数据,消费者在缓冲区空之前不断从里面取数据。程序最终输出了生产者和消费者的操作日志,以及缓冲区中的数据。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)