How to block running two instances of the same program?

后端 未结 8 1255
暗喜
暗喜 2020-12-01 05:23

I need to make sure that user can run only one instance of my program at a time.
Which means, that I have to check programatically, whether the same program is already r

相关标签:
8条回答
  • 2020-12-01 06:19

    It is very un-unix to prohibit multiple instances of a program to run.

    If the program is, say, a network daemon, it doesn't need to actively prohibit multiple instances--only the first instance gets to listen to the socket, so subsequent instances bomb out automatically. If it is, say, an RDBMS, it doesn't need to actively prohibit multiple instances--only the first instance gets to open and lock the files. etc.

    0 讨论(0)
  • 2020-12-01 06:20

    I actually use exactly the process you describe, and it works fine except for the edge case that happens when you suddenly run out of disk space and can no longer create files.

    The "correct" way to do this is probably to use shared memory: http://www.cs.cf.ac.uk/Dave/C/node27.html

    0 讨论(0)
提交回复
热议问题