简体   繁体   English

如何限制C程序的实例数?

[英]How can I restrict the number of instances of my C program?

Title says it all, I need to only allow one instance of my C program to be run. 标题说明了一切,我只需要允许运行我的C程序的一个实例。 How can I accomplish this? 我该怎么做?

You can use shared memory for this purpose. 您可以为此目的使用共享内存。 Shared memory is an OS-level mechanism. 共享内存是操作系统级别的机制。

  1. Start instance #1 of your app 启动应用程序的实例1
    • The app checks the shared memory if there is a value stored 该应用程序检查共享内存中是否存储了值
    • If not, the app stores some value into the shared memory. 如果不是,则该应用将一些值存储到共享内存中。
  2. Start instance #2 of your app 启动应用程序的实例2
    • The app checks the shared memory if there is a value stored 该应用程序检查共享内存中是否存储了值
    • The app sees there's already a shared memory value with a value and kills itself 该应用程序发现已经存在一个带有值的共享内存值,并杀死了自己

You can use the shared memory to store the specific number of instances your app is allowed to run. 您可以使用共享内存来存储允许您的应用程序运行的特定实例数。

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

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