samephore1:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

char name[100] = "haihualovefang";

void main()
{
    HANDLE hsem = CreateSemaphoreA(NULL, 0, 1, name);
    printf("创建成功");
    char ch = getch();

    //ReleaseMutex(mutex);//

    ReleaseSemaphore(hsem, 1, NULL);
    printf("触发信号量");
    CloseHandle(hsem);


}

 

samephore2:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

char name[100] = "haihualovefang";

void main()
{

    HANDLE hsem = OpenSemaphoreA(SEMAPHORE_ALL_ACCESS, TRUE, name);;
    if (hsem == NULL)
    {
        printf("打开失败");
        system("pause");
        return;
    }
    printf("等待-------");
    DWORD res = WaitForSingleObject(hsem, 20000);
    switch (res)
    {
    case WAIT_OBJECT_0:
        printf("收到信号-------");
        break;
    case WAIT_TIMEOUT:
        printf("超时没有收到-------");
        break;
    case WAIT_ABANDONED:
        printf("另外一个进程意外终止-------");
        break;
    default:
        break;

    }
    CloseHandle(hsem);



    system("pause");
}

 

相关文章:

  • 2022-01-10
  • 2021-08-30
  • 2021-07-01
  • 2022-01-17
  • 2021-11-13
  • 2022-02-12
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2021-10-11
  • 2021-06-15
  • 2021-12-28
  • 2021-06-22
  • 2021-07-30
  • 2022-12-23
相关资源
相似解决方案