简体   繁体   中英

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. 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
    • 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
    • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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