【发布时间】:2020-12-09 13:01:22
【问题描述】:
如果我在程序的主进程中执行exec,我能否以某种方式获取exec 执行的进程的PID(进程ID),以便稍后向它发送中断/信号?
【问题讨论】:
-
也许
execl("child", ..., getpid(), ...);和child以某种方式使用父pid 来通知它自己的进程ID。
如果我在程序的主进程中执行exec,我能否以某种方式获取exec 执行的进程的PID(进程ID),以便稍后向它发送中断/信号?
【问题讨论】:
execl("child", ..., getpid(), ...); 和child 以某种方式使用父pid 来通知它自己的进程ID。
是的,在 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()也在此链接中
【讨论】: