Reputation: 1834
When I use fork to create a new child process and then call execlp syscall to run a new program in the child. The process ids that I get in the child process after execlp and I get from waitpid syscall after the child terminates are different.
For example, getpid() returns 7103 in the child and waitpid returns 7101 in the parent.
I guess something happens after execlp runs. Can anyone explain this. Thanks.
BTW, I run my code on Fedora.
Here is the code:
/* program parent */
if ((pid = fork()) < 0){
perror("fork failed");
exit(2);
}
if (pid == 0){
// child
execlp("xterm", "xterm", "-e", "./echo_cli", "127.0.0.1", (char *)0);
exit(0);
}
/* ... */
// sig_chld handles SIGCHLD singal
void sig_chld(int signo){
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0){
printf("Child %d terminated\n", pid);
}
return ;
}
/* program echo_cli */
pid = getpid();
Upvotes: 0
Views: 1979
Reputation: 798536
You're executing xterm
, not echo_cli
. Your child's child will of course report a different PID.
Upvotes: 4