Windows API Watch Dog----看门狗(相互监视的两个进程(真源代码))


#define BUF_SIZE 1025
wchar_t share_name_one[] = L"TheMappingOfAdventure";    // 共享内存的名字
bool exit_current_process = false;

void sig_abort(int sgl) {
	switch (sgl) {
	case SIGINT: {
		break;
	}
	case SIGILL: {
		break;
	}
	case SIGFPE: {
		break;
	}
	case SIGTERM: {
		break;
	}
	case SIGBREAK: {
		break;
	}
	case SIGABRT: {
		exit_current_process = true;
		break;
	}
	}
}

int main()
{
	HANDLE h_mutex_another = CreateMutex(NULL, TRUE, L"another");
	auto h_exist = GetLastError();
	if (h_mutex_another) {
		if (ERROR_ALREADY_EXISTS == h_exist) {
			std::cout << "this instance has existed.\n";
			CloseHandle(h_mutex_another);
			return 0;
		}
	}
	else {
		std::cout << "Create mutex error, exists.\n";
		CloseHandle(h_mutex_another);
		return 0;
	}

	signal(SIGINT, sig_abort);
	/*signal(SIGILL, sig_abort);
	signal(SIGFPE, sig_abort);
	signal(SIGTERM, sig_abort);
	signal(SIGBREAK, sig_abort);
	signal(SIGABRT, sig_abort);*/
	auto current_pid = GetCurrentProcessId();
	wchar_t that_path[] = L"D:\\Project\\C++Proj\\adventure\\Debug\\adventure.exe";
	char* buff_one = (char*)malloc(BUF_SIZE * sizeof(char));
	std::mutex mt;

	// 打开共享文件句柄
	HANDLE handle_one = CreateFileMapping(
		INVALID_HANDLE_VALUE,    // 物理文件句柄
		NULL,                    // 默认安全级别
		PAGE_READWRITE,          // 可读可写
		0,                       // 高位文件大小
		BUF_SIZE,                // 地位文件大小
		share_name_one           // 共享内存名称
	);
	buff_one = (char*)MapViewOfFile(
		handle_one,            // 共享内存的句柄
		FILE_MAP_ALL_ACCESS,   // 可读写许可
		0,
		0,
		BUF_SIZE
	);
	auto err = GetLastError();

	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi = PROCESS_INFORMATION();
	int ct_times = 0; 
	std::string tmp;
	std::string the_other_pid;
	bool is_success = false;
	while (!exit_current_process)
	{
		auto pid = std::to_string(current_pid);
		std::cout << "current id:" << current_pid << std::endl;
		//getchar();
		if (strlen(buff_one) != 0)
			std::cout << "contents:" << buff_one << std::endl;
		tmp = buff_one;
		std::lock_guard<mutex> lg(mt);
		strncpy(buff_one, pid.c_str(), BUF_SIZE - 1);
		std::cout << "tmp:" << tmp << std::endl;
		buff_one[BUF_SIZE - 1] = '\0';
		if (pid != tmp && tmp != "") {
			the_other_pid = tmp;
			ct_times = 0;
			std::cout << "the other is still alive\n";
			std::this_thread::sleep_for(std::chrono::seconds(3));
		}
		else
		{
			if (ct_times >= 3) {
				int other_pid = std::atoi(the_other_pid.c_str());
				HANDLE the_other = OpenProcess(
					PROCESS_ALL_ACCESS,
					TRUE,
					other_pid
				);
				TerminateProcess(the_other, 0);
				CloseHandle(the_other);
				ct_times = 0;
				pi.dwProcessId = 0;
			}
			if (pi.dwProcessId != 0) {
				++ct_times;
				std::cout << "attempt reconnect:" << ct_times << std::endl;
				std::this_thread::sleep_for(std::chrono::seconds(3));
				continue;
			}
			std::cout << "the other is down\n";
			is_success = CreateProcess(
				that_path,
				NULL,
				NULL, NULL,
				TRUE,
				CREATE_NEW_CONSOLE,
				NULL, NULL,
				&si, &pi);
			if (is_success) {
				std::cout << pi.dwProcessId << "launch success\n";
			}
			else
				std::cout << "fault\n";
			std::this_thread::sleep_for(std::chrono::seconds(3));
		}
	}
	
	UnmapViewOfFile(buff_one);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	CloseHandle(handle_one);
	CloseHandle(h_mutex_another);
	return 0;
}
附录:

源代码解析:https://blog.csdn.net/weixin_38374997/article/details/103124829


注重版权,转载请标明出处

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值