进程启动限制
我创建了一个使用 C++ 的简单应用程序并生成一个可执行文件。
我想确保该过程不能启动两次。如何强制进程/服务仅启动一次?
谢谢。
I have create a simple application which using C++ and produce a executable file.
I would like to ensure the process cannot be start twice. How to enforce a process/service is start once only ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么操作系统?在 Windows 上,执行此操作的典型方法是创建一个命名的互斥体,因为如果其他进程已经创建了具有该名称的互斥体,操作系统会给您一个
ERROR_ALREADY_EXISTS
,并且操作系统将确保当进程结束时(即使它崩溃或被杀死),互斥量就会被释放。What operating system? On Windows, the typical way to do this is to create a named Mutex, because the OS will give you an
ERROR_ALREADY_EXISTS
if some other process already created a mutex with that name, and the OS will ensure that the mutex is released when the process ends (even if it crashes or is killed).写入一个临时文件并将其用作锁。
编辑:回答评论:如果您在 Unix 系统上,请写入文件 /tmp/my_application_lock_file。如果它已经存在,请停止您的程序并显示适当的消息。文件创建者退出后,将其删除。
Write a temporary file and use it as a lock.
Edit: To answer the comment: If you are on a Unix system, write a file /tmp/my_application_lock_file. If it already exists, stop your program with an appropriate message. On exit of the creator of the file, delete it.