I m writing a simple web server. The program in simplified form is as follows
while(1)
{
// accepting the connection.
accept();
pid = fork();
if(pid == 0)
{
// performing some operations
_exit(EXIT_SUCCESS);
} else {
sleep(1);
}
}
So once the child process executes it should terminate, and the parent process should continue accepting the connection. But, for me the child process is not getting terminated and even it(child) is also accepting the connections. Am I doing any mistake here.
I am able to see the process (child) not being killed using.
top -U <username>
I need help on this. Thanks in advance. 🙂
The parent process has to call
waitto “reap” the child process.The reasons you need to “wait” for the children is because there are still resources left after a process exits, for the exit code of the child process. What
wait(and its sibling system calls) are doing is not only waiting for child processes to end, but also get the exit code so the child process can be cleaned up properly by the operating system. If the parent process doesn’t wait for all child processes to exit before itself exits, then all child processes becomes orphaned, and the process with process id1is set as their parent.