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?
Asked
Active
Viewed 171 times
0
-
You check to see if the process is running or not and if so, don't start it again. Such as TaskManager shows in Windows or Top in Linux. Your programming language of choice *may/should* have a way to get a list of running processes. – beeker Jan 27 '21 at 06:46
-
Mutex might be preferable if you want to ensure against user deliberately copying and modifying the executable name – Martheen Jan 27 '21 at 06:48
-
Just linking some relevant posts with methods for some languages: [C#](https://stackoverflow.com/questions/13562464/how-to-prevent-multiple-instances-of-a-program-running-at-the-same-time), [C++](https://stackoverflow.com/questions/8799646/preventing-multiple-instances-of-my-application), [Python](https://stackoverflow.com/questions/1155315/prevent-opening-a-second-instance), [VB.NET](https://stackoverflow.com/questions/25949179/how-to-prevent-multiple-instances-of-my-application), [Java](https://stackoverflow.com/questions/7036108/prevent-launching-multiple-instances-of-a-java-application) – costaparas Jan 27 '21 at 06:50
-
@costaparas I am not asking from the perspective of programming language. But I want to know a list of possible ways we can prevent the second instance from starting? We can assume this as an operating system interview question? – Nagasitaram Thigulla Jan 27 '21 at 07:52
1 Answers
0
I can think of two ways:
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)
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.

JCWasmx86
- 3,473
- 2
- 11
- 29