简体   繁体   English

如何使进程知道同一程序的其他进程

[英]How to make a process aware of other processes of the same program

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? 我的第一个想法是编写一个包含进程的PID的文件,并在每次程序执行时查找该文件,但该文件的“正确”位置和名称在哪里? Is there a better, or more "correct" way? 是否有更好或更“正确”的方式?

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. 我想过只发送一个信号,比如SIGUSR1,但这不允许我发送更多信息,比如用户执行第二个进程的X11显示。 How to send this info? 如何发送此信息?

The program is linked against Gtk, so a solution that uses the glib is OK. 该程序与Gtk链接,因此使用glib的解决方案是可以的。

Putting the pid in a file is a common way of achieving this. 将pid放在文件中是实现此目的的常用方法。 For daemons ("system programs"), the common place to put such a file is /var/run/PROGRAM.pid . 对于守护进程(“系统程序”),放置这样一个文件的常见位置是/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文件隐藏在用户的homedir中(如果程序也有配置文件,则将配置文件和pid文件放在home目录的子目录中)。

Sending information to the "master" instance is most commonly achieved using Unix domain sockets, also known as local sockets. 使用Unix域套接字(也称为本地套接字)最常将信息发送到“主”实例。 With a socket, you won't need a pid file (if no-one listens on the socket, the process knows it's master). 使用套接字,您将不需要pid文件(如果没有人监听套接字,则进程知道它是主服务器)。

Unix domain sockets. Unix域套接字。 Have the first instance create one in a temporary directory, then have other instances communicate with it via that. 让第一个实例在临时目录中创建一个,然后让其他实例通过它与之通信。

Writing a PID file is a common approach. 编写PID文件是一种常见的方法。 Check the pidfile(3) library. 检查pidfile(3)库。

Does linux have the equivalent of a named mutex or semaphore? linux是否具有相应的命名互斥锁或信号量? 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? 所以你可以查看它是否“锁定”然后警告用户他们已经有一个并关闭它?

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/

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. 有时,应用程序的配置文件包含PID文件的路径,有时则使用硬编码路径。 Usually application put the PID file in /tmp , in /var (if they run with uid 0) or in their local directory ( ~/.application/ ). 通常应用程序将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文件放在何处的一般建议,只需选择您喜欢的位置即可。

You can certainly use a Unix domain socket; 你当然可以使用Unix域套接字; I think most applications (which don't use a higher-level system like DCOP or DBUS) use these. 我认为大多数应用程序(不使用像DCOP或DBUS这样的高级系统)都使用这些。

If you're happy for it to be Linux-specific, you can use an "abstract namespace" unix socket; 如果你很高兴它是特定于Linux的,你可以使用“抽象命名空间”unix socket; these are rather nice because they don't need to exist in the filesystem. 这些都相当不错,因为它们不需要存在于文件系统中。

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?). 一个用户不应该能够触发另一个用户的应用程序副本中的行为,并且需要安全性以确保用户不能轻易地相互DoS(例如:如果用户A的程序副本挂起,是否会停止用户B从开始?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MPI如何使在根进程中初始化的变量在其他进程中可见 - MPI How to make variables initialized in root process visible in other processes 有关如何使监视程序了解并处理其中一个病房的退出代码的设计思想 - Design idea on how to make a watchdog program to be aware and handle an exit code of one of it's wards 如何使两个子进程互相等待 - how to make two child processes waiting for each other 如何使我的 Python 程序提供的功能可用于在同一台计算机或其他计算机上运行的其他语言编写的程序? - How can I make the functions provided by my Python program available to programs written in other languages running on the same or other computers? 如何使程序在其他计算机上运行? - How to make a program run on other computers? 如何使我的应用程序能够感知DPI? - How to make my application DPI-aware? 如何让 Scons 中的 SConscript 知道 header 位置? - How to make a SConscript in Scons aware of a header location? 如何使超链接调用相同的C ++ CGI进程 - How to make hyperlinks call same C++ CGI process 如何杀死程序的所有进程? - How can I kill all processes of a program? FIFO块程序,直到读取其他进程 - FIFO block program until other process read
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM