玉生香 2022-02-23 23:44 采纳率: 0%
浏览 44

CreateSemaphore 与 WaitForSingleObject 当一个进程被kill或者异常终止,怎么处理

CreateSemaphore 与 WaitForSingleObject 当一个进程被kill或者异常终止,怎么处理?

如果一个进程异常终止或者被kill ,CreateSem的计数器一直不修改。导致另一个进程一直处于超时等待状态。详细请见代码示例。

 

代码1




#include <iostream>
#include <Windows.h>

char semName1[] = "semWrite";//信号量名字


int main(int argc, char** argv)
{

	while (1)
	{
		HANDLE pSemWrite = CreateSemaphore(NULL, 1, 1, semName1);
		DWORD dwRet = WaitForSingleObject(pSemWrite, 10000);
		switch (dwRet)
		{

		case WAIT_ABANDONED:

			printf("WAIT_ABANDONED\n");

			break;

		case WAIT_OBJECT_0:
			ReleaseSemaphore(pSemWrite, 1, NULL);
			printf("WAIT_OBJECT_0\n");

			break;

		case WAIT_TIMEOUT:
			printf("WAIT_TIMEOUT\n");
			break;

		case WAIT_FAILED:
			printf("WAIT_FAILED\n");
			break;


		}
	}

	
	std::cout << "test" << std::endl;

}

代码2

#include <iostream>
#include <Windows.h>

char semName1[] = "semWrite";//信号量名字


int main(int argc, char** argv)
{

	HANDLE pSemWrite = CreateSemaphore(NULL, 1, 1, semName1);
	DWORD dwRet = WaitForSingleObject(pSemWrite, 20000);

	char* p = NULL;
	memcpy(p, "abc", 4);

	switch (dwRet)

	{

	case WAIT_ABANDONED:

		printf("WAIT_ABANDONED\n");

		break;

	case WAIT_OBJECT_0:
		ReleaseSemaphore(pSemWrite, 1, NULL);
		printf("WAIT_OBJECT_0\n");

		break;

	case WAIT_TIMEOUT:
		printf("WAIT_TIMEOUT\n");
		break;

	case WAIT_FAILED:
		printf("WAIT_FAILED\n");
		break;


	}



	std::cout << "test" << std::endl;

}
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-02-24 14:27
    关注

    CreateSemaphore
    The CreateSemaphore function creates a named or unnamed semaphore object.

    HANDLE CreateSemaphore(
    LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
    // pointer to security attributes
    LONG lInitialCount, // initial count
    LONG lMaximumCount, // maximum count
    LPCTSTR lpName // pointer to semaphore-object name
    );

    Parameters
    lpSemaphoreAttributes
    Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSemaphoreAttributes is NULL, the handle cannot be inherited.
    Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new semaphore. If lpSemaphoreAttributes is NULL, the semaphore gets a default security descriptor.

    lInitialCount
    Specifies an initial count for the semaphore object. This value must be greater than or equal to zero and less than or equal to lMaximumCount. The state of a semaphore is signaled when its count is greater than zero and nonsignaled when it is zero. The count is decreased by one whenever a wait function releases a thread that was waiting for the semaphore. The count is increased by a specified amount by calling the ReleaseSemaphore function.
    lMaximumCount
    Specifies the maximum count for the semaphore object. This value must be greater than zero.
    lpName
    Pointer to a null-terminated string specifying the name of the semaphore object. The name is limited to MAX_PATH characters, and can contain any character except the backslash path-separator character (). Name comparison is case sensitive.
    If lpName matches the name of an existing named semaphore object, this function requests SEMAPHORE_ALL_ACCESS access to the existing object. In this case, the lInitialCount and lMaximumCount parameters are ignored because they have already been set by the creating process. If the lpSemaphoreAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.

    If lpName is NULL, the semaphore object is created without a name.

    If lpName matches the name of an existing event, mutex, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

    Return Values
    If the function succeeds, the return value is a handle to the semaphore object. If the named semaphore object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

    Remarks
    The handle returned by CreateSemaphore has SEMAPHORE_ALL_ACCESS access to the new semaphore object and can be used in any function that requires a handle to a semaphore object.

    Any thread of the calling process can specify the semaphore-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.

    The state of a semaphore object is signaled when its count is greater than zero, and nonsignaled when its count is equal to zero. The lInitialCount parameter specifies the initial count. Each time a waiting thread is released because of the semaphore's signaled state, the count of the semaphore is decreased by one. Use the ReleaseSemaphore function to increment a semaphore's count by a specified amount. The count can never be less than zero or greater than the value specified in the lMaximumCount parameter.

    Multiple processes can have handles of the same semaphore object, enabling use of the object for interprocess synchronization. The following object-sharing mechanisms are available:

    A child process created by the CreateProcess function can inherit a handle to a semaphore object if the lpSemaphoreAttributes parameter of CreateSemaphore enabled inheritance.
    A process can specify the semaphore-object handle in a call to the DuplicateHandle function to create a duplicate handle that can be used by another process.
    A process can specify the name of a semaphore object in a call to the OpenSemaphore or CreateSemaphore function.
    Use the CloseHandle function to close the handle. **The system closes the handle automatically when the process terminates. **The semaphore object is destroyed when its last handle has been closed.

    QuickInfo
    Windows NT: Requires version 3.1 or later.
    Windows: Requires Windows 95 or later.
    Windows CE: Unsupported.
    Header: Declared in winbase.h.
    Import Library: Use kernel32.lib.
    Unicode: Implemented as Unicode and ANSI versions on Windows NT.

    See Also
    Synchronization Overview, Synchronization Functions, CloseHandle, CreateProcess, DuplicateHandle, OpenSemaphore, ReleaseSemaphore, SECURITY_ATTRIBUTES

    评论

报告相同问题?

问题事件

  • 创建了问题 2月23日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘