The previous article described the use of mutexes and semaphore to implement thread mutexes in multithreaded scenarios. In fact, because the mutex, semaphore is a kernel object, although it is created in a process, but because the kernel module can be shared among the processes, it is also possible to use a mutex to semaphore the amount of mutex between processes.
0. Using mutexes to implement mutual exclusion between multiple processes
Race_process_mutex_1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
char name[50] = "Multiple_ Process_shared_mutex_test ";
void Main ()
{
HANDLE G_mutex = Createmutexa (NULL, TRUE, name);//How the handle is created successfully returns a valid handle value
if (G_mutex)
printf ("Handle creation succeeded \ n");
else
{
printf ("Handle creation failed \ n");
return;
}
char ch = getchar ();
ReleaseMutex (G_mutex);//Leave the Mutex
("The host process has freed the mutex, the remaining process can trigger the mutex \ n");
CloseHandle (G_mutex);
}
Race_process_mutex_2.cpp
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[50] =
"Multiple_process_shared_mutex_test";
void Main () {HANDLE mutex = Openmutexa (mutex_all_access,true,name);//mutex_all_access Find all if (mutex==null) {
printf ("The mutex kernel object with the given parameter name failed to open and may not have created the mutex\n");
System ("pause");
Return
The printf ("----has searched for the specified mutex kernel object, is waiting for the object to be used-----\ n");
DWORD res = WaitForSingleObject (mutex, 20000);
Switch (res) {case wait_object_0:printf ("----received a signal that successfully obtained the mutex, the following process can go into the critical section to operate-----\ n");
Break
Case wait_timeout:printf ("----timeout, the mutex host process is still declaring the use of the mutex-----\ n");
Break
Case wait_abandoned:printf ("----The mutex host process terminated unexpectedly-------\ n");
Break
Default:break;
} closehandle (mutex);
System ("pause"); }
1. Using semaphore to implement mutual exclusion between multiple processes
Race_process_semaphore_1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
char name[50] = "Multiple_ Process_shared_mutex_test ";
void Main ()
{
HANDLE Hsem = Createsemaphorea (NULL, 0, 1, name);
The second parameter is 0, which declares the occupancy of the semaphore when the process creates the semaphore
("The host process creates semaphore success");
char ch = getchar ();
ReleaseSemaphore (Hsem, 1, NULL);
printf ("The host process has freed up the use of semaphores, the rest of the process can trigger semaphores");
CloseHandle (Hsem);
}
Race_process_semaphore_2.cpp
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[50] = "Multiple_process_shared_
Mutex_test ";
void Main () {HANDLE Hsem = Opensemaphorea (semaphore_all_access, TRUE, name);
if (Hsem = = NULL) {printf ("Semaphore kernel object with the given parameter name failed to open, may not have created the semaphore\n");
System ("pause");
Return
The printf ("----has searched for the specified semaphore kernel object, is waiting for the object to be used-----\ n"); DWORD res = WaitForSingleObject (Hsem, 20000); The second parameter 20000, unit MS, represents the maximum listener for the semaphore 20s switch (RES) {case wait_object_0:printf ("----received a signal, successfully obtained to the semaphore, below
This process can be entered into the critical section for operation-----\ n ");
Break
Case wait_timeout:printf ("----timeout, the semaphore host process is still declaring the use of the semaphore-----\ n");
Break /*semaphore is similar to event, and cannot detect the active interruption of the host process, so it can only wait for the time to be consumed before responding, so this is the disadvantage of semaphore compared to the mutex.
That is, the host process of the semaphore actively terminates, the listener process can only passively consume all the waiting time, but not to listen to the other side of the process is terminated.
Case wait_abandoned:printf ("----semaphore the host process terminated unexpectedly-------");
Break */Default:break;
} closehandle (Hsem);
System ("pause"); }
2. Using event to implement multi-process collaborative execution
Race_process_event_1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
char name[50] = "Multiple_ Process_shared_mutex_test ";
void Main ()
{
HANDLE event = Createeventa (NULL, False, False, name);
printf ("Host process creation semaphore succeeded");
char ch = getchar ();
SetEvent (event);
printf ("The host process activated the event, waiting for other processes to trigger the event");
CloseHandle (event);
System ("pause");
}
Race_process_event_2.cpp
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[50] =
"Multiple_process_shared_mutex_test"; void Main () {HANDLE event = Openeventa (event_all_access, TRUE, name);//Open Event if (event = = NULL) {pri
NTF ("The event kernel object with the given parameter name failed to open and may not have created the event\n");
System ("pause");
Return
The printf ("----has searched for the specified event kernel object, is waiting for the object to be used-----\ n");
DWORD res = WaitForSingleObject (event, 20000);
Switch (res) {case wait_object_0:printf ("----received a signal to successfully activate the target event, the following process can be followed by the cooperative operation-----\ n");
Break
Case wait_timeout:printf ("----timeout, the host process of the target event still does not activate the event that the other process uses----\ n");
Break /*//event mechanism is actually not aware of the loss of host process interruption, which is the biggest disadvantage of event compared to the mutex case wait_abandoned:printf ("-------The target event of the host process terminated unexpectedly-
------");
Break
*/Default:break;
} CloseHandle (event);
System ("pause"); }
According to the above can be seen basically kernel objects can be modified to use in the multi-process for information exchange, which is a multi-process signaling (on or off) interactive way, while the more large-scale data interaction between processes involves other mechanisms, such as inter-process message (Windows Socket sockets belong to this category), shared memory blocks, pipe pipeline and other technologies.
summarize the content of this article , a simple sentence, if the only switch lock, in two processes to achieve mutual exclusion, mutexes than event and Semaphre more secure, more responsive.