#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
printf("pid : %d\n", getpid());
if( pid == 0)
{
printf("child: pid : %d \n", getpid());
while(1);
}
else
{
printf("parent: pid : %d \n", getpid());
//while(1);
}
}
In the above code snippet inside if statement if we put while(1), it doesn't remains blocked and when enter key is pressed program is exited, but in case of parent if we put while(1), parent remains blocked until we give ctrl+c. Please clarify this behaviour of child.
In the above code snippet inside if statement if we put while(1), it doesn't remains blocked
The child process doesn't exit actually; it just becomes an orphan process because its parent exits. The orphaned chuld process will be adopted by the init process of your system. You can see it via ps command.
But if you put the while(1); in the parent process it remains blocked.
Basically whichever process has while(1); infinite loop, it's still running. When parent exits you get the prompt back and the child becomes orphan. But the child process is still running.
In general, you need to wait(2) for the child process in the parent process to reap child processes.
Related
I am trying to understand the re-parenting process in linux. So when the parent process exits abruptly the child process is adopted by the init process. This is re-parenting.
I am not able to understand the below code that shows how re-parenting works.
First the process is forked and the child process runs.
The first "printf" statement is executed.
Then sleep() is called. Why is "terminated" printed after this?
Is the parent process ever called?
Please somebody explain me how does this work?
#include <stdio.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
// child process
printf("My parent's PID is %d\n", getppid());
sleep(2);
printf("My parent's PID is %d\n", getppid());
} else {
// parent process
sleep(1);
}
// both processes
printf("Terminated\n");
}
The output:
My parent's PID is 18892
Terminated
shell $ My parent's PID is 1
Terminated
I'm working on c program which uses fork() and wait() calls, firstly I created five child processes and then I called wait() for five times. Whenever I execute the program it prints the same child process id in the second for loop which is displayed from the first for loop. The child processes were never getting exited before the wait() function is getting called. Why is this happening? Why the cpid is always printing the exact child process id which were displayed before?
code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main( )
{
int child_pids[5];
int i;
printf("PARENT ID: %d \n", getpid());
for(i=0;i<5;i++) {
if(fork()==0) {
printf("child(pid): %d of parent(pid): %d \n",getpid(),getppid());
exit(0);
}
}
for(i=0;i<5;i++) {
int cpid=wait(NULL);
printf("parent (pid): %d waited for child(pid): %d \n",getpid(),cpid);
}
return 0;
}
If there are any mistakes in my way of asking questions, please comment below
Upon exit, the child leaves an exit status that should be returned to the parent. So, when the child finishes it becomes a zombie.
Whenever the child exits or stops, the parent is sent a SIGCHLD signal.
The parent can use the system call wait() or waitpid() along with the macros WIFEXITED and WEXITSTATUS with it to learn about the status of its stopped child.
If the parent exits, than you can see your children still as zombie processes (unwaited children ).
wait() just tells you which child exited so you can get the exit code. If you have more children running, then of course, others could have terminated in the meantime as well.
If you don't care about the exit status, then wait() is just fine, but you still have to wait on all children you started.
Why doesn't the program end? The child hangs after printing what it has to print. If the parent process slept instead of the child, It would have worked, but why is that? I have also tried calling exit at the end of each process, but with the same result. Do I always have to wait for the child to finish?
int main(){
int pid = fork();
char s[100] = "Hello";
if(pid > 0){
printf("FIRST PRINT IN PARENT: %s\n", s);
strcat(s, " - PARENT");
printf("SECOND PRINT IN PARENT: %s\n", s);
}
else if(pid == 0){
printf("IMMEDIATELY IN CHILD: %s\n", s);
sleep(2);
printf("AFTER 2 SCONDS IN CHILD: %s\n", s);
}
return 0;
}
When the parent exits it might send a signal (SIGHUP) to the child.
If it does, and if the child doesn't catch that signal, the child dies.
Historically, the default has been for a process to send SIGHUP to it's children when it exits. Nowadays, many Linux distributions don't send SIGHUP by default.
I tried your code on RHEL and the child process wasn't killed.
So the parent dies and control returns to the shell. The child continues and prints it's second output 2 seconds later.
If the child does receive a SIGHUP it won't hang. It dies, and the final string is never printed.
In Linux, you can turn on SIGHUP via the prctl system call:
#include <sys/prctl.h>
prctl(PR_SET_PDEATHSIG, SIGHUP);
Related question: how-to-make-child-process-die-after-parent-exits
The shell does give you the prompt back once its child process (i.e. the parent process in your code) exits. However,
it doesn't know about the child process your code started.
The source of problems you have observed is that your parent process doens't wait for its child.
Use wait(2) system call, such as wait(0);, in the parent process.
The general risk of not waiting for child process(es) is that you might end up with zombie processes; conversely, orphan processes (if you parent process exits first).
It seems that if I create a process, fork it and send a SIGHUP from the parent to the child, the child dies but it's "/proc/PID" dir doesn't dissappear until the parent also dies.
(See code below).
What is the right way to let the parent check if the child is dead ?
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
void testprocdir(pid_t pid) {
struct stat sb;
char path[1024];
sprintf(path,"/proc/%d",pid);
if(stat(path, &sb)==-1 && errno == ENOENT) {
printf("%s does not exist\n", path);
} else {
printf("%s exists\n", path);
}
}
int main(int argc,char **argv) {
pid_t parent,child;
parent=getpid();
printf("I am %d\n",parent);
child=fork();
switch(child) {
case -1:
printf("Forking failed\n");
return 2;
case 0:
parent=getppid();
child=getpid();
printf("I am the child (%d) and my parent is %d\n", child, parent);
while(1) { sleep(1); printf("I am the child and I have slept 1s\n");}
printf("This line should not be visible\n");
}
sleep(1); //make sure kid is in the while loop
printf("I am the parent (%d) and my kid is %d\n", parent, child);
kill(child,SIGHUP);
testprocdir(parent);
printf("Waiting 5s before testing if the procdir of the child (/proc/%d) is removed\n",child);
sleep(5);
testprocdir(child);
return 0;
}
You could use the wait family of system-calls.
fork returns the PID of the child process in the parent process, and 0 in the child process.
man waitpid should provide more than enough direction beyond that to call waitpid in the parent, allowing you to check that child process or all child processes ― including the ability to allow the parent to continue executing if the child is still alive or stop all execution in the parent until the child is dead.
I will start with some concepts:
The OS will keep a child process' entry in the process table (including exit status) around until the parent calls waitpid (or another wait-family function) or until the parent exits (at which point the status is collected by the init process). This is what a "zombie" process is: a process that has exited by is still resident in the process table for exactly this purpose. The process' entry in the table should go away after the first call to waitpid.
Also, from the man page :
A child that terminates, but has not been waited for becomes a "zombie". The kernel maintains a minimal set of information about the zombie process (PID, termination status, resource usage information) in order to allow the parent to later perform a wait to obtain information about the child.
So, by using the wait family of functions you can examine the status of child process.
There are some macros also that can be used with with wait family of functions to examine the status of child process like WEXITSTATUS, WIFSIGNALED, WIFEXITED etc .
I'm writing a multi process program in C.
I hope parents process can wait all child processes finish then exit when
it receives SIGINT.
I have two questions.
How can parents record each pid of child process it forked.
Child process may finish before recording function run on
main process.
If parents has no idea about how many child processes it has. How can he wait
all child process finish.
Thanks in advance.
You record the pid of child processes as you fork them (if required).
call waitpid in a loop with pid = 0 it will either return a pid of a process that exited or return -1 and if errno = ECHILD you have no slaves left.
Keep calling wait(2) in a loop. Every time wait() returns, you'll get back the PID of the exited child and its status. The status will tell you, whether it exited normally (with an exit code) or due to a signal. Something like this (not tested):
#include <sys/types.h>
#include <sys/wait.h>
...
pid_t pid;
int status;
...
while ((pid = wait(&status)) > 0) {
printf("Child %lu ", (unsigned long)pid);
if (WIFEXITED(status))
printf("exited with status %d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("killed by signal %d\n", WTERMSIG(status));
else if (WIFSTOPPED(status))
printf("stopped by signal %d\n", WSTOPSIG(status));
else if (WIFCONTINUED(status))
printf("resumed\n");
else
warnx("wait(2) returned for no discernible reason");
}