Warm tip: This article is reproduced from serverfault.com, please click

c-我可以获取在另一个程序中执行的程序的PID吗?

(c - Can I get the PID of a program I execute in another program?)

发布于 2020-12-09 13:01:22

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

Questioner
user157629
Viewed
0
ralf htp 2020-12-09 21:10:28

是的,在Linux上,你可以派生一个子进程并获得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() 也在此链接中