如何使一个进程意识到同一程序的其他进程

时间:2022-01-14 00:40:57

I must write a program that must be aware of another instance of itself running on that machine, and communicate with it, then die. I want to know if there is a canonical way of doing that in Linux.

我必须编写一个程序,该程序必须意识到自己在那台机器上运行的另一个实例,并与它通信,然后死亡。我想知道在Linux中是否有一种规范的方法可以做到这一点。

My first thought was to write a file containing the PID of the process somewere, and look for that file every time the program executes, but where is the "right" place and name for that file? Is there a better, or more "correct" way?

我的第一个想法是编写一个包含进程的PID的文件,并且每次执行程序时都要查找该文件,但是该文件的“正确”位置和名称在哪里?有没有更好或更“正确”的方法?

Then I must communicate, saying the user tried to run it, but since there is another instance it will hand over the job and exit. I thought of just sending a signal, like SIGUSR1, but that would not allow me to send more information, like the X11 display from where the user executed the second process. How to send this info?

然后我必须进行通信,说用户试图运行它,但是由于有另一个实例,它将提交作业并退出。我想只发送一个信号,比如SIGUSR1,但是这不能让我发送更多的信息,比如用户执行第二个进程的地方的X11显示。如何发送此信息?

The program is linked against Gtk, so a solution that uses the glib is OK.

程序链接到Gtk,所以使用glib的解决方案是可以的。

6 个解决方案

#1


10  

Putting the pid in a file is a common way of achieving this. For daemons ("system programs"), the common place to put such a file is /var/run/PROGRAM.pid. For user programs, put the pid file hidden in the user's homedir (if the program also has configuration files, then put both config files and the pid file in a subdir of the home dir).

将pid放入文件中是实现这一点的一种常见方法。对于守护进程(“系统程序”),放置此类文件的常见位置是/var/run/PROGRAM.pid。对于用户程序,将pid文件隐藏在用户的homedir中(如果程序也有配置文件,那么将配置文件和pid文件放在homedir的子目录中)。

Sending information to the "master" instance is most commonly achieved using Unix domain sockets, also known as local sockets. With a socket, you won't need a pid file (if no-one listens on the socket, the process knows it's master).

向“主”实例发送信息通常使用Unix域套接字(也称为本地套接字)实现。有了套接字,您就不需要一个pid文件(如果没有人监听套接字,这个过程就知道它的主人)。

#2


9  

Unix domain sockets. Have the first instance create one in a temporary directory, then have other instances communicate with it via that.

Unix域套接字。让第一个实例在临时目录中创建一个实例,然后让其他实例通过它与它通信。

#3


5  

Writing a PID file is a common approach. Check the pidfile(3) library.

编写PID文件是一种常见的方法。检查pidfile(3)图书馆。

#4


2  

Does linux have the equivalent of a named mutex or semaphore? So you can check to see if it's 'locked' and then warn the user they already have one out there and close it out?

linux是否具有等价的命名互斥或信号量?所以你可以检查它是否被“锁住”,然后警告用户他们已经有一个了,然后关闭它?

does this make sense from this link? http://www.linuxquestions.org/questions/programming-9/named-mutex-in-linux-296816/

这个链接有意义吗?http://www.linuxquestions.org/questions/programming-9/named-mutex-in-linux-296816/

#5


1  

There are many ways to do this. The way you proposed (using a file containing the PID) is a valid one and is used by many applications.

有很多方法可以做到这一点。您提出的方法(使用包含PID的文件)是有效的,并且被许多应用程序使用。

Some times the application's configuration file contains the path for the PID file, other times a hardcoded path is used. Usually application put the PID file in /tmp, in /var (if they run with uid 0) or in their local directory (~/.application/).

有时应用程序的配置文件包含PID文件的路径,有时使用硬编码路径。通常应用程序将PID文件放在/tmp、/var(如果它们使用uid 0运行)或本地目录(~/.application/)中。

Wouldn't have a general suggestion on where to put your PID file, just choose the place you prefer.

对于将PID文件放在何处没有一般的建议,只需选择您喜欢的位置。

#6


1  

You can certainly use a Unix domain socket; I think most applications (which don't use a higher-level system like DCOP or DBUS) use these.

您当然可以使用Unix域套接字;我认为大多数应用程序(它们不使用像DCOP或DBUS这样的高级系统)都使用这些。

If you're happy for it to be Linux-specific, you can use an "abstract namespace" unix socket; these are rather nice because they don't need to exist in the filesystem.

如果您喜欢它是特定于linux的,可以使用“抽象名称空间”unix套接字;这些非常好,因为它们不需要存在于文件系统中。

If your program is user-oriented, it should probably be multiuser aware; one user should not be able to trigger behaviour in another user's copy of the app, and security needs to be in place to ensure that users cannot DoS each other easily either (Example: if user A's copy of the program hangs, does it stop user B's from starting?).

如果您的程序是面向用户的,那么它可能应该具有多用户意识;一个用户不应该能够在另一个用户的应用拷贝中触发行为,并且需要有安全措施来确保用户不能很容易地相互执行(例如:如果用户A的程序拷贝挂起,它是否阻止用户B的启动?)

#1


10  

Putting the pid in a file is a common way of achieving this. For daemons ("system programs"), the common place to put such a file is /var/run/PROGRAM.pid. For user programs, put the pid file hidden in the user's homedir (if the program also has configuration files, then put both config files and the pid file in a subdir of the home dir).

将pid放入文件中是实现这一点的一种常见方法。对于守护进程(“系统程序”),放置此类文件的常见位置是/var/run/PROGRAM.pid。对于用户程序,将pid文件隐藏在用户的homedir中(如果程序也有配置文件,那么将配置文件和pid文件放在homedir的子目录中)。

Sending information to the "master" instance is most commonly achieved using Unix domain sockets, also known as local sockets. With a socket, you won't need a pid file (if no-one listens on the socket, the process knows it's master).

向“主”实例发送信息通常使用Unix域套接字(也称为本地套接字)实现。有了套接字,您就不需要一个pid文件(如果没有人监听套接字,这个过程就知道它的主人)。

#2


9  

Unix domain sockets. Have the first instance create one in a temporary directory, then have other instances communicate with it via that.

Unix域套接字。让第一个实例在临时目录中创建一个实例,然后让其他实例通过它与它通信。

#3


5  

Writing a PID file is a common approach. Check the pidfile(3) library.

编写PID文件是一种常见的方法。检查pidfile(3)图书馆。

#4


2  

Does linux have the equivalent of a named mutex or semaphore? So you can check to see if it's 'locked' and then warn the user they already have one out there and close it out?

linux是否具有等价的命名互斥或信号量?所以你可以检查它是否被“锁住”,然后警告用户他们已经有一个了,然后关闭它?

does this make sense from this link? http://www.linuxquestions.org/questions/programming-9/named-mutex-in-linux-296816/

这个链接有意义吗?http://www.linuxquestions.org/questions/programming-9/named-mutex-in-linux-296816/

#5


1  

There are many ways to do this. The way you proposed (using a file containing the PID) is a valid one and is used by many applications.

有很多方法可以做到这一点。您提出的方法(使用包含PID的文件)是有效的,并且被许多应用程序使用。

Some times the application's configuration file contains the path for the PID file, other times a hardcoded path is used. Usually application put the PID file in /tmp, in /var (if they run with uid 0) or in their local directory (~/.application/).

有时应用程序的配置文件包含PID文件的路径,有时使用硬编码路径。通常应用程序将PID文件放在/tmp、/var(如果它们使用uid 0运行)或本地目录(~/.application/)中。

Wouldn't have a general suggestion on where to put your PID file, just choose the place you prefer.

对于将PID文件放在何处没有一般的建议,只需选择您喜欢的位置。

#6


1  

You can certainly use a Unix domain socket; I think most applications (which don't use a higher-level system like DCOP or DBUS) use these.

您当然可以使用Unix域套接字;我认为大多数应用程序(它们不使用像DCOP或DBUS这样的高级系统)都使用这些。

If you're happy for it to be Linux-specific, you can use an "abstract namespace" unix socket; these are rather nice because they don't need to exist in the filesystem.

如果您喜欢它是特定于linux的,可以使用“抽象名称空间”unix套接字;这些非常好,因为它们不需要存在于文件系统中。

If your program is user-oriented, it should probably be multiuser aware; one user should not be able to trigger behaviour in another user's copy of the app, and security needs to be in place to ensure that users cannot DoS each other easily either (Example: if user A's copy of the program hangs, does it stop user B's from starting?).

如果您的程序是面向用户的,那么它可能应该具有多用户意识;一个用户不应该能够在另一个用户的应用拷贝中触发行为,并且需要有安全措施来确保用户不能很容易地相互执行(例如:如果用户A的程序拷贝挂起,它是否阻止用户B的启动?)