Windows实现生产者-消费者问题|操作系统

在这里插入图片描述
在这里插入图片描述

#include <Windows.h>
#include <iostream>
#define SIZE_OF_BUFFER 10
using namespace std;
typedef HANDLE semaphore;

#define P(S) WaitForSingleObject(S,INFINITE)
#define V(S) ReleaseSemaphore(S,1,NULL)

int g_buffer[10];
int in=0;
int out=0;
int ProduceID = 1000;
int CousumeID;

DWORD WINAPI Producer(LPVOID lpPara);
DWORD WINAPI Consumer(LPVOID lpPara);
semaphore g_hEmptySemaphore = CreateSemaphore(NULL, 10, 10, NULL);
semaphore g_hFullSemaphore = CreateSemaphore(NULL, 0, 10, NULL);
semaphore g_hMutex = CreateSemaphore(NULL, 1, 1, NULL);

void print();
int x=0;
int y=0;
void main()
{
	HANDLE hThread1;
	HANDLE hThread2;
	hThread1 = CreateThread(NULL, 0, Producer,NULL, 0, NULL);
	hThread2 = CreateThread(NULL, 0, Consumer,NULL, 0, NULL);
	while(1)
	{
			WaitForSingleObject(hThread1,INFINITE);
			WaitForSingleObject(hThread2,INFINITE);
	}

	Sleep(4000);
}

int color(int num)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),num);
	return 0;
}

void gotoxy(int x,int y)
{
	HANDLE hCon;
	hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD Pos;
	Pos.X = x;
	Pos.Y = y;
	SetConsoleCursorPosition(hCon,Pos);
}

void print()
{
	for(int i=0;i<SIZE_OF_BUFFER;i++)
	{
		gotoxy(40,i+5);
		if(i==in) 
			color(1);
		else if(i==out) 
			color(4);
		else 
			color(7);
		std::cout<<i<<":"<<g_buffer[i];
	}
}


//生产一个产品
void Produce()
{
	++ProduceID;
}

//把新生产的产品放入缓冲区
void Append()
{
	color(7);
	g_buffer[in]=ProduceID;
	if(y==25)
	{
		y=0;
		system("cls");
	}
	gotoxy(x,y++);
	std::cout<<"生产者生产了"<<g_buffer[in];
	std::cout<<std::endl;
	print();
	in=(in+1)%SIZE_OF_BUFFER;
}

//取出一个产品
void Take()
{
	color(7);
	CousumeID=g_buffer[out];
	if(y==25)
	{
		y=0;
		system("cls");
	}

	gotoxy(x,y++);
	std::cout<<"\t\t消费者消费了"<<CousumeID;
	g_buffer[out]=1000;
	print();
	out=(out+1)%SIZE_OF_BUFFER;
}


DWORD WINAPI Producer(LPVOID lpPara)
{
	while(1)
	{
		P(g_hEmptySemaphore);
		P(g_hMutex);
		Produce();
		Append();
		Sleep(1500);
		V(g_hMutex);
		V(g_hFullSemaphore);
	}
	return 0;
}

DWORD WINAPI Consumer(LPVOID lpPara)
{
	while(1)
	{
		P(g_hFullSemaphore);
		P(g_hMutex);
		Take();
		Sleep(1500);
		V(g_hMutex);
		V(g_hEmptySemaphore);
	}
	return 0;
}


  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
生产者消费者问题是一个经典的同步问题,用于描述多个线程之间的生产和消费关系。在Windows操作系统中,可以使用Windows API提供的信号量机制来实现生产者消费者问题的解决方案。 下面是一个使用C语言实现Windows生产者消费者问题的示例代码: ```c #include <windows.h> #include <stdio.h> #define BUFFER_SIZE 10 #define PRODUCER_LOOP 100 #define CONSUMER_LOOP 100 int buffer[BUFFER_SIZE]; int in = 0; int out = 0; int count = 0; HANDLE hSemEmpty, hSemFull, hMutex; DWORD WINAPI producer(LPVOID lpParam) { int i, data; for (i = 0; i < PRODUCER_LOOP; i++) { data = i; WaitForSingleObject(hSemEmpty, INFINITE); WaitForSingleObject(hMutex, INFINITE); buffer[in] = data; in = (in + 1) % BUFFER_SIZE; count++; printf("Producer %d produced data %d\n", GetCurrentThreadId(), data); ReleaseMutex(hMutex); ReleaseSemaphore(hSemFull, 1, NULL); } return 0; } DWORD WINAPI consumer(LPVOID lpParam) { int i, data; for (i = 0; i < CONSUMER_LOOP; i++) { WaitForSingleObject(hSemFull, INFINITE); WaitForSingleObject(hMutex, INFINITE); data = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; printf("Consumer %d consumed data %d\n", GetCurrentThreadId(), data); ReleaseMutex(hMutex); ReleaseSemaphore(hSemEmpty, 1, NULL); } return 0; } int main() { HANDLE hProducer[2], hConsumer[2]; DWORD producerId[2], consumerId[2]; hSemEmpty = CreateSemaphore(NULL, BUFFER_SIZE, BUFFER_SIZE, NULL); hSemFull = CreateSemaphore(NULL, 0, BUFFER_SIZE, NULL); hMutex = CreateMutex(NULL, FALSE, NULL); hProducer[0] = CreateThread(NULL, 0, producer, NULL, 0, &producerId[0]); hProducer[1] = CreateThread(NULL, 0, producer, NULL, 0, &producerId[1]); hConsumer[0] = CreateThread(NULL, 0, consumer, NULL, 0, &consumerId[0]); hConsumer[1] = CreateThread(NULL, 0, consumer, NULL, 0, &consumerId[1]); WaitForMultipleObjects(4, hProducer, TRUE, INFINITE); WaitForMultipleObjects(2, hConsumer, TRUE, INFINITE); CloseHandle(hSemEmpty); CloseHandle(hSemFull); CloseHandle(hMutex); return 0; } ``` 在这个例子中,定义了一个大小为10的缓冲区,两个生产者线程和两个消费者线程。生产者线程循环100次,每次产生一个数据并将其存入缓冲区,消费者线程循环100次,每次从缓冲区中取出一个数据进行消费。 使用Windows API提供的CreateSemaphore、WaitForSingleObject和ReleaseSemaphore函数实现信号量机制,使用CreateMutex函数实现互斥锁机制。 需要注意的是,在生产者消费者线程中使用WaitForSingleObject和ReleaseSemaphore函数需要注意顺序,不然会导致线程死锁或者数据混乱的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值