Samephore1:
#include <stdio.h>#include<stdlib.h>#include<Windows.h>Charname[ -] ="Haihualovefang";voidMain () {HANDLE Hsem= Createsemaphorea (NULL,0,1, name); printf ("created successfully"); CharCH =Getch (); //ReleaseMutex (mutex);//ReleaseSemaphore (Hsem,1, NULL); printf ("Trigger Signal Volume"); CloseHandle (Hsem);}
Samephore2:
#include <stdio.h>#include<stdlib.h>#include<Windows.h>Charname[ -] ="Haihualovefang";voidMain () {HANDLE Hsem=Opensemaphorea (semaphore_all_access, TRUE, name);; if(Hsem = =NULL) {printf ("Open Failed"); System ("Pause"); return; } printf ("Waiting for-------"); DWORD Res= WaitForSingleObject (Hsem,20000); Switch(res) { Casewait_object_0:printf ("Signal received-------"); Break; Casewait_timeout:printf ("Timeout not received-------"); Break; Casewait_abandoned:printf ("another process terminated unexpectedly-------"); Break; default: Break; } closehandle (Hsem); System ("Pause");}
Samephore () Semaphore communication across threads