【问题标题】:Can I get the PID of a program I execute in another program?我可以获取在另一个程序中执行的程序的 PID 吗?
【发布时间】:2020-12-09 13:01:22
【问题描述】:

如果我在程序的主进程中执行exec,我能否以某种方式获取exec 执行的进程的PID(进程ID),以便稍后向它发送中断/信号?

【问题讨论】:

  • 也许execl("child", ..., getpid(), ...);child 以某种方式使用父pid 来通知它自己的进程ID

标签: c exec pid


【解决方案1】:

是的,在 linux 上,您可以 fork 一个子进程并获得类似于 https://ece.uwaterloo.ca/~dwharder/icsrts/Tutorials/fork_exec/ 中的 PID

#include <stdio.h>

    
int main( void ) {
    char *argv[3] = {"Command-line", ".", NULL};

    int pid = fork();

    if ( pid == 0 ) {
        execvp( "find", argv );
    }

    /* Put the parent to sleep for 2 seconds--let the child finished executing */
    wait( 2 );

    printf( "Finished executing the parent process\n"
            " - the child won't get here--you will only see this once\n" );

    return 0;
}

来源:https://ece.uwaterloo.ca/~dwharder/icsrts/Tutorials/fork_exec/

getpid()也在此链接中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多