我可以获取我在另一个程序中执行的程序的 PID 吗?
Can I get the PID of a program I execute in another program?
如果我在程序的主进程中执行 exec
,我能否以某种方式获取 exec
执行的进程的 PID(进程 ID)以便发送中断/信号以后会怎样?
在 linux 上是的,您可以分叉一个子进程并获取 PID,就像在 https://ece.uwaterloo.ca/~dwharder/icsrts/Tutorials/fork_exec/
中一样
#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()
也在这个link
如果我在程序的主进程中执行 exec
,我能否以某种方式获取 exec
执行的进程的 PID(进程 ID)以便发送中断/信号以后会怎样?
在 linux 上是的,您可以分叉一个子进程并获取 PID,就像在 https://ece.uwaterloo.ca/~dwharder/icsrts/Tutorials/fork_exec/
中一样#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()
也在这个link