以生产者/消费者模型为依据,在Windows环境下创建一个控制台进程,在该进程中创建n个线程模拟生产者和消费者,实现进程(线程)的同步与互斥。他的核心代码相关: 1.先初始化缓冲区长度为6: /*buffer.h*/ 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()函数 ;6.打印出相应结果。
时间: 2023-06-19 08:03:21 浏览: 414
(Linux C)利用多进程或多线程模拟实现生产者/消费者问题。
以下是基于生产者/消费者模型的Windows控制台进程代码:
/* buffer.h */
typedef int buffer_item;
#define BUFFER_SIZE 6
/* Global variables */
buffer_item buffer[BUFFER_SIZE];
int buffer_index;
HANDLE mutex, full, empty;
DWORD producer_thread_id, consumer_thread_id;
/* Function prototypes */
int insert_item(buffer_item item);
int remove_item(buffer_item *item);
DWORD WINAPI producer(void *param);
DWORD WINAPI consumer(void *param);
int main()
{
/* Initialize buffer */
buffer_index = 0;
/* Create semaphores */
mutex = CreateSemaphore(NULL, 1, 1, NULL);
full = CreateSemaphore(NULL, 0, BUFFER_SIZE, NULL);
empty = CreateSemaphore(NULL, BUFFER_SIZE, BUFFER_SIZE, NULL);
/* Create threads */
HANDLE producer_thread = CreateThread(NULL, 0, producer, NULL, 0, &producer_thread_id);
HANDLE consumer_thread = CreateThread(NULL, 0, consumer, NULL, 0, &consumer_thread_id);
/* Wait for threads to finish */
WaitForSingleObject(producer_thread, INFINITE);
WaitForSingleObject(consumer_thread, INFINITE);
/* Close handles */
CloseHandle(producer_thread);
CloseHandle(consumer_thread);
CloseHandle(mutex);
CloseHandle(full);
CloseHandle(empty);
return 0;
}
int insert_item(buffer_item item)
{
/* Acquire empty semaphore */
WaitForSingleObject(empty, INFINITE);
/* Acquire mutex semaphore */
WaitForSingleObject(mutex, INFINITE);
/* Insert item into buffer */
buffer[buffer_index] = item;
buffer_index++;
/* Release mutex semaphore */
ReleaseSemaphore(mutex, 1, NULL);
/* Release full semaphore */
ReleaseSemaphore(full, 1, NULL);
return 0;
}
int remove_item(buffer_item *item)
{
/* Acquire full semaphore */
WaitForSingleObject(full, INFINITE);
/* Acquire mutex semaphore */
WaitForSingleObject(mutex, INFINITE);
/* Remove item from buffer */
buffer_index--;
*item = buffer[buffer_index];
/* Release mutex semaphore */
ReleaseSemaphore(mutex, 1, NULL);
/* Release empty semaphore */
ReleaseSemaphore(empty, 1, NULL);
return 0;
}
DWORD WINAPI producer(void *param)
{
buffer_item item;
while (TRUE)
{
/* Sleep for a random period of time */
Sleep(rand() % 1000);
/* Generate random item */
item = rand();
if (insert_item(item))
{
printf("Producer error\n");
}
else
{
printf("Producer produced %d\n", item);
}
}
return 0;
}
DWORD WINAPI consumer(void *param)
{
buffer_item item;
while (TRUE)
{
/* Sleep for a random period of time */
Sleep(rand() % 1000);
if (remove_item(&item))
{
printf("Consumer error\n");
}
else
{
printf("Consumer consumed %d\n", item);
}
}
return 0;
}
在该模型中,生产者线程随机生成一个项并插入缓冲区,消费者线程从缓冲区中删除一个项。mutex信号量用于互斥的访问缓冲区,full信号量用于判断缓冲区是否有值,empty信号量用于判断缓冲区是否有空缓冲区。在main函数中,创建了两个线程,分别为生产者和消费者线程,并等待线程完成。最后,关闭所有的句柄并返回0。执行该程序将会打印出相应的结果。
阅读全文