简体   繁体   English

是否可以在具有相同PID的Shell脚本中运行多个可执行文件?

[英]is it possible to run multiple executables in a shell script with the same PID?

I have a simple Shell script in which multiple executables will run sequentially. 我有一个简单的Shell脚本,其中多个可执行文件将顺序运行。 Every time a new executable starts to run, a new process with a new PID will start. 每当新的可执行文件开始运行时,带有新PID的新进程就会启动。 Is it possible to run them with the same PID? 是否可以使用相同的PID运行它们? I know for a shell script, we can use "source". 我知道对于shell脚本,我们可以使用“源代码”。 But I do not know how to handle executables. 但是我不知道如何处理可执行文件。

The PID is assigned by OS when shell creates a new process. 当Shell创建新进程时,PID由OS分配。 There is no way to tell the OS to use some specific PID. 无法告诉操作系统使用某些特定的PID。 So it's not possible. 所以这是不可能的。

In principle, I believe it should be possible, but in practice it would be very complicated and brittle. 原则上,我认为应该有可能,但是在实践中这将是非常复杂和脆弱的。

The exec family of system calls in Linux allows a process to replace itself with an entirely new process, which holds on to the same PID. Linux中的exec系列系统调用允许一个进程将自己替换为一个拥有相同PID的全新进程。 The tricky part would be to somehow "return" from the second process back to the first. 棘手的部分是以某种方式从第二个过程“返回”到第一个过程。 When exec is called, the OS loads everything it needs to start running the new process, and wipes out every piece of state related to the current process (the one being replaced). 调用exec ,操作系统将加载开始运行新进程所需的所有内容,并清除与当前进程相关的所有状态(被替换的状态)。 And when the new process terminates, the OS releases all resources (including the PID) associated with that process. 当新进程终止时,操作系统将释放与该进程关联的所有资源(包括PID)。

So if you really wanted to do this, you would have to hijack how processes terminate in order to restart your original process rather than let the OS clean everything up. 因此,如果您真的想这样做,则必须劫持进程如何终止才能重新启动原始进程,而不是让操作系统清理所有内容。 How can you do this? 你该怎么做? Well, execle and execvpe functions allow a program to specify the environment of the new process before starting the process. 那么, execleexecvpe功能允许一个程序来启动进程之前指定新进程的环境 Since every process depends on libc (or equivalent) to bring up/tear down a process, you should be able to provide a custom libc which would re-start execution of your script, or exec another process. 由于每个进程都依赖libc (或等效程序)来启动/删除进程,因此您应该能够提供一个自定义的libc ,它将重新开始执行脚本或exec另一个进程。 The great difficulty would be hacking such a libc . 巨大的困难将是破解这样的libc Additionally, you would have to figure out a good way for your master program to keep state even though the OS wipes away any memory it might have been using when it called exec . 另外,即使操作系统擦除了调用exec时可能已经使用的任何内存,您也必须为主程序找到一种保持状态的好方法。 You can probably accomplish this with temporary files. 您可能可以使用临时文件来完成此操作。

So long story short, don't do it. 长话短说,不要这样做。 While it's kind of fun for me to sit here and think about the massive hacks that would be required to pull this off, it would be a huge pain and I'm sure there is a much more elegant solution to whatever problem you are actually trying to solve. 我坐在这里思考一下实现这一目标所需要的大量技巧,虽然这很有趣,但是这将是一个巨大的痛苦,而且我敢肯定,对于您实际尝试的任何问题,都有更优雅的解决方案解决。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM