Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

operating system - Single instance of application

In general, what are the ways that we can make sure a single instance of application? This is not specific to any programming language. I just want to know how we can prevent starting the second instance of an application?

question from:https://stackoverflow.com/questions/65914077/single-instance-of-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can think of two ways:

  1. Application side: Create a lockfile somewhere on a specified location. If it already exists, another instance is running, otherwise create the lockfile. If the program exits, delete the lockfile. (For example X or firefox do this)

  2. OS side: (Although this wouldn't be that practical)

2.1. For all started binaries, store some hash value somewhere. If an binary is already started, (E.g. the hash value is already available), don't start it.

2.2. This will fail quite often, as you could run only ONE JVM, ONE Python interpreter and so on. So to improve this, the OS could add the commandline arguments for example to the hash value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...