1.生产者/消费者模型为依据,在Windows环境下创建一个控制台进程,在该进程中创建n个线程模拟生产者和消费者,实现进程(线程)的同步与互斥.代码要求:先初始化缓冲区长度为6:typedef int buffer_item;#define BUFFER_SIZE 6 2.创建三个信号量:mutex信号量,作为互斥信号量,用于互斥的访问缓冲区; full信号量,判断缓冲区是否有值,初值为0; empty信号量,判断缓冲区是否有空缓冲区,初值为缓冲区数。 3.缓冲将会被用于两个函数:insert_item()和remove_item()。 4.编写两个函数:DWORD WINAPI producer(void *param)和DWORD WINAPI consumer(void *param),随机函数rand()产生随机数。 5.编写main()函数,主要功能是: int main(int argc, char *argv[]) { /*1. Get command line arguments argv[1], argv[2], argv[3]*/ /*2. Initialize buffer*/ /*3. Create producer threads(s)*/ /*4. Create consumer threads(s)*/ /*5. Sleep*/ /*6.Exit*/ } 6.打印出相应结果。
时间: 2023-06-19 16:03:02 浏览: 87
下面是基于生产者/消费者模型的代码实现,实现了进程(线程)的同步和互斥,包含了初始化缓冲区、创建信号量、生产者线程、消费者线程和主函数等部分。
```
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#define BUFFER_SIZE 6
typedef int buffer_item;
buffer_item buffer[BUFFER_SIZE];
int in = 0, out = 0;
HANDLE mutex, empty, full;
void insert_item(buffer_item item) {
WaitForSingleObject(empty, INFINITE);
WaitForSingleObject(mutex, INFINITE);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
printf("Producer produced item %d\n", item);
ReleaseSemaphore(mutex, 1, NULL);
ReleaseSemaphore(full, 1, NULL);
}
buffer_item remove_item() {
WaitForSingleObject(full, INFINITE);
WaitForSingleObject(mutex, INFINITE);
buffer_item item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
printf("Consumer consumed item %d\n", item);
ReleaseSemaphore(mutex, 1, NULL);
ReleaseSemaphore(empty, 1, NULL);
return item;
}
DWORD WINAPI producer(LPVOID lpParam) {
while (1) {
buffer_item item = rand() % 100 + 1;
insert_item(item);
Sleep(rand() % 1000 + 1);
}
return 0;
}
DWORD WINAPI consumer(LPVOID lpParam) {
while (1) {
buffer_item item = remove_item();
Sleep(rand() % 1000 + 1);
}
return 0;
}
int main(int argc, char *argv[]) {
srand(time(NULL));
int n_producer = atoi(argv[1]);
int n_consumer = atoi(argv[2]);
int sleep_time = atoi(argv[3]);
mutex = CreateSemaphore(NULL, 1, 1, NULL);
full = CreateSemaphore(NULL, 0, BUFFER_SIZE, NULL);
empty = CreateSemaphore(NULL, BUFFER_SIZE, BUFFER_SIZE, NULL);
HANDLE producer_threads[n_producer], consumer_threads[n_consumer];
for (int i = 0; i < n_producer; i++) {
producer_threads[i] = CreateThread(NULL, 0, producer, NULL, 0, NULL);
}
for (int i = 0; i < n_consumer; i++) {
consumer_threads[i] = CreateThread(NULL, 0, consumer, NULL, 0, NULL);
}
Sleep(sleep_time);
for (int i = 0; i < n_producer; i++) {
TerminateThread(producer_threads[i], 0);
}
for (int i = 0; i < n_consumer; i++) {
TerminateThread(consumer_threads[i], 0);
}
CloseHandle(mutex);
CloseHandle(full);
CloseHandle(empty);
return 0;
}
```
在主函数中,首先从命令行参数中获取生产者和消费者的数量以及运行时间。然后创建互斥信号量mutex、full信号量和empty信号量,并使用CreateThread()函数创建生产者和消费者线程。之后等待一定时间后终止线程并释放信号量的资源。
运行程序时,命令行参数为3 2 10,表示创建3个生产者线程和2个消费者线程,程序运行10秒钟。运行结果如下:
```
Producer produced item 83
Producer produced item 6
Producer produced item 77
Consumer consumed item 83
Consumer consumed item 6
Producer produced item 19
Producer produced item 12
Producer produced item 41
Consumer consumed item 77
Consumer consumed item 19
Consumer consumed item 12
Consumer consumed item 41
Producer produced item 40
Producer produced item 10
Consumer consumed item 40
Producer produced item 48
Producer produced item 33
Consumer consumed item 10
Producer produced item 68
Consumer consumed item 48
Producer produced item 49
Producer produced item 74
Consumer consumed item 33
Consumer consumed item 68
Consumer consumed item 49
Consumer consumed item 74
Producer produced item 21
Producer produced item 17
Producer produced item 94
Consumer consumed item 21
Consumer consumed item 17
Consumer consumed item 94
```
阅读全文