Been practicing with those system calls, but I stucked into this code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //first child
printf("\ni'm the first child, my pid is %d", getpid());
fflush(stdout);
break;
default: //parent
sleep(5); /** sleep is generating problems **/
printf("\ni'm the parent process, my pid is %d", getpid());
printf("\ngenerating a new child");
fflush(stdout);
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //second child
printf("\nhere i am, the second child, my pid is %d", getpid());
break;
default: //parent
wait((int *)0);
printf("\nback to parent, my pid is %d", getpid());
}
}
return 0;
}
The output I'm getting is:
i'm the first child, my pid is 6203
i'm the parent process, my pid is 6202
generating a new child
back to parent, my pid is 6202
Process returned 0 (0x0) execution time: 5.004 s
Press ENTER to continue
here i am, the second child, my pid is 6204
What I'm trying it's a simple print of these messages managing the timing via sleep().
I can't understand why the program is returning before printing the second child message.
The default case(the one right after the second fork) got printed before its child(second) acting on the output like he's ignoring its wait(). Its child therefore got printed after the process returns.
I wasn't able to figure out what's the problem. I've marked sleep() function since if I substitute it with wait((int *)0); the processes flux is working how it was designed to (anyhow, without any timing).
At this point I'm not sure anymore about process flux, or the sleep() usage (man pages wasn't that helpful, way too concise to be honest).
Actually, your call to wait works. It detects the end of the first child process and continues afterwards. If you do two consecutive calls to wait(), you will get the proper behaviour.
Updated test code :
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
int status;
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //first child
printf("\ni'm the first child, my pid is %d", getpid());
fflush(stdout);
break;
default: //parent
sleep(5); /** sleep is generating problems **/
printf("\ni'm the parent process, my pid is %d", getpid());
printf("\ngenerating a new child");
fflush(stdout);
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //second child
printf("\nhere i am, the second child, my pid is %d", getpid());
break;
default: //parent
pid = wait(&status);
printf("\nParent detects process %d was done", pid);
pid = wait(&status);
printf("\nParent detects process %d was done", pid);
printf("\nback to parent, my pid is %d", getpid());
}
}
return 0;
}
Output :
i'm the first child, my pid is 30897
i'm the parent process, my pid is 30896
generating a new child
here i am, the second child, my pid is 30940
Parent detects process 30897 was done
Parent detects process 30940 was done
back to parent, my pid is 30896
The man page for wait says the following:
The wait() function shall suspend execution of the calling thread until status information for one of the terminated child processes of the calling process is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. If more than one thread is suspended in wait() or waitpid() awaiting termination of the same process, exactly one thread shall return the process status at the time of the target process termination.
Wait is returning because of the first child.
Related
I am trying to understand how and does waitpid() using WNOHANG actually clean the resources of the child it's supposed to wait for?
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
printf("Hello from start\n");
int pid;
if((pid = fork()) == 0){
printf("Hello from child: %d\n", getpid());
printf("My parent PID is: %d\n", getppid());
}else if(pid > 0){
printf("Hello from parent: %d\n", getpid());
int status;
waitpid(pid, &status, WNOHANG);
}else{
perror("ERROR");
}
printf("Outside if statement, pid is: %d\n", getpid());
}
The output for child process for parent PID is: 1 meaning parent terminated and child was zombie passed to init process.
From what I understood WNOHANG in man description, it's supposed to return whether child process terminated at the time of testing it and let parent process continue on. I however thought that if parent was about to terminate before its child, it would wait for child to "clean it up". From this it seems that the parent just keeps on going and terminates without cleaning up.
What am I missing, and how to let parent continue its work but not let it terminate without waiting for child to terminate first?
Thanks!
I however thought that if parent was about to terminate before its child, it would wait for child to "clean it up".
That is not the case. The parent won't wait. Children of a process that terminates get reparented to process 1, and they will automatically get reaped when they terminate.
how to let parent continue its work but not let it terminate without waiting for child to terminate first?
work();
int status;
waitpid(pid, &status, 0);
According to the man page
getpid() returns the process ID (PID) of the calling process.
In the following code why does the parent pid return a different value than getpid() ?
Isn't the main process the same as the parent process?
And why do I get different outputs when run on a different system?
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{
printf("getpid = %d \n", (int)getpid());
pid_t pid = fork();
printf("fork returned %d \n", (int)pid);
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
printf("I am a child with pid = %d\n", (int)pid);
break;
default:
printf("I am a parent with pid = %d\n", (int)pid);
break;
}
return 0;
}
output when i run :
getpid = 8208
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0
output when run on a different pc:
getpid = 2522
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523
Yes, the parent process and main process are the same thing.
This snippet of your code should give you a clue about the solution:
switch (pid) {
/* ... */
case 0:
printf("I am a child with pid = %d\n", (int)pid);
break;
This effectively says "if pid is zero then the child pid is always zero". This obviously cannot be true, so it is your interpretation of fork()'s return value that is incorrect.
The man page states:
Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.
So the variable pid in the parent process is the pid of the child, NOT the parent's own pid.
In the child process, you would need to call getpid() after fork() to get the child's own pid.
fork duplicates a process and after the fork, they both run in parallel. To tell the program whether it is a child or the parent, it returns 0 for the child and the PID of the child process is returned in the parent.
So after the fork, you must call getpid() for both parent and child, in your code that would be:
pid_t thispid= getpid();
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
printf("I am a child with pid = %d\n", (int)thispid);
break;
default:
printf("I am a parent with pid = %d\n", (int)thispid);
break;
}
I'm totally new to C.
I tried the following code, expecting that the child's ppid would match the parent's pid, but this is not the case.
int main() {
int pid;
printf("I'm process with pid=%d\n", getpid());
switch (pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
printf("I'm the child process: pid=%d, ppid=%d\n", getpid(), getppid());
break;
default:
printf("I'm the parent process: pid=%d, ppid=%d\n", getpid(), getppid());
break;
}
exit(0);
}
> gcc -o fork fork.c
> ./fork
I'm process with pid=16907
I'm the parent process: pid=16907, ppid=6604
I'm the child process: pid=16908, ppid=1 // <-- expected ppid=16907, why 1?
>
What did I do wrong ?
It is likely the parent process has already exited and no longer exists. You could try some delay in the parent.
'init' which is the root process running in a linux system has pid 1 .
When a process's parent gets terminated before itself(i.e. the child) , the child becomes an 'orphan' process and is taken up by the root process or the process just above the hierarchy of the process which created it(parent process) .
Hence , here it is taken up by and executed under init which has pid = 1 .
So, delay your parent process for solution.
Like others have mentioned, looks like the parent process has terminated while the child process is still under execution making it(the child process) an orphan. Adding delay before exiting may work.
But an elegant way of doing it is, the parent process has to wait till the child process terminates.
This can be achieved by calling waitpid() with the pid of the child (the value returned by fork() ). When the control comes out of this function, you can be sure that the child process has terminated. Also, waitpid() returns the status of the process termination. Based on the status that it returns, you can get to know the normal/abnormal child termination.
Here is the code that does it:
int main() {
int pid;
int status = 0;
printf("I'm process with pid=%d\n", getpid());
switch (pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
printf("I'm the child process: pid=%d, ppid=%d\n", getpid(), getppid());
break;
default:
waitpid(pid, &status, 0);
if(WIFEXITED(status)
{
printf("Normal termination of child process %d\n", pid);
}
else
{
printf("Abormal termination of child process %d\n", pid);
}
printf("I'm the parent process: pid=%d, ppid=%d\n", getpid(), getppid());
break;
}
exit(0);
}
Just got confused with parent pid value in child process block. My program is given below:
int main(int argc, char *argv[])
{
pid_t pid;
pid=fork();
if(pid==-1){
perror("fork failure");
exit(EXIT_FAILURE);
}
else if(pid==0){
printf("pid in child=%d and parent=%d\n",getpid(),getppid());
}
else{
printf("pid in parent=%d and childid=%d\n",getpid(),pid);
}
exit(EXIT_SUCCESS);
}
Output:
pid in parent=2642 and childid=2643
pid in child=2643 and parent=1
In "Advanced Unix programming" it says that child process can get parent process id using getppid() function. But here I am getting "1" which is "init" process id.
How can I get the parent pid value in the child process block.. Please help me in getting output.
I executed in "Linux Mint OS" but in "WindRiver" OS I am not getting this problem. Does this program change behaviour according to OS?
That's because the father can / will exit before the son. If a father exists without having requested the return value of it's child, the child will get owned by the process with pid=1. What is on classic UNIX or GNU systems SystemV init.
The solution is to use waitpid() in father:
int main(int argc, char *argv[])
{
pid_t pid;
pid=fork();
if(pid==-1){
perror("fork failure");
exit(EXIT_FAILURE);
}
else if(pid==0){
printf("pid in child=%d and parent=%d\n",getpid(),getppid());
}
else{
printf("pid in parent=%d and childid=%d\n",getpid(),pid);
}
int status = -1;
waitpid(pid, &status, WEXITED);
printf("The child exited with return code %d\n", status);
exit(EXIT_SUCCESS);
}
After the fork you have two new processes and you can know the child id in the parent but not the other way round. If you really need this you would have to open a pipe (popen) before the fork and then the parent could write this into the pipe and the child could read it.
Once the parent completes it execution and child is still running. Then child is known as orphan (as it's parent died) and it is adopted by init process if you are login by root ( whose pid =1 ).
If you want child to exit first before parent then use wait() system call and its variants.
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid,pid2;
pid=fork();
if (pid<0) {
printf("fork failed");
exit(-1);
} else if (pid==0) {
printf("child id is%d",getpid());
execlp("/bin/ls","is",NULL);
printf("\nsleeping for 2 seconds using pid of child class");
sleep(2);
printf("killing the child process");
kill(getpid());
} else {
wait(NULL);
printf("The parent id is %d\n",getpid());
printf("The child id is %d\n",getpid());
printf("\nsleeping for 3 seconds without pid");
sleep(3);
printf("\nchild completed\n");
exit(0);
}
}
It is simply, because the parent process no longer exists. If you call the wait() system function, then it will exist until the child finishes its work and you will get the parent PID.
I want to create a process B from process A. However, I don't want B to be the child of A, which will be the case if I simply use fork. How can I achieve that? In other words I want process B to keep on executing even if process A is killed.
You can use the setsid() function.
Alternatively, as you have tagged your question "linux", maybe you want to use daemon() instead of fork() + setsid().
Why do you think that B would not keep executing after A is killed if B is a child of A? That's not true.
But if you still want B to not be a child of A then you can do this by fork()ing twice: once to create a child A½ and once to create B. B is a child of A½ and a grandchild of A. Then have A½ exit immediately. B will be inherited by init and will have no further relationship to A.
If you are concerned about signals like SIGHUP and SIGINT that are generated in response to events like a Control-C keypress that are broadcast to the entire foreground process group, see cnicutar's answer.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
switch(pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
printf(" CHILD: This is the child process!\n");
printf(" CHILD: My PID is %d\n", getpid());
printf(" CHILD: My parent's PID is %d\n", getppid());
/* you can exec another program here if you wish to */
printf(" CHILD: I'm outta here!\n");
break;
default:
printf("PARENT: This is the parent process!\n");
printf("PARENT: My PID is %d\n", getpid());
printf("PARENT: My child's PID is %d\n", pid);
printf("PARENT: I'm not going to wait for my child to exit\n");
signal(SIGCHLD, SIG_IGN);
printf("PARENT: I'm outta here!\n");
}
return 0;
}
If you don't send the signal(), then once after the parents completes the execution (and exits), the child process will be termed as a zombie process. For better understanding, execute the following program.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
/*
Execute this program and do 'ps ax | grep Z' to see that this
is put in a defunct state or zombie state
*/
int main()
{
pid_t child_pid;
child_pid = fork();
if (child_pid > 0) {
sleep(60);
} else {
return 0;
}
return 0;
}
The only way I see is having the child orphan (then adopted by init).
This may be achieved by terminating the father process before the child (but just take care of signal propagation).
Some nice samples may be found here