I was running the program
#include<stdio.h>
#include <unistd.h>
main()
{
pid_t pid, ppid;
printf("Hello World1\n");
pid=fork();
if(pid==0)
{
printf("I am the child\n");
printf("The PID of child is %d\n",getpid());
printf("The PID of parent of child is %d\n",getppid());
}
else
{
printf("I am the parent\n");
printf("The PID of parent is %d\n",getpid());
printf("The PID of parent of parent is %d\n",getppid());
}
}
THe output I got was.
$ ./a.out
Hello World1
I am the parent
The PID of parent is 3071
The PID of parent of parent is 2456
I am the child
The PID of child is 3072
The PID of parent of child is 1
I couldnt understand the line
The PID of parent of child is 1
It should have been 3071?
Because parent process is finished by the time the child asks for its parent's pid.
When a process finishes, all its children are reassigned as children of the init process, which pid is 1.
Try using wait() in parent's code to wait for the child to execute. It should then work as you expect.
pid 1 is for init process and it looks like the parent process finished before the child could print.
If you edit the else part like this :-
else
{
printf("I am the parent\n");
printf("The PID of parent is %d\n",getpid());
printf("The PID of parent of parent is %d\n",getppid());
while(1);
}
You should see the right output.
Related
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.
I have created a program that creates two child process for a parent process. The program is to output the parent's process showing its process ID and then the two child processes showing their IDs and the ID of the parent. The parent process is supposed to capture the child process using the wait() function after the program exits and print an output.
However, my program keeps creating parent processes and giving children to those processes. I only want one parent process for the two child processes. Inside the while loop is the wait() function that is supposed to check for the changed state of the children process and print " Child 'xxx' process terminated". Instead, it is terminating some of the parent processes and other random processes.
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t cpid, cpid2, wpid;
int child = fork();
int child2 = fork();
int status;
if ((child = fork()) == 0){
child = cpid;
}
if((child2 = fork()) == 0){
child2 = cpid2;
}
else{
printf("I am the parent %d\n", getppid());
printf("I am the process %d created by %d\n", cpid, getppid());
printf("I am the process %d created by %d\n", cpid2, getppid());
while ((wpid = wait(&status)) > 0){
printf("Child process %d terminated\n", wpid);
}
}
return (0);
}
My output is showing me this
I am the parent 5764
I am the process 2 created by 5764
I am the process 6411548 created by 5764
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 23612
I am the process 2 created by 23612
I am the process 6411548 created by 23612
I am the parent 15096
I am the process 2 created by 15096
I am the process 6411548 created by 15096
I am the parent 24276
I am the process 2 created by 24276
I am the process 6411548 created by 24276
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 5764
I am the process 2 created by 5764
I am the process 6411548 created by 5764
Child process 17016 terminated
Child process 18584 terminated
Child process 13984 terminated
Child process 8480 terminated
Child process 10816 terminated
Child process 21968 terminated
Child process 23388 terminated
Child process 11452 terminated
Child process 2776 terminated
Child process 19328 terminated
Child process 17116 terminated
Child process 18352 terminated
Child process 24276 terminated
Child process 15096 terminated
Child process 5764 terminated
Once you fork(), both the child and the parent are calling fork() again if you want to call fork() in the parent process only, check the return value before forking again.
int child = fork();
// This will be called by both, the child and the parent
int child2 = fork();
when fork() returns it returns the child PID in the parent and 0 in the child.
Start by reading the fork man page as well as the getppid / getpid man pages.
From fork's documentation:
On success, the PID of the child process is returned in the parent's
thread of execution, and a 0 is returned in the child's thread of
execution. On failure, a -1 will be returned in the parent's context,
no child process will be created, and errno will be set appropriately.
if ((child = fork()) == 0){
printf(" %u and %u", getpid(), getppid());
} else{ /* avoids error checking*/
printf("Parent - %u ", getpid());
}
The first child you fork() is forking the second child and also you're not handling else for the first fork()
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t cpid, cpid2, wpid;
int child = fork();
int child2 = fork();
int status;
if ((child = fork()) == 0){
child = cpid;
}
else {
if((child2 = fork()) == 0){
child2 = cpid2;
}
else{
printf("I am the parent %d\n", getppid());
printf("I am the process %d created by %d\n", cpid, getppid());
printf("I am the process %d created by %d\n", cpid2, getppid());
while ((wpid = wait(&status)) > 0){
printf("Child process %d terminated\n", wpid);
}
}
The way you're doing it, the first child is forking it's own child which defines a handler for the case where the returned PID != 0. So you will spawn two of the second child and handle the parent case twice.
I want to write an UNIX program that creates N child processes, so that the first process creates one child process, then this child creates only one process that is its child, then the child of the child creates another child etc.
Here's my code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N=3;
int i=0;
printf("Creating %d children\n", N);
printf("PARENT PROCESS\nMy pid is:%d \n",getpid() );
for(i=0;i<N;i++)
{
pid_t pid=fork();
if(pid < 0)
{
perror("Fork error\n");
return 1;
}
else if (pid==0) /* child */
{
printf("CHILD My pid is:%d my parent pid is %d\n",getpid(), getppid() );
}
else /* parrent */
{
exit(0);
}
}
return 0;
}
The output that I expect is in the form:
Creating 3 children
PARENT PROCESS
My pid is 1234
CHILD My pid is 4567 my parent pid is 1234
CHILD My pid is 3528 my parent pid is 4567
CHILD My pid is 5735 my parent pid is 3528
The output I get in the terminal is
Creating 3 children
PARENT PROCESS
My pid is:564
CHILD My pid is:5036 my parent pid is 564
User#User-PC ~
$ CHILD My pid is:4804 my parent pid is 1
CHILD My pid is:6412 my parent pid is 4804
The problem is that the program doesn't seem to terminate. I should use Ctrl+C to get out of the terminal, which is not normal. Can you help me to fix this issue?
The children die when the parent dies.
In your case the parent exits before all the children have been created.
Try waiting for the children before exiting:
else /* parrent */
{
int returnStatus;
waitpid(pid, &returnStatus, 0); // Parent process waits for child to terminate.
exit(0);
}
try to wait the process with wait(NULL);
pid_t child = fork();
if (child == -1)
{
puts("error");
exit(0);
}
else if (child == 0)
{
// your action
}
else
{
wait(&child);
exit(0);
}
so your father will wait the child process to exit
The proposed cure is correct, but the reason stated is wrong. Children do not die with the parent. The line
CHILD My pid is:4804 my parent pid is 1
clearly indicates that by the time child called getppid() its parent is already dead, and the child has been reparented to init (pid 1).
The real problem is that after the child prints its message, it continues to execute the loop, producing more children, making your program into a fork bomb.
I have read this in books and also in some online forums that child process pid is assigned to its parent. But I have this code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("In Child Process\n");
printf("Child process PID : %d\n",getpid());
printf("Parent Process PID : %d\n",getppid());
}
else
{
printf("In Parent Process\n");
printf("Child PID : %d\n",getpid());
printf("Parent PID : %d\n",getppid());
}
}
It outputs:
In Parent Process
Child PID : 2061
Parent PID : 1830
In Child Process
Child process PID : 2062
Parent Process PID : 1161
But if I write a wait() function in else block, i.e:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("In Child Process\n");
printf("Child process PID : %d\n",getpid());
printf("Parent Process PID : %d\n",getppid());
}
else
{
wait();
printf("In Parent Process\n");
printf("Child PID : %d\n",getpid());
printf("Parent PID : %d\n",getppid());
}
}
It outputs-
In Child Process
Child process PID : 2044
Parent Process PID : 2043
In Parent Process
Child PID : 2043
Parent PID : 1830
I'm not getting why the pid value returned by child process in first code is not the same as parent pid.
While in the second code, it is the same. Can someone please explain the reason for the above problem?
Remember that getpid returns the pid of the current process, and that getppid returns the parent pid of the current process.
So in the second example, when you call getpid in the parent process you get the pid of if itself (the parent process) and getppid gets the pid of the grand-parent.
The child pid is the value returned by fork.
More related to your problem is that you have no control over when a specific process runs in a modern multi-tasking system, which means that the child and parent processes may take turn printing out text. In your case it seems that the child process in the first example doesn't get to run until the parent process has printed its lines.
What the wait function does, is to actually wait until one child process has exited, and so the parent process will block until the child process has exited.
Joachim's answer is excellent. As additional note, you can get the child process' pid in the else branch by printing the result of fork:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("In Child Process\n");
printf("Child process PID : %d\n",getpid());
printf("Parent Process PID : %d\n",getppid());
}
else
{
wait();
printf("In Parent Process\n");
printf("Child PID : %d\n", pid); // fork returns the pid of the child
printf("Parent PID : %d\n", getpid()); // because I'm the parent
}
}
First Example:
fork returns two processes, In your first example after fork the parent starts execution and prints its pid 2061 and parent pid 1830 (of bash) and the parent will terminate without waiting for the child to finish, So when child starts executing it will print its pid 2062 and its parent has already terminated it is printing some other pid.
You please sleep() before printing parent pid when child process is running and on other terminal use command ps -l to see which pid the child is printing as a parent pid.
Second example:
You are using wait in parent process that is parent will wait untill child terminates thus child process is printing the correct parentpid because parent is still not terminated.
I have the following program
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
main()
{
pid_t pid, ppid;
printf("Hello World1\n");
pid=fork();
if(pid==0)
{
printf("I am the child\n");
printf("The PID of child is %d\n",getpid());
printf("The PID of parent of child is %d\n",getppid());
}
else
{
while(1)
{
printf("I am the parent\n");
printf("The PID of parent is %d\n",getpid());
printf("The PID of parent of parent is %d\n",getppid());
}
}
}
The output of this program is parent runs forever without switching to child process. In this case why there is no switch to child process?
Ideally, the parent should wait for the child to finish, so that the child's process entry is properly reaped.
waitpid(pid, 0, 0);
To see if the child ever gets to run with your infinite loop. run the program and grep for child:
./a.out | grep child