Mutual exclusion and synchronization processes - the producer and consumer issues

Purpose

Using the Windows API functions, you can write a program to solve the problem of producers and consumers to achieve mutual exclusion and synchronization processes.

Experimental procedure and content

  1. Mutual exclusion and synchronization processes. Write a program to simulate the producer and consumer threads, mutual exclusion and synchronization realization process.
  2. Use VC ++ 6.0 to achieve the above programming and debugging operations for producer and consumer threads success of the operation to provide some tips box.
  3. By reading and analysis of experimental procedures, familiar with the concept of mutual exclusion and synchronization of processes.

The main program structure and notes

#include<Windows.h>
#include<iostream>
#include<cstdlib>
#include<queue>
#include<time.h>
using namespace std;

const int N = 10;//缓冲区的大小
HANDLE m_S_Empty;// 生产者Semaphore
HANDLE m_S_Full;// 消费者Semaphore
HANDLE m_M_Mutex;;//互斥信号量
queue<int> buffer;//定义共享缓冲区
int i = 0;

DWORD WINAPI Producer(PVOID pParam) {
	
	while (1) {
	srand((int)time(0));
	int tmp = rand() % 100;
		if (WaitForSingleObject(m_S_Empty, INFINITE) == WAIT_OBJECT_0) {
			if (WaitForSingleObject(m_M_Mutex, INFINITE) == WAIT_OBJECT_0) {
				i++;
				if (i == 20)system("PAUSE");//当生产者执行20次以后系统暂停,观察输出情况
				buffer.push(tmp+i);
				printf("生产者放入的数字是:%d\n", tmp+i);
				printf("此时共享区的内容为:");
				//遍历输出队列内容
				for (int j = 0; j < buffer.size(); j++) {
					int x = buffer.front();
					buffer.pop();
					printf("%d ", x);
					buffer.push(x);
				}
				printf("\n");
				ReleaseMutex(m_M_Mutex);
				ReleaseSemaphore(m_S_Full, 1, NULL);
			}
		}
		if(i>10) Sleep(1500);//当生产者执行10次后,让生产者休眠,以此来模拟消费者不会从空共享区取数的情况
	}
	
}

DWORD WINAPI Consumer(PVOID pParam) {
	while (1)
	{
		if(WaitForSingleObject(m_S_Full, INFINITE) == WAIT_OBJECT_0) {
			if (WaitForSingleObject(m_M_Mutex, INFINITE) == WAIT_OBJECT_0) {
				int tmp = buffer.front();
				buffer.pop();
				printf("消费者拿出的数字是:%d\n", tmp);
				printf("此时共享区的内容为:");
				for (int j = 0; j < buffer.size(); j++) {
					int x = buffer.front();
					buffer.pop();
					printf("%d ", x);
					buffer.push(x);
				}
				printf("\n");
				ReleaseMutex(m_M_Mutex);
				ReleaseSemaphore(m_S_Empty, 1, NULL);
			}
		}
		if(i<10) Sleep(1500);//生产者执行10次前,让消费者休眠,以此来模拟生产者不会向已经满的共享区放数据
	}
}

int main() {
	CreateThread(NULL,0,Producer,0,0,0);
	//CreateThread(NULL, 0, Producer, 0, 0, 0);
	Sleep(2000);
	CreateThread(NULL, 0, Consumer, 0, 0, 0);
	//CreateThread(NULL, 0, Consumer, 0, 0, 0);
	m_S_Empty = CreateSemaphore(NULL, N, N, NULL);
	m_S_Full = CreateSemaphore(NULL, 0, N, NULL);
	m_M_Mutex = CreateMutex(NULL, FALSE, NULL);
	Sleep(INFINITE);//主线程永久休眠,让消费者和生产者执行
	//system("PAUSE");
}

Analysis of the results of program execution

The main problem consumers and producers need to verify three issues: the same point in time only one thread in a shared area, consumers will not extract data from an empty shared area, producers will not be put to the full data sharing area .

Based on this, we need to simulate three environments: 1) consumers and producers compete shared area; 2) shared area is empty; 3) share full.

The first environment is relatively easy to create, just create a producer-consumer threads can simultaneously access the same data. To simulate the second and third environments, the program established a mechanism: the producer before being executed 10 times, consumers need to be performed once every 1.5 seconds dormant, so producers try to put the region to share data; in after the execution of 10 producers, producers replaced each time you need to sleep 1.5 seconds, so that consumers try to extract data from the shared area.

FIG execution results are as follows.
Here Insert Picture Description
As shown, the case in which the red frame part is: the consumer thread sleep, producers have been put to the shared data area.

Yellow box part of the case is: shared area is full, so producers need to sleep after consumers wait until the end of the extracted data into the data area shared again.

Blue box case is part of: the producers began to sleep, consumers have to extract data from the shared area.

Purple frame part of the case is: shared area is empty, then consumers need to wait for the producer to extract data from the shared data area into a future.

In summary, it is considered that the program fully implement the requirements of consumers to producers of the issues raised.

Released five original articles · won praise 1 · views 282

Guess you like

Origin blog.csdn.net/weixin_44318192/article/details/102954668
Recommended