Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 48627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:18:35+00:00 2026-05-10T16:18:35+00:00

I need to make sure that user can run only one instance of my

  • 0

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 running, and quit in such case.

The first thing that came to my mind was to create a file somewhere, when the program starts. Then, each other instance of the program would check for this file and exit if it found it.
The trouble is, that the program must always exit gracefully and be able to delete the file it created, for this to work. In case of, say, power outage, the lock file remains in place and the program can’t be started again.

To solve this, I decided to store the first program’s process ID into the lock file and when another instance starts, it checks if the PID from the file is attached to some running process.
If the file doesn’t exist, is empty, or the PID doesn’t correspond to any existing process, the program continues to run and writes its own PID to the file.

This seems to work quite fine – even after an unexpected shutdown, the chance that the (now obsolete) process ID will be associated with some other program, seems to be quite low.

But it still doesn’t feel right (there is a chance of getting locked by some unrelated process) and working with process IDs seems to go beyond the standard C++ and probably isn’t very portable either.

So, is there another (more clean and secure) way of doing this? Ideally one that would work with the ISO 98 C++ standard and on Windows and *nix alike.
If it cannot be done platform-independently, Linux/Unix is a priority for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:18:35+00:00Added an answer on May 10, 2026 at 4:18 pm

    There are several methods you can use to accomplish only allowing one instance of your application:

    Method 1: Global synchronization object or memory

    It’s usually done by creating a named global mutex or event. If it is already created, then you know the program is already running.

    For example in windows you could do:

        #define APPLICATION_INSTANCE_MUTEX_NAME '{BA49C45E-B29A-4359-A07C-51B65B5571AD}'      //Make sure at most one instance of the tool is running     HANDLE hMutexOneInstance(::CreateMutex( NULL, TRUE, APPLICATION_INSTANCE_MUTEX_NAME));     bool bAlreadyRunning((::GetLastError() == ERROR_ALREADY_EXISTS));     if (hMutexOneInstance == NULL || bAlreadyRunning)     {         if(hMutexOneInstance)         {             ::ReleaseMutex(hMutexOneInstance);             ::CloseHandle(hMutexOneInstance);         }         throw std::exception('The application is already running');     } 

    Method 2: Locking a file, second program can’t open the file, so it’s open

    You could also exclusively open a file by locking it on application open. If the file is already exclusively opened, and your application cannot receive a file handle, then that means the program is already running. On windows you’d simply not specify sharing flags FILE_SHARE_WRITE on the file you’re opening with CreateFile API. On linux you’d use flock.

    Method 3: Search for process name:

    You could enumerate the active processes and search for one with your process name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 83k
  • Answers 83k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the following foreach ( var item in numbers.Select( (x,i)… May 11, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer This link just came up on reddit today, has some… May 11, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer you can override all the properties and put a Browsable… May 11, 2026 at 4:50 pm

Related Questions

My program starts a process and I need to make sure it is killed
I am writing an application in C# that needs to run as a service
I come from the land of Java, C#, etc. I am working on a
I am trying to write a system restart Task for Windows Vista. I'm a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.