简体   繁体   English

等待程序的最后一个实例

[英]wait for last instance of program

I have two programs, X is the normal program with which the user interacts, and program Y which cleans up the resources acquired by program Y. There can be multiple instances of X but only one of Y (I already solved that part with named mutexes).我有两个程序,X 是用户与之交互的普通程序,程序 Y 清理程序 Y 获取的资源。可以有多个 X 实例,但只有一个 Y(我已经用命名互斥锁解决了该部分)。 Now, since Y is a cleanup program, it should be blocked until the last instance of X disappear.现在,由于 Y 是一个清理程序,它应该被阻止直到 X 的最后一个实例消失。

I tried using a semaphore but I couldn't figure it out.我尝试使用信号量,但我无法弄清楚。 Can somebody help me?有人可以帮助我吗?

A semaphore is one valid way of doing this, but not necessarily the best.信号量是一种有效的方法,但不一定是最好的。 Whenever program X starts, call ReleaseSemaphore .每当程序 X 启动时,调用ReleaseSemaphore Whenever a process terminates, call WaitForSingleObject with a timeout of zero on the semaphore handle (be sure to also include this in the exception handler, in case the program crashes).每当一个进程终止时,调用WaitForSingleObject信号量句柄上的超时为零(确保在异常处理程序中也包含它,以防程序崩溃)。
Process Y can regularly poll WaitForSingleObject with a zero (or a few milliseconds) timeout then.然后,进程 Y 可以定期轮询WaitForSingleObject ,超时时间为零(或几毫秒)。 If the return value is WAIT_OBJECT_0 , it must release the semaphore again immediately (otherwise it will block the last X process trying to exit.).如果返回值为WAIT_OBJECT_0 ,它必须立即再次释放信号量(否则它将阻塞最后一个试图退出的 X 进程。)。 If the return value is WAIT_TIMEOUT , there are not X processes any more.如果返回值为WAIT_TIMEOUT ,则不再有 X 进程。

The best solution would of course be to launch all X processes from Y. In that case, Y could just WaitForMultipleObjects on the process handles that it gets from CreateProcess with no extra "ifs and whens".最好的解决方案当然是从 Y 启动所有 X 进程。在这种情况下,Y 可以只在它从CreateProcess获得的进程句柄上WaitForMultipleObjects ,而无需额外的“ifs and whens”。 This will "just work", always.这将永远“正常工作”。 It is more efficient, too.它也更有效率。
Which leads to the second best solution... getting handles to the running processes with OpenProcess and WaitForMultipleObjects on those.这导致了第二个最佳解决方案......使用OpenProcessWaitForMultipleObjects获取正在运行的进程的句柄。 The problem is where to get the process IDs from.问题是从哪里获取进程 ID。 A shared memory area might do, a pipe might do, or CreateToolhelp32Snapshot might give you that info.共享的 memory 区域可能会这样做,pipe 可能会这样做,或者CreateToolhelp32Snapshot可能会为您提供该信息。

Another way would be to use a named mutex object.另一种方法是使用命名互斥锁 object。 All processes X call CreateMutex .所有进程 X 调用CreateMutex If the mutex already exists, no harm is done (GetLastError will return ERROR_ALREADY_EXISTS , but so what).如果互斥体已经存在,则不会造成任何伤害(GetLastError 将返回ERROR_ALREADY_EXISTS ,但那又怎样)。 If the process terminates or crashes, all open handles are closed, and thus the mutex reference count is decremented.如果进程终止或崩溃,所有打开的句柄都将关闭,因此互斥体引用计数会减少。
The Y process calls OpenMutex . Y 进程调用OpenMutex This either succeeds or fails.这要么成功,要么失败。 If it succeeds, it closes the handle again, sleeps, and tries again.如果成功,它会再次关闭句柄,休眠并再次尝试。 If it fails, no single X process is running.如果失败,则没有单个 X 进程正在运行。

Yet another way (though it might possibly have race issues) would be creating a named shared memory segment and calling InterlockedIncrement and InterlockedDecrement at process X start and exit.另一种方法(尽管可能存在竞争问题)是创建一个命名的共享 memory 段,并在进程 X 启动和退出时调用InterlockedIncrementInterlockedDecrement Process Y knows that no X processes are running if either the shared memory object cannot be opened or the counter is zero.如果无法打开共享 memory object 或计数器为零,则进程 Y 知道没有 X 进程正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM