0

Can anyone share some links or its own experience on the interprocess synchronization on windows. I run more instances of my program (each having 1 Thread) and want to synchronize them. I read bunch of functions related to it link, none seems to give an example on the interprocess communication (they all talk about the thread synchronization). So I want the process to wait until all the processes have reached the same line in the code.

Here is what I am having so far:;

interprocess_mtx = CreateMutex(NULL, FALSE, TEXT("mtx_name"));
if (interprocess_mtx == NULL) {
    return (int)GetLastError();
}

if (WaitForSingleObject(interprocess_mtx, 10000) == WAIT_OBJECT_0) {
    // here comes the code that needs to be executed synchronously
    ReleaseMutex(interprocess_mtx);
}

if (CloseHandle(interprocess_mtx) == 0){
    return (int)GetLastError();
}

Thanks

// Edited Solution: I run the first .exe file, wait for some small time, then run the rest. The first process creates a named mutex, locks it, waits for e.g. 5 seconds and releases it. The rest of the exe files (processes) create the same named mutex, then lock the mutex and release immediately. The critical code section is executed after the release.

  • 2
    how about a named semaphore? – kenny Feb 21 '18 at 14:22
  • This is not link-sharing service. Do you have a specific code and a question about it? – Eugene Sh. Feb 21 '18 at 14:22
  • @kenny Sure, but if I use function WaitForSingleObject() the process will continue as soon as only 1 different process has made it till the same line (leaving the rest of them unsync). And I think using WaitForMultipleObjects() doesn't make sense to use as I only have one thread per process- – Larry The Blackbird Feb 21 '18 at 14:32
  • you can build something custom in shared memory, good luck! https://stackoverflow.com/questions/7894224/sharing-memory-between-two-applications – kenny Feb 21 '18 at 14:38
  • 1
    @LarryTheBlackbird 'WaitForMultipleObjects()' makes sense to me. All processes have at least one thread, so waiting on N single-threaded processes is waiting on N threads. – Martin James Feb 21 '18 at 14:58
  • ...or you could wait with WFSO and loop around until you have received N semaphore units. – Martin James Feb 21 '18 at 15:00
  • @MartinJames Sure but as the first input argument of the function I need to give the number of simultaneous threads and as the second the pointer to the handlers of those threads. So I cannot give lets say 5 and 1 thread as input arguments. Ill try with the semaphores – Larry The Blackbird Feb 21 '18 at 15:03
  • question too general. exist huge count of different synchronization technique. which select - depend from concrete task/situation – RbMm Feb 21 '18 at 15:43
  • Okay, I added some code, this should be enough. So basically I want that all my processes (independent on how many I run) wait till all of them have reached the line "if(WaitForSingleObject(...) == WAIT_OBJECT_0) – Larry The Blackbird Feb 21 '18 at 16:15
  • and so what ? code not match to your description. you implement some critical section. *wait till all of them have reached the line* - nothing common with code. and what sense in this impossible task – RbMm Feb 21 '18 at 16:52
  • Do you want all the processes to be identical or is there one special process that starts the others and waits? Also, did you mean "communication" in the question or "synchronization"? – Adrian McCarthy Feb 21 '18 at 19:17
  • @AdrianMcCarthy They are all the same. Just .exe files run from a batch script. I meant synchronization, meaning they all execute the critical section at the same time – Larry The Blackbird Feb 22 '18 at 08:00
  • Even though it's not the best way, you can even use a lock file, a process first check if the .lock file is exists, wait it to be deleted or raise timeout error, if the .lock file doesn't exists it creates, so the other processes should wait. A better way is create and keep open/lock that file, other processes should do also the same (and fail because it is lock). This way, if somehow the .lock file is not deleted (the process created the lock file crashed for example), second process will able to re-create it. – endo64 Feb 03 '21 at 19:12

1 Answers1

0
  1. The starter process creates a Windows event object for each of the others processes, then it creates the other (child) processes, passing each of them the handles of all of the event objects (e.g., on the command line).
  2. Each child process raises its own event.
  3. Each process waits (with WaitForMultipleObject) for all of the events to be raised.

The starter process code looks something like this:

HANDLE handles[child_count];
for (int i = 0; i < child_count; ++i) {
  handles[i] = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
}
for (int i = 0; i < child_count ++i) {
  LaunchChild(i, handles);
}
DWORD result = ::WaitForMultipleObjects(child_count, handles, /*all=*/TRUE, INFINITE);
if (WAIT_OBJECT_0 <= result && result < WAIT_OBJECT_0 + child_count) {
  // All of the children have set their events!
}

Where LaunchChild starts the child process, passing the child's number and the handles of all of the events on the command line. Each child parses the command line info into a child_index and an array of HANDLEs.

void SyncWithSiblings(int child_index, HANDLE *handles) {
  // Raise my own event:
  ::SetEvent(handles[child_index]);
  DWORD result = ::WaitForMultipleObjects(child_count, handles, /*all=*/TRUE, INFINITE);
  if (WAIT_OBJECT_0 <= result && result < WAIT_OBJECT_0 + child_count) {
    // All of the siblings have set their events!
  }
}
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Thanks for the replay. As I answered a few seconds ago, there is no starter process. I run the same code (the same exe file) from the batch file multiple times. The first one which reaches the critical sections code should wait for the rest of the processes. Basically they should wait eachother until all of the processes have reached the same line in the code – Larry The Blackbird Feb 22 '18 at 08:03