如何判断应用程序是否已经在运行? C 便携式 Linux/Win
有没有办法编写 C 代码来允许我们确定应用程序的先前实例是否已经在运行?我需要在 Linux 和 Windows 上以可移植的方式检查这一点,两者都使用可用的最新版本的 GCC。
任何可移植代码的示例都会有巨大的帮助。我现在看到两个选项:
- 检查进程列表。这里linux有很好的工具,但我认为同样的功能不适用于windows。也许有一些适用于这两个 SO 的 gnu 库?什么库或函数?
- 保存并锁定文件。现在,如何以两个系统都能理解的方式做到这一点?一个问题是文件保存在哪里?每个系统的路径树都不同。此外,如果选择相对路径,两个应用程序仍然可以在不同目录中使用不同的锁定文件运行。
谢谢! 贝科。
PS。 SO 有不同的要求,因此如果您知道其中一项而不是另一项,请回答。毕竟,如果没有可移植的“单一”方式,我仍然可以使用 #ifdef 和建议作为答案的代码。
C 语言(不是 c++)、控制台应用程序、gcc、linux 和窗口
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.
Any examples of portable code would be of enormous help. I see two options now:
- 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?
- 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.
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.
C language (not c++), console application, gcc, linux and windows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,如果您将自己限制在 C 语言上,那么您可能很难移植。对于 C++,有 boost interprocess 的 named_mutex ,但在 C 上,您必须:
/dev/shm
路径。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:
/dev/shm
path if you want to ensure it's local and safe to lock.对于 Windows,互斥体效果很好。
http://msdn.microsoft.com/en- us/library/ms682411(v=vs.85).aspx
文章还提到了互斥锁的替代方案...
要将应用程序限制为每个用户一个实例,请在用户的配置文件目录中创建一个锁定文件。
for windows, a mutex works well.
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.
Unixland 中的一种规范方法是让进程将自己的 PID 写入已知位置的文件中。如果该文件存在,则程序可以使用该文件中的 pid 检查自己的 pid(可通过系统调用获得),如果不熟悉,您就知道另一个进程正在运行。
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.
C 没有提供内置的工具来检查应用程序是否已经在运行,因此,使其跨平台是困难/不可能的。但是,在 Linux 上,可以使用 IPC。而且,在 Windows 上(我在这个类别上不是很有经验),您可能会发现 这 有帮助。
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.