如果我执行exec
一个程序的主进程,是否可以某种方式获取该进程执行的PID(进程ID),exec
以便稍后向其发送中断/信号?
是的,在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()
也在此链接中