Undergraduate operating system class exercise 3 (and water taking problem C++ Windows code multithreaded semaphore)

Keywords: Windows

There are several little monks and old monks in a temple. There is a water tank. The little monk carries water into the tank, and the old monk takes water from the tank for drinking. The water tank can hold 10 barrels of water. The water is taken from the same water well. The diameter of the well is narrow, and only one bucket can be used for water intake at a time. The total number of buckets is 3, and each time the water in and out of the tank is only 1 bucket, which can not be carried out at the same time. Try to give the algorithm description of water intake and inflow.

Single cpp file code:

#include <iostream>
#include <windows.h>
using namespace std;

//Set the initial value of the quantity of water in the cylinder and the number of barrels
int gang = 10, tong = 3;

//Declare semaphore variable (initial value depends on initial value of global variable)
HANDLE mutex1 = CreateMutex(NULL, FALSE, NULL);//Two little monks are not allowed to pour water at the same time
HANDLE mutex2 = CreateMutex(NULL, FALSE, NULL);//Two old monks are not allowed to take water at the same time
HANDLE amount = CreateSemaphore(NULL, tong, 3, NULL);//3 barrels quantity
HANDLE emtry = CreateSemaphore(NULL, 10 - gang, 10, NULL);//Number of spare parts in cylinder 10 (initial available resources in the second parameter table and maximum resources in the third parameter table)
HANDLE full = CreateSemaphore(NULL, gang, 10, NULL);//Quantity of water in 10 cylinders


DWORD WINAPI Xiao(LPVOID lpParamter){
	while (true) {
		WaitForSingleObject(emtry, INFINITE);//It is allowed to take the bucket and pour water only when there is free bucket in the tank
		WaitForSingleObject(amount, INFINITE);
		
		Sleep(100);
		printf("%05d Little monk No%d A barrel.%d Buckets of water.\n", GetCurrentThreadId(), --tong, gang);
		
		WaitForSingleObject(mutex1, INFINITE);
		Sleep(100);
		printf("%05d Little monk No. 1 poured in a bucket of water and put it back into a bucket. Now%d A barrel.%d Buckets of water.\n", GetCurrentThreadId(), ++tong, ++gang);
		ReleaseMutex(mutex1);

		ReleaseSemaphore(amount, 1, NULL);
		ReleaseSemaphore(full, 1, NULL);//Release a "quantity of water in the tank"

		
	}
	return 0;
}

DWORD WINAPI Lao(LPVOID lpParamter){
	while (true) {
		WaitForSingleObject(full, INFINITE);//When there is water in the tank and there is an empty bucket, it is allowed to take the bucket for water
		WaitForSingleObject(amount, INFINITE);
		
		Sleep(200);
		printf("%05d Old monk No%d A barrel.%d Buckets of water.\n", GetCurrentThreadId(), --tong, gang);

		WaitForSingleObject(mutex2, INFINITE);//If there's an old monk taking water, he can't get it
		Sleep(200);
		printf("%05d No. 1 old monk took a bucket of water and put it back into a bucket%d A barrel.%d Buckets of water.\n", GetCurrentThreadId(), ++tong, --gang);
		ReleaseMutex(mutex2);

		ReleaseSemaphore(amount, 1, NULL);
		ReleaseSemaphore(emtry, 1, NULL);//Free up a "spare in the tank"
	}
	return 0;
}

int main()
{
	printf("Initial:%d A barrel.%d Buckets of water.\n", tong, gang);
	HANDLE hThread;
	while(true) {//Endless cycle, constantly generate new little monk and old monk
		hThread = CreateThread(NULL, 0, Xiao, NULL, 0, NULL);
		hThread = CreateThread(NULL, 0, Lao, NULL, 0, NULL);
		Sleep(500);
	}
	CloseHandle(hThread);
	return 0;
}

The operation effect is as follows:

Posted by SuprSpy79 on Mon, 30 Mar 2020 21:38:02 -0700