Results 1 to 20 of 30

Thread: Limiting the number of instances of one application

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    A quick and dirty cross-platform solution would be to create a 'lock' file at startup and delete the file at shutdown. Any new instances check if the file exists first before continuing.

    This isn't foolproof, since if your app crashes, you will have to delete the file manually.
    Save yourself some pain. Learn C++ before learning Qt.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by Chicken Blood Machine View Post
    This isn't foolproof, since if your app crashes, you will have to delete the file manually.
    You can overcome that problem for example by checking if a process which created the file exists (the file can contain the process id of the creator). This still isn't 100% sure, but improves the situation a little.

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    I know I can use process ID under Unix platform but what about others, especially Windows? Actually, even if it's not the most straightforward I think the sockect stuff is the best... I'll try to understand designerassistant sources and, well, we'll see...
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by fullmetalcoder View Post
    I know I can use process ID under Unix platform but what about others, especially Windows?
    I'm sure there is something like a process id on Windows as well.

    Actually, even if it's not the most straightforward I think the sockect stuff is the best... I'll try to understand designerassistant sources and, well, we'll see...
    What if some other process is already using those sockets? It might be impossible to start even a single instance of the application. Probably the proper way to go would be to use semaphores and/or shared memory. It's just a few lines of platform dependent code.

  5. #5
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by fullmetalcoder View Post
    I know I can use process ID under Unix platform but what about others, especially Windows? Actually, even if it's not the most straightforward I think the sockect stuff is the best... I'll try to understand designerassistant sources and, well, we'll see...
    http://msdn.microsoft.com/library/de...tprocessid.asp
    Save yourself some pain. Learn C++ before learning Qt.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Limiting the number of instances of one application

    Someone recently came up with this kind of solution on X11.
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    Of course the drawback is that once the application holding the semaphore crashes, you have no way of starting it again but reboot

  8. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by Chicken Blood Machine View Post
    It's good to obtain a process ID (even though I'm pretty sure it can be done in plain Qt4) but what is important is to check that a proccess with this ID is running, as suggested...

    Someone recently came up with this kind of solution on X11.
    It uses something called "semaphore". Honestly I have no idea of what it is but I know that ther is a class called QSemaphore in Qt4, maybe it would fit...
    Current Qt projects : QCodeEdit, RotiDeCode

  9. The following user says thank you to fullmetalcoder for this useful post:

    arnaiz (20th October 2006)

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by fullmetalcoder View Post
    It uses something called "semaphore". Honestly I have no idea of what it is
    http://en.wikipedia.org/wiki/Semaphore_(programming)


    but I know that ther is a class called QSemaphore in Qt4, maybe it would fit...
    It wouldn't. QSemaphore works in scope of the application, whereas the above mentioned semaphores are system-wide and are therefore suited for IPC (Inter Process Communication) which is the basic idea of using them in this situation.

    Trust me, you won't manage to do it in a system independent way - under the hood there is always platform specific code - even in Qt itself.

  11. #10
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by wysota View Post
    Trust me, you won't manage to do it in a system independent way - under the hood there is always platform specific code - even in Qt itself.
    Actually QTcpSocket works quite well. I've tried a few things and right now by mixing sockets, kinda lock file and a QSemaphore I'm able to prevent the creation of more than a given number of application instance. The only problem left is that communication with the working instances is not really straightforward and, ATM, buggy.
    Last edited by fullmetalcoder; 19th October 2006 at 07:55. Reason: spelling error
    Current Qt projects : QCodeEdit, RotiDeCode

  12. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Did you also look at the QUdpSocket? (Qt Broadcast example)
    Maybe it would also work this way:
    1) Application is started.
    2) Application opens a Udp port that is known to all applications.
    3) The new app sends out a datagramm
    4) Now the app does listen for all responses from other running applications
    5) The new app does verify the number of responses and determines the total number of running instances
    6.1) If there are too many applications running, the new app terminates itself.
    6.2) If it is possible to start another instance, then the new app will close the Udp server socket and open a Udp client socket, listening for broadcast messages itself.

    This should really work on any platform.

  13. The following user says thank you to Mike for this useful post:

    fullmetalcoder (20th October 2006)

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    It may work but is still an ugly solution. If you only want to make sure you have a single instance of the application, you can avoid sockets. If you want your applications on a local machine to communicate, you could come up with a better solution too. Depending on high-numbered sockets is a risk, because there is no way telling if no other app (for example some ftp client or server) doesn't use the same socket.

    Why do you need a QSemaphore here? Is your app accessing the socket from multiple threads?

  15. #13
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by wysota View Post
    It may work but is still an ugly solution. If you only want to make sure you have a single instance of the application, you can avoid sockets. If you want your applications on a local machine to communicate, you could come up with a better solution too. Depending on high-numbered sockets is a risk, because there is no way telling if no other app (for example some ftp client or server) doesn't use the same socket.
    Maybe ugly but better than nothing. Besides socket corruption is highly improbable since localhost is used and the port is dynamically assigned by Qt through the server classes (QTcpServer or QUdpServer)

    Quote Originally Posted by wysota View Post
    Why do you need a QSemaphore here? Is your app accessing the socket from multiple threads?
    The semaphore was just used as a convinient counter providing possibilities of extension to multithreaded approach...
    Current Qt projects : QCodeEdit, RotiDeCode

  16. #14
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    IMHO... When I've had to limit the number of application instances in the past, I've used the port-method. With Qt it's simple to do as well. Granted, there are some problems like, what happens if another app is already using the requested port, etc. But i believe that handling those issues are simpler than creating platform specific code for locking processes or handling lock files (which in the worst case can require one or more monitoring processes to clean up after crashes etc).

    Those are just my two cents, for whatever they're worth
    Last edited by TheRonin; 11th May 2009 at 10:33. Reason: Accidentally posted.

  17. #15
    Join Date
    Mar 2006
    Location
    Mexico City
    Posts
    31
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by Chicken Blood Machine View Post
    A quick and dirty cross-platform solution would be to create a 'lock' file at startup and delete the file at shutdown. Any new instances check if the file exists first before continuing.

    This isn't foolproof, since if your app crashes, you will have to delete the file manually.
    IMO this is the best solution, easy and clean. It works for all platforms, it can be instrumented through the net, or locally, it can be a counter to limit number of applications...

    Simple is, in many cases, the best solution...

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by arnaiz View Post
    Simple is, in many cases, the best solution...
    It has its strong disadvantages. There is really no way telling if the lock file is stale or not. Under Windows you can easily use a file to make a singleton application by demanding a mandatory lock on the file. On the other hand I don't know if the lock will be released when the app gets killed. So a solution is easy but the drawback is that you might not be able to restart the application after a crash. The same goes with files holding pids of processes. You can never be sure if the lock file is stale or not and eventually you have to guess (for example by trying to find the process in the process table) or ask the user (like for example kmail does). So simple is easy to implement, but can fail as easily.

  19. #17
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Mixing kinda lock file and TCP connection is definitely the best slution IMO. I'm attaching the source I've come to. It can lock the application to whatever number of instances wanted and can send messages to the first created instances (this is a limitation I didn't managed to fix with the current design so I'm working on a new version...)

    Well actually I can't attach my file (too much already attached by me on other posts : I'm waiting for an answer from the admins and will attach it ASAP )
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2006, 18:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.