⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 producer_consumer.cpp

📁 操作系统生产者消费者多线程的编程实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				com_place++;

				_tprintf (_T("\nproducer2 produce one project and producers product"));
				printf("%d",pro_instan);
//				_tprintf (_T("\nnow there's "));
	//			printf("%d",com_place);
	//			_tprintf(_T(" in the common place"));
				_tprintf(_T("\nbuffer size is %d"),com_place);
			}
//			else 	if(pro_instan==K)
//			{
//				ReleaseMutex (mblock.mguard);
//				return 0;
//			}
		
		} 
		__finally 
		{ 
			_tprintf(_T("\nProducer2 leave\n"));
			ReleaseMutex (mblock.mguard);
//		Mutex++;
//			_tprintf(_T("\nProducer2 leave"));
		}
//		if(pro_instan==K)return 0;
	}
	return 0;
}

DWORD WINAPI consume (void *arg)
{
	DWORD ShutDown = 0;
	CHAR command[10];
	srand ((DWORD)time(NULL)); /* Seed the random # generator 	*/
	if(con_instan>=K)return 0;
	/* Consume the NEXT message when prompted by the user */
	while (!ShutDown&&con_instan<K) { /* This is the only thread accessing stdin, stdout */
		if(con_instan>=K)return 0;
			Sleep(rand()%500);
			WaitForSingleObject (mblock.mready, INFINITE);
			WaitForSingleObject (mblock.mguard, INFINITE);
			__try
//			if(Mutex>0)
			{
//				Mutex--;
				_tprintf(_T("\n\nConsumer1 Enter"));
//				if (!mblock.f_ready) _leave; /* Don't process a message twice */
				/* Wait for the event indicating a message is ready */
				if (com_place>0&&con_instan<K){
//					MessageDisplay (&mblock);
					mblock.nCons++;
					mblock.nLost = mblock.sequence - mblock.nCons;
					mblock.f_ready = 0; /* No new messages are ready */
					com_place--;
					con_instan++;
					_tprintf(_T("\nconsumer1 consume one product and consumers consume"));
					printf("%d",con_instan);
//					_tprintf(_T("\nnow there's "));
//					printf("%d",com_place);
//					_tprintf(_T(" in the common place"));
					_tprintf(_T("\nbuffer size is %d"),com_place);
				}
//				else 	if(con_instan==K)
//				{
//					ReleaseMutex (mblock.mguard);
//					return 0;
//				}
			} 
			__finally
			{ 
				_tprintf(_T("\nConsumer1 leave\n"));
				ReleaseMutex (mblock.mguard); 
//				Mutex++;
//				_tprintf(_T("\nConsumer1 leave"));
			}
//			if(con_instan==K)return 0;
//		} else {
//			_tprintf (_T("Illegal command. Try again.\n"));
//		}
	}
	return 0;		
}

DWORD WINAPI consume2 (void *arg)
{
	DWORD ShutDown = 0;
	CHAR command[10];
	srand ((DWORD)time(NULL)); /* Seed the random # generator 	*/
	if(con_instan>=K)return 0;
	/* Consume the NEXT message when prompted by the user */
	while (!ShutDown&&con_instan<K) { /* This is the only thread accessing stdin, stdout */
		if(con_instan>=K)return 0;
		Sleep(rand()%80);
			WaitForSingleObject (mblock.mready, INFINITE);
			WaitForSingleObject (mblock.mguard, INFINITE);
			__try 
//			if(Mutex>0)
			{
//				Mutex--;
				_tprintf(_T("\n\nConsumer2 Enter"));
//				if (!mblock.f_ready) _leave; /* Don't process a message twice */
				/* Wait for the event indicating a message is ready */
				if (com_place>0&&con_instan<K){
//					MessageDisplay (&mblock);
					mblock.nCons++;
					mblock.nLost = mblock.sequence - mblock.nCons;
					mblock.f_ready = 0; /* No new messages are ready */
					com_place--;
					con_instan++;
					_tprintf(_T("\nconsumer2 consume one product and consumers consume"));
					printf("%d",con_instan);
//					_tprintf(_T("\nnow there's "));
//					printf("%d",com_place);
//					_tprintf(_T(" in the common place"));
					_tprintf(_T("\nbuffer size is %d"),com_place);
				}
//				else	if(con_instan==K)
//				{
//					ReleaseMutex (mblock.mguard);
//					return 0;
//				}
			} 
			__finally 
			{
				_tprintf(_T("\nConsumer2 leave\n"));
				ReleaseMutex (mblock.mguard); 
//				Mutex++;
//				_tprintf(_T("\nConsumer2 leave"));
			}
//			if(con_instan==K)return 0;
//		} else {
//			_tprintf (_T("Illegal command. Try again.\n"));
//		}
	}
	return 0;		
}

DWORD WINAPI consume3 (void *arg)
{
	DWORD ShutDown = 0;
	CHAR command[10];
	srand ((DWORD)time(NULL)); /* Seed the random # generator 	*/
	if(con_instan>=K)return 0;
	/* Consume the NEXT message when prompted by the user */
	while (!ShutDown&&con_instan<K) { /* This is the only thread accessing stdin, stdout */
		if(con_instan>=K)return 0;
		Sleep(rand()%100);
			WaitForSingleObject (mblock.mready, INFINITE);
			WaitForSingleObject (mblock.mguard, INFINITE);
			__try 
//			if(Mutex>0)
			{
//				Mutex--;
				_tprintf(_T("\n\nConsumer3 Enter"));
//				if (!mblock.f_ready) _leave; /* Don't process a message twice */
				/* Wait for the event indicating a message is ready */
				if (com_place>0&&con_instan<K){
//					MessageDisplay (&mblock);
					mblock.nCons++;
					mblock.nLost = mblock.sequence - mblock.nCons;
					mblock.f_ready = 0; /* No new messages are ready */
					com_place--;
					con_instan++;
					_tprintf(_T("\nconsumer3 consume one product and consumers consume"));
					printf("%d",con_instan);
//					_tprintf(_T("\nnow there's "));
//					printf("%d",com_place);
//					_tprintf(_T(" in the common place"));
					_tprintf(_T("\nbuffer size is %d"),com_place);
				}
//				else	if(con_instan==K)
//				{
//					ReleaseMutex (mblock.mguard);
//					return 0;
//				}
			} 
			__finally 
			{ 
				_tprintf(_T("\nConsumer3 leave\n"));
				ReleaseMutex (mblock.mguard);
//				Mutex++;
//				_tprintf(_T("\nConsumer3 leave")); 
			}
//			if(con_instan==K)return 0;
//		} else {
//			_tprintf (_T("Illegal command. Try again.\n"));
//		}
	}
	return 0;		
}

DWORD WINAPI consume4 (void *arg)
{
	DWORD ShutDown = 0;
	CHAR command[10];
	srand ((DWORD)time(NULL)); /* Seed the random # generator 	*/
	/* Consume the NEXT message when prompted by the user */
	while (!ShutDown&&con_instan<K) { /* This is the only thread accessing stdin, stdout */
		Sleep(rand()/6500);
			WaitForSingleObject (mblock.mready, INFINITE);
			WaitForSingleObject (mblock.mguard, INFINITE);
			__try {_tprintf(_T("\nEnter"));
				if (!mblock.f_ready) _leave; /* Don't process a message twice */
				/* Wait for the event indicating a message is ready */
				if (com_place>0&&con_instan<K){
					MessageDisplay (&mblock);
					mblock.nCons++;
					mblock.nLost = mblock.sequence - mblock.nCons;
					mblock.f_ready = 0; /* No new messages are ready */
					com_place--;
					con_instan++;
					_tprintf(_T("\nconsumer4 consume one product and consumers consume"));
					printf("%d",con_instan);
					_tprintf(_T("\nnow there's "));
					printf("%d",com_place);
					_tprintf(_T(" in the common place"));
				}
//				else	if(con_instan==K)
//				{
//					ReleaseMutex (mblock.mguard);
//					return 0;
//				}
			} 
			__finally { ReleaseMutex (mblock.mguard); _tprintf(_T("\nleave")); }
//			if(con_instan==K)return 0;
//		} else {
//			_tprintf (_T("Illegal command. Try again.\n"));
//		}
	}
	return 0;		
}
void MessageFill (MSG_BLOCK *mblock)
{
	/* Fill the message buffer, and include checksum and timestamp	*/
	/* This function is called from the producer thread while it 	*/
	/* owns the message block mutex					*/
	
	DWORD i;
	
	mblock->checksum = 0;	
	for (i = 0; i < DATA_SIZE; i++) {
		mblock->data[i] = rand();
		mblock->checksum ^= mblock->data[i];
	}
	mblock->timestamp = time(NULL);
	return;
}

void MessageDisplay (MSG_BLOCK *mblock)
{
//	 Display message buffer, timestamp, and validate checksum	
//	 This function is called from the consumer thread while it 	
//	 owns the message block mutex					
	DWORD i, tcheck = 0;
	
	for (i = 0; i < DATA_SIZE; i++) 
		tcheck ^= mblock->data[i];
	_tprintf (_T("\nMessage number %d generated at: %s"), 
		mblock->sequence, _tctime (&(mblock->timestamp)));
	_tprintf (_T("First and last entries: %x %x\n"),
		mblock->data[0], mblock->data[DATA_SIZE-1]);
	if (tcheck == mblock->checksum)
		_tprintf (_T("GOOD ->Checksum was validated.\n"));
	else
		_tprintf (_T("BAD  ->Checksum failed. message was corrupted\n"));
		
	return;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -