sh 杀死非法数:杀死并行进程

sh kill illegal number : Kill parallel process

本文关键字:并行 进程 非法 sh      更新时间:2023-10-16

我正在尝试使用C++程序打开多个并行进程。他们四个必须并行运行。但是当我的C++程序关闭时,我想杀死每个进程。这是我的尝试:

system("python okcsend.py & PID1=$! python okccnysend.py & PID2=$! python okc.py & PID3=$! python okccny.py & PID4=$!");

当试图杀死他们时,这就是我所做的:

system("kill PID1; kill PID2; kill PID3; kill PID4");

但是,这就是我得到的:

sh: 1: kill: Illegal number: PID1
sh: 1: kill: Illegal number: PID2
sh: 1: kill: Illegal number: PID3
sh: 1: kill: Illegal number: PID4

正确的方法是什么?

谢谢。

您需要分别从每个进程中取回 PID。

伪代码:

pid1 = system("python okcsend.py & echo $!)
pid2 = system("python okcsend.py & echo $!)
pid3 = system("python okcsend.py & echo $!)
pid4 = system("python okcsend.py & echo $!)

然后你可以做这样的事情:

system("kill " + pid1 + "; kill " + pid2 + "; kill " + pid3 + "; kill " + pid4 + ";");