如何确定应用程序是否已经在运行?C Linux便携/赢

时间:2021-02-28 16:25:01

Is there a way to write a C code that allow us to determine if a previous instance of an application is already running? I need to check this in a portable way for Linux and Windows, both using the last version of GCC avaiable.

是否有一种方法可以编写一个C代码,它允许我们确定一个应用程序的前一个实例是否已经在运行?我需要以一种可移植的方式来检查Linux和Windows,两者都使用了最后版本的GCC avaiable。

Any examples of portable code would be of enormous help. I see two options now:

任何可移植代码的例子都有很大的帮助。我现在有两个选择:

  1. Check process list. Here linux has good tools, but I don't think the same functions apply to windows. Maybe some gnu libraries for both SO? What libraries, or functions?
  2. 检查进程列表。这里linux有很好的工具,但是我不认为同样的功能适用于windows。也许有些gnu库可以同时使用?库,或功能?
  3. Save and lock a file. Now, how to do that in a way that both systems can understand? One problem is where to save the file? Path trees are different from each systems. Also, if a relative path is chosen, two applications can still run with different locked files in different directories.
  4. 保存并锁定一个文件。现在,如何用两种系统都能理解的方式来做呢?一个问题是如何保存文件?路径树不同于每个系统。此外,如果选择了相对路径,两个应用程序仍然可以在不同的目录中运行不同的锁定文件。

Thanks! Beco.

谢谢!构成。

PS. The SO have different requisites, so if you know one and not another, please answer. After all, if there is no portable "single" way, I still may be able to use #ifdef and the codes proposed as answer.

SO有不同的要求,所以如果你知道一个而不知道另一个,请回答。毕竟,如果没有可移植的“单一”方式,我仍然可以使用#ifdef和建议的代码作为答案。

C language (not c++), console application, gcc, linux and windows

C语言(不是c++),控制台应用,gcc, linux和windows

4 个解决方案

#1


1  

Unfortunately, if you limit yourself to C, you may have difficulty doing this portably. With C++, there's boost interprocess's named_mutex, but on C, you will have to either:

不幸的是,如果您将自己限制在C范围内,那么您可能很难以可移植性的方式实现这一点。在c++中,有boost interprocess的named_mutex,但是在C语言中,您必须:

  • UNIXes (including Mac OS): Open and flock a file somewhere. Traditionally you will also write your current PID into this file. NOTE: This may not be safe on NFS; but your options are extremely limited there anyway. On Linux you can use a /dev/shm path if you want to ensure it's local and safe to lock.
  • UNIXes(包括Mac OS):在某个地方打开并聚集一个文件。传统上,您还将把当前PID写到这个文件中。注意:这在NFS上可能不安全;但是你的选择是非常有限的。在Linux上,如果您希望确保锁是本地的和安全的,可以使用/dev/shm路径。
  • Windows: Open and lock a named mutex
  • 窗口:打开并锁定一个命名的互斥对象

#2


1  

for windows, a mutex works well.

对于windows来说,互斥锁很好用。

http://msdn.microsoft.com/en-us/library/ms682411(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ms682411(v = vs.85). aspx

the article also mentions an alternative to a mutex....

文章还提到了另一个互斥锁....

To limit your application to one instance per user, create a locked file in the user's profile directory.

要将应用程序限制为每个用户一个实例,请在用户的配置文件目录中创建一个锁定的文件。

#3


1  

The sort of canonical method in Unixland is to have the process write its own PID to a file in a known location. If this file exists, then the program can check its own pid (available by system call) with the one in that file, and if it's unfamiliar you know that another process is running.

Unixland中典型的方法是让进程将自己的PID写到一个已知位置的文件中。如果这个文件存在,那么程序可以用该文件中的一个来检查它自己的pid(系统调用可用),如果不熟悉,您就知道另一个进程正在运行。

#4


0  

C does not give in-built facilities to check if an application is already running, so, making it cross platform is difficult/impossible. However, on Linux, one can use IPC. And, on Windows (I'm not very experienced in this category), you may find this helpful.

C不提供内部设施来检查应用程序是否已经在运行,因此,使它跨平台是困难的/不可能的。但是,在Linux上,可以使用IPC。而且,在Windows上(我在这方面不是很有经验),你可能会发现这很有帮助。

#1


1  

Unfortunately, if you limit yourself to C, you may have difficulty doing this portably. With C++, there's boost interprocess's named_mutex, but on C, you will have to either:

不幸的是,如果您将自己限制在C范围内,那么您可能很难以可移植性的方式实现这一点。在c++中,有boost interprocess的named_mutex,但是在C语言中,您必须:

  • UNIXes (including Mac OS): Open and flock a file somewhere. Traditionally you will also write your current PID into this file. NOTE: This may not be safe on NFS; but your options are extremely limited there anyway. On Linux you can use a /dev/shm path if you want to ensure it's local and safe to lock.
  • UNIXes(包括Mac OS):在某个地方打开并聚集一个文件。传统上,您还将把当前PID写到这个文件中。注意:这在NFS上可能不安全;但是你的选择是非常有限的。在Linux上,如果您希望确保锁是本地的和安全的,可以使用/dev/shm路径。
  • Windows: Open and lock a named mutex
  • 窗口:打开并锁定一个命名的互斥对象

#2


1  

for windows, a mutex works well.

对于windows来说,互斥锁很好用。

http://msdn.microsoft.com/en-us/library/ms682411(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ms682411(v = vs.85). aspx

the article also mentions an alternative to a mutex....

文章还提到了另一个互斥锁....

To limit your application to one instance per user, create a locked file in the user's profile directory.

要将应用程序限制为每个用户一个实例,请在用户的配置文件目录中创建一个锁定的文件。

#3


1  

The sort of canonical method in Unixland is to have the process write its own PID to a file in a known location. If this file exists, then the program can check its own pid (available by system call) with the one in that file, and if it's unfamiliar you know that another process is running.

Unixland中典型的方法是让进程将自己的PID写到一个已知位置的文件中。如果这个文件存在,那么程序可以用该文件中的一个来检查它自己的pid(系统调用可用),如果不熟悉,您就知道另一个进程正在运行。

#4


0  

C does not give in-built facilities to check if an application is already running, so, making it cross platform is difficult/impossible. However, on Linux, one can use IPC. And, on Windows (I'm not very experienced in this category), you may find this helpful.

C不提供内部设施来检查应用程序是否已经在运行,因此,使它跨平台是困难的/不可能的。但是,在Linux上,可以使用IPC。而且,在Windows上(我在这方面不是很有经验),你可能会发现这很有帮助。