简体   繁体   English

Bash:获取子进程名称

[英]Bash: Get child process name

I'm new with bash. 我是bash新手。 I'm have this little code: 我有这个小代码:
bash.sh bash.sh

./mama

mama.cpp 妈妈

#include <stdlib.h>
int main()
{
  system("./shvili");
  while(1){}
}

shvili.cpp shvili.cpp

int main()
{
  while(1){}
}

As it shows me mama is parent of shvili process. 正如我shvili那样, mamashvili进程的父shvili I have situation like this where I don't know exactly the name of child process. 我遇到这样的情况,我不知道子进程的确切名称。 So my question is, How can I get the PID of child process from c++?(It would be more comfortable for me to get the name of process). 所以我的问题是,如何从c ++获取子进程的PID?(对我来说,获得进程的名称会更舒服)。

A way to at least check via bash in case "situation like this where I don't know exactly the name of child process" but knows the name of the parent process assumed to be unique, one can use in bash based on ps , grep , sed (to remove leading space for smaller pids), tr (to squeeze multiple consecuting spaces into one), and cut : 一种至少通过bash进行检查的方式,以防“我不知道子进程的确切名称,但知道父进程的名称是唯一的,可以在基于psgrep bash使用” , sed (删除较小的pid的前导空间), tr (将多个相交的空间压缩为一个),然后cut

$> cat foo_bar.sh 
#! /bin/bash
sleep 120

$> ./foo_bar.sh &
[1] 89239

$> ps -eo pid,ppid,args|grep -e " $(ps -eo pid,args| grep foo_bar.sh| grep -v grep| sed s/^\ //g |cut -f 1 -d ' ') "|grep -v foo_bar.sh| sed s/^\ //g | tr -s ' ' | cut -f 1,3 -d ' '
89241 sleep

Thus the unique name of the parent process is used to determine the parent pid: 因此,使用父进程的唯一名称来确定父pid:

$> ps -eo pid,args| grep foo_bar.sh| grep -v grep| sed s/^\ //g |cut -f 1 -d ' '
89239

This evaluated in a sub process ( $(...) ) is used to grep the correct line from another ps call to determine the seeked pid of child and name (without additional arguments and without prior knowledge of the childs name. 在子进程( $(...) )中求值的结果用于从另一个ps调用grep正确行,以确定所寻求的孩子和孩子的pid(无需附加参数,也无需事先知道孩子的孩子的名字。

Note - as usual in bash some spaces are important - the added space at the end of the search pattern: 注意-与往常一样,bash中的一些空格很重要-搜索模式结尾处添加的空格:

... grep " $(ps -eo pid,args| grep foo_bar.sh| grep -v grep| cut -f 1 -d ' ') " ...

This helps avoid false positives, like when a parent pid is 123, without padding with spaces this would match many pids that contain these digits like 1234, 12345, 1123, ... 这有助于避免误报,例如当父pid为123时,如果不使用空格进行填充,则将匹配包含这些数字的许多pid,例如1234、12345、1123等。

Update (reacting on comment): In case parent process is a_mama (forking off a_shvili sub process) and it is the only process with that name on the machine, then the following should work: 更新 (对评论作出反应):如果父进程是a_mama (派生a_shvili子进程),并且它是机器上唯一具有该名称的进程,则以下应该起作用:

$> p_proc="a_mama"
$> ps -eo pid,ppid,args|grep -e " $(ps -eo pid,args| grep ${p_proc}| grep -v grep| sed s/^\ //g | cut -f 1 -d ' ') "|grep -v ${p_proc}| sed s/^\ //g | tr -s ' ' | cut -f 1,3 -d ' '
12346 a_shvili

you can try using pidof to get the pid of a particular process. 您可以尝试使用pidof来获取特定进程的pid。

Example

char line[LEN];
FILE *cmd = popen("pidof...", "r");

fgets(line, LEN, cmd);
pid_t pid = strtoul(line, NULL, 10);

pclose(cmd);

You do know the name of the child process it is shvili , how can you start it and not know its name. 您确实知道子进程shvili的名称,但是如何启动它却不知道其名称。

If you with to know the PID of the child, then do not use system . 如果您知道孩子的PID ,则不要使用system system is not a well behaved procedure, do not use it. system不是行为良好的程序,请勿使用它。

Instead use fork and exec : 而是使用forkexec

In parent do: 在家长中:

#include <unistd.h>

int child_pid = fork();
if (child_pid == -1) {
   //fork failed do something about it
} else if (child_pid == 0) {
   //this runs for child
   execl("shvili", NULL);
   //if you get here then exec errored
} else {
   //This runs for parent
   //child_pid has child pid
   //Add parent code hear.
   wait(…);
}

Explanation: 说明:

  • the first branch of the ifs runs if fork errors. 如果fork错误,则ifs的第一个分支运行。
  • else 其他
    • In 2 separate processes: 在2个独立的过程中:
    • the second branch runs for the child. 第二个分支为孩子运行。
    • and the third branch runs for the parent. 第三个分支为父级运行。

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

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