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()?
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.
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;
}
Related
I'm not sure what does this code do.
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
for(int i=0; i<2; i++)
{
int pid=fork();
if(pid==0)
{
printf("child process \n");
printf("Child pid id [%d], parent pid is [%d]\n", (int) getpid(), (int) getppid());
} else if(pid>0)
{
int stats;
wait(&stats);
printf("parent process \n");
printf("Child pid id [%d], parent pid is [%d]\n", (int) getpid(), (int) getppid());
}
}
return 0;
}
I call fork() and assign it's value to variabe pid. Then we go to int stats, next line returns pid=0, then the program displays child process and then the parent process. It works pretty nice, but only when i<1. I thought that it is possible to do the same thing once again, but it's strange. fork() creates a new child process, so if it is used only once, if should create a child process, which parent is an IDE. Why am I wrong and what should I change to make 2 parents and 1 child for each, basically 4 processes?
I am not completely sure what you mean by "2 parents". In any case, you need to return the child so that they do not loop again:
printf("Child pid id [%d], parent pid is [%d]\n", (int) getpid(), (int) getppid());
return 0;
Otherwise you will have the children spawning other processes.
I am a bit confused about why the child process in the following two programs is showing different parents ids.
First program:
int main ( void ) {
int pid, fpid, ppid;
fpid = fork ();
pid = getpid();
ppid = getppid();
printf ("fpid is %d\n", fpid);
sleep(5);
if (fpid > 0){
printf ("\nThis is Parent. My pid %d. My parent's pid %d\n",pid,ppid);
}
else if (fpid ==0){
sleep(1);
printf ("\nThis is Child. My pid %d. My parent's pid %d\n",pid,ppid);
}
else
printf ("fork failed\n");
return (0);
}
Output:
fpid is 53560
fpid is 0
This is Parent. My pid 53559. My parent's pid 44632
MacBook-Pro:~/Desktop/$
This is Child. My pid 53560. My parent's pid 53559
Second program:
int main ( void ) {
int pid, fpid, ppid;
fpid = fork ();
printf ("fpid is is %d\n", fpid);
sleep(5);
if (fpid > 0){
pid = getpid();
ppid = getppid();
printf ("\nThis is Parent. My pid %d. My parent's pid %d\n",pid,ppid);
}
else if (fpid ==0){
sleep(1);
pid = getpid();
ppid = getppid();
printf ("\nThis is Child. My pid %d. My parent's pid %d\n",pid,ppid);
}
else
printf ("fork failed\n");
return (0);
}
Output:
fpid is is 53635
fpid is is 0
This is Parent. My pid 53634. My parent's pid 44632
MacBook-Pro:~/Desktop$
This is Child. My pid 53635. My parent's pid 1
I understand that process 1 is the process that takes over as a parent once the original parent terminates. I guess what I want to know is: isn't the parent process being finished before the child process can process its printf in both cases? Shouldn't the outputs be the same?
Since parent and child processes run concurrently, the order of execution depends on runtime. One of them can finish earlier. When parent finishes before child reaches its getppid(), child process would be adopted by init. Hence the parent id 1.
To see child's actual parent process id:
Let the parent wait for its child termination using wait() or waitpid(), or
Let parent sleep for some perceivable amount like sleep(120) after 'This is parent' printf().
isn't the parent process being finished before the child process can process it's printf in both cases?
Very likely so, but not absolutely certain. You cannot ensure that by sleep()ing for any length of time.
Shouldn't the outputs be the same?
They could be the same, but they are unlikely to be. It's important to note when in each program getppid() is called. The parent process ID reported is the one that applied at the time of the call; it is not necessarily still applicable later, when the value is printed.
My problem is that the children does not have the same parent and does not appear correctly, here is my code:
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid[3];
pid[0] = fork();
if(pid[0] == 0)
{
/* First child */
pid[1] = fork();
if(pid[1] == 0)
{
/* First child continued */
printf("Hello, I'm the first child! My PID is : %d, My PPID is: %d", getpid(), getppid());
sleep(1);
}
else
{
/* Second child */
pid[2] = fork();
if(pid[2] == 0)
{
/* Second child continued */
printf("Hello, I'm the second child! My PID is : %d, My PPID is: %d", getpid(), getppid());
sleep(1);
}
else
{
/* Third child */
printf("Hello, I'm the first child! My PID is : %d, My PPID is: %d", getpid(), getppid());
sleep(1);
}
}
}
else
{
/* Parent */
sleep(1);
wait(0);
printf("Hello, I'm the parent! My PID is : %d, My PPID is: %d", getpid(), getppid());
}
return 0;
}
As of right now when i run the program i will get this as output in bash, where bash has the PID of 11446:
>Hello, I'm the third child! My PID is: 28738, My PPID is: 28735
>Hello, I'm the first child! My PID is: 28742, My PPID is: 28738
>Hello, I'm the second child! My PID is: 28743, My PPID is: 28738
>Hello, I'm the parent! My PID is: 28753, My PPID is: 11446
How do i get the first child to appear first, second child to appear second and the third child to appear last, and get all the children to have the PPID 28753
From man fork:
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in
the parent, no child process is created, and errno is set
appropriately.
Your if-else conditions are swapped.
I am doing a practice of multi processes. I just want to check out the parent process id, and it looks not the same as I expected. Here is my code:
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <string.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void child1()
{
//printf("we are child1..\n");
printf("my pid is:%d\n",getpid());
printf("I am child1..\n");
}
void child2()
{
//printf("we are child2..\n");
printf("my pid is:%d\n",getpid());
printf("I am child2.., my parent is:%d\n",getppid());
}
int main()
{
pid_t childpid[4];
int i;
printf("main pid is:%d\n",getpid());
for (i=0;i<2;i++)
{
childpid[i] = fork();
if (childpid[i]<0)
printf("fork fail..\n");
else if (childpid[i]==0)
{
childpid[i+2] = fork();
if (childpid[i+2] <0)
printf("fork inside fail..\n");
else if (childpid[i+2]==0)
child2();
else // this is parent process..
child1();
}
}
}
and here is the execution result:
my pid is:3951
I am child1..
my pid is:3950
I am child1..
my pid is:3952
I am child2.., my parent is:1118
my pid is:3954
I am child1..
my pid is:3953
I am child2.., my parent is:1118
my pid is:3956
I am child1..
my pid is:3955
I am child2.., my parent is:1118
my pid is:3957
I am child2.., my parent is:1118
since my child2 process is forked inside the process1, I expect the parent pid might be something like:3950. How come my result get:1118?
thanks for your help!
When I run your code verbatim (well, OK; I added void to each empty argument list and made the child functions static), then I got sample output:
main pid is:46761
my pid is:46762
I am child1..
my pid is:46763
my pid is:46764
I am child1..
I am child2.., my parent is:46762
my pid is:46765
I am child2.., my parent is:1
my pid is:46766
I am child1..
my pid is:46768
my pid is:46767
I am child2.., my parent is:46766
I am child1..
my pid is:46769
I am child2.., my parent is:1
For amusement value, I ran the output to a pipe and got:
main pid is:46770
main pid is:46770
my pid is:46773
I am child1..
main pid is:46770
my pid is:46772
I am child1..
main pid is:46770
my pid is:46775
I am child2.., my parent is:1
main pid is:46770
my pid is:46774
I am child2.., my parent is:46772
main pid is:46770
my pid is:46772
I am child1..
my pid is:46776
I am child1..
main pid is:46770
my pid is:46772
I am child1..
my pid is:46778
I am child2.., my parent is:1
main pid is:46770
my pid is:46774
I am child2.., my parent is:46772
my pid is:46777
I am child1..
main pid is:46770
my pid is:46774
I am child2.., my parent is:46772
my pid is:46779
I am child2.., my parent is:46777
See printf() anomaly after fork() for an explanation of that behaviour.
Then I instrumented your code slightly differently, but the core logic is unchanged:
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
static void child1(void)
{
printf("Child1: PID = %d, PPID = %d\n", (int)getpid(), (int)getppid());
}
static void child2(void)
{
printf("Child2: PID = %d, PPID = %d\n", (int)getpid(), (int)getppid());
}
int main(void)
{
pid_t childpid[4];
int i;
printf("main pid is:%d\n", getpid());
for (i = 0; i < 2; i++)
{
fflush(0);
childpid[i] = fork();
if (childpid[i] < 0)
printf("fork fail in PID %d\n", getpid());
else if (childpid[i] == 0)
{
childpid[i + 2] = fork();
if (childpid[i + 2] < 0)
printf("fork fail in PID %d\n", getpid());
else if (childpid[i + 2] == 0)
child2();
else
{
printf("child pid %d forked child %d\n", (int)getpid(), (int)childpid[i+2]);
child1();
}
}
else
printf("main pid %d forked child %d\n", (int)getpid(), (int)childpid[i]);
}
int status;
int corpse;
while ((corpse = wait(&status)) != -1)
printf("PID %d: child %d died with status %.4X\n", (int)getpid(), corpse, status);
printf("PID %d: finished\n", (int)getpid());
return getpid() % 256;
}
I did add the code to wait for children to die, and to report when a process finishes, and added non-zero exit statuses for most of the processes. The code also flushes standard output before forking to keep things clean (even if the output is going to a pipe or file).
On my Mac OS X 10.11.1 El Capitan machine, I got, for example:
main pid is:46730
main pid 46730 forked child 46731
main pid 46730 forked child 46732
child pid 46731 forked child 46733
Child1: PID = 46731, PPID = 46730
child pid 46732 forked child 46734
main pid 46731 forked child 46735
Child1: PID = 46732, PPID = 46730
Child2: PID = 46733, PPID = 46731
Child2: PID = 46734, PPID = 46732
PID 46734: finished
main pid 46733 forked child 46736
PID 46732: child 46734 died with status 8E00
PID 46732: finished
child pid 46735 forked child 46737
Child1: PID = 46735, PPID = 46731
PID 46730: child 46732 died with status 8C00
Child2: PID = 46737, PPID = 46735
child pid 46736 forked child 46738
Child1: PID = 46736, PPID = 46733
PID 46737: finished
Child2: PID = 46738, PPID = 46736
PID 46735: child 46737 died with status 9100
PID 46735: finished
PID 46738: finished
PID 46736: child 46738 died with status 9200
PID 46731: child 46735 died with status 8F00
PID 46736: finished
PID 46733: child 46736 died with status 9000
PID 46733: finished
PID 46731: child 46733 died with status 8D00
PID 46731: finished
PID 46730: child 46731 died with status 8B00
PID 46730: finished
When you study the sequencing in that output, there are some interesting interleavings of execution.
None of that explains why you got 1118; in many ways, that is impossible to explain because no-one can sequence things exactly as on your machine. However, what I've shown is plausible behaviour, and it does not reproduce the 'all processes have a single PID as the PPID'. You should be able to make incremental monitoring improvements to your code and demonstrate which change alters the behaviour on your machine.
Your processes don't wait for their children to terminate. Most likely, the parent process had already terminated and the child was reparented.
I am trying to print the pid of the processes after running the fork() command. Here is my code-
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid;
pid=fork();
if(pid==0)
printf("I am the child.My pid is %d .My parents pid is %d \n",getpid(),getppid());
else
printf("I am the parent.My pid is %d. My childs pid is %d \n",getpid(),pid);
return 0;
}
This is the answer I am getting-
I am the parent.My pid is 2420. My childs pid is 3601
I am the child.My pid is 3601 .My parents pid is 1910
Why is the parents id in 2nd line not 2420.Why am I getting 1910 How can I get this value?
The parent is exiting before the child performs its printf call. When the parent exits, the child gets a new parent. By default this is the init process, PID 1. But recent versions of Unix have added the ability for a process to declare itself to be the "subreaper", which inherits all orphaned children. PID 1910 is apparently the subreaper on your system. See https://unix.stackexchange.com/a/177361/61098 for more information about this.
Put a wait() call in the parent process to make it wait for the child to exit before it continues.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid;
pid=fork();
if(pid==0) {
printf("I am the child.My pid is %d .My parents pid is %d \n",getpid(),getppid());
} else {
printf("I am the parent.My pid is %d. My childs pid is %d \n",getpid(),pid);
wait(NULL);
}
return 0;
}