Reputation: 123
In order to get the PID of the child process I am doing this.
pid_t pid;
pid=fork();
if(pid==0){
//child's work
}else{
printf("The child's PID is %d",pid);
}
I want to print the child's pid
from parent! So is it ok if I printf
pid
or do I need to use getpid()
?
Upvotes: 0
Views: 1839
Reputation: 53
I think this sums up the question :
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("In child => Own pid : %d\n", getpid());
printf("In child => Parent's pid : %d\n", getppid());
}
else{
printf("In Parent => Child's pid is %d\n", pid);
printf("In Parent => Own pid : %d\n", getpid());
}
return 0;
}
Upvotes: 1
Reputation: 23832
On success fork
returns the pid
of the child process, so pid
will be the pid of the child process, So I'd say it's correct.
getpid
is to get the pid of the current process, so it's not suited to get the child's pid.
Upvotes: 1