#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
注重版權,轉載請标明出處