There is a server:

STARTUPINFO si; PROCESS_INFORMATION pi; DWORD dwExitCode; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); if (!CreateProcess("C:\\Users\\Артем\\Documents\\visual studio 2015\\Projects\\Writer\\Debug\\Writer.exe", NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi)) { cout << GetLastError(); _getch(); return -1; } Sleep(10); cout << "Process 1: "; while (GetExitCodeProcess(pi.hProcess, &dwExitCode) != 0 && dwExitCode == STILL_ACTIVE) { WaitForSingleObject(hMutex, INFINITE); cout << *((LPSTR)lpFileMap); ReleaseMutex(hMutex); Sleep(100); } 

Which creates the client process and gets from FileMapping the character that was entered in the Client.

Customer:

 WaitForSingleObject(hMutex, INFINITE); while (true) { chr = _getch(); if (chr == '1') { *((LPSTR)lpFileMap) = '+'; ReleaseMutex(hMutex); } if (chr == '2') { *((LPSTR)lpFileMap) = '-'; ReleaseMutex(hMutex); } if (chr == '3') { *((LPSTR)lpFileMap) = ' '; ReleaseMutex(hMutex); WaitForSingleObject(hMutex, INFINITE); while ((chr = _getch()) != '\r') { *((LPSTR)lpFileMap) = chr; ReleaseMutex(hMutex); WaitForSingleObject(hMutex, INFINITE); *((LPSTR)lpFileMap) = '\0'; } break; } Sleep(10); *((LPSTR)lpFileMap) = '\0'; if(chr > '0' && chr < '4') WaitForSingleObject(hMutex, INFINITE); } 

When entering 1, the + symbol is transmitted, when 2 is entered, the - symbol is displayed, and when 3 is entered, a message informs about the completion of the process.

Question: How can I start several identical client processes and synchronize them? For example, the user enters from the console the number of client processes, the server creates them, and in turn from each process receives a message. Those. The first process sent +, the server accepted and passed to the second, the second sent -, the server accepted and passed to the first and so on.

    1 answer 1

    It is not easy and has nothing to do with the classic mutex, but Microsoft came up with special ones. named objects. https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms684123(v=vs.85).aspx

    One process creates and owns it, while others can access it by name.

    This program creates a mutex. This turns out to be a success even if such an object already exists.

     #include <windows.h> #include <stdio.h> #include <conio.h> // This process creates the mutex object. int main(void) { HANDLE hMutex; hMutex = CreateMutex( NULL, // default security descriptor FALSE, // mutex not owned TEXT("NameOfMutexObject")); // object name if (hMutex == NULL) printf("CreateMutex error: %d\n", GetLastError() ); else if ( GetLastError() == ERROR_ALREADY_EXISTS ) printf("CreateMutex opened an existing mutex\n"); else printf("CreateMutex created a new mutex.\n"); // Keep this process around until the second process is run _getch(); CloseHandle(hMutex); return 0; } 

    This program opens an existing mutex using the OpenMutex function, which returns a failure state if the object is not found. Full access is required for wait operations.

     #include <windows.h> #include <stdio.h> // This process opens a handle to a mutex created by another process. int main(void) { HANDLE hMutex; hMutex = OpenMutex( MUTEX_ALL_ACCESS, // request full access FALSE, // handle not inheritable TEXT("NameOfMutexObject")); // object name if (hMutex == NULL) printf("OpenMutex error: %d\n", GetLastError() ); else printf("OpenMutex successfully opened the mutex.\n"); CloseHandle(hMutex); return 0; }