I'm working on some stuff using fork()
in C. This is my first contact with the concept of forking processes.
Basically, I have something like this:
int pid;
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork Failed");
exit(-1);
} else if (pid == 0) {
fprintf(stderr, "Inside child %d\n", getpid());
// do some other stuff
exit(0);
} else {
fprintf(stderr, "Inside parent %d\n", getpid());
}
Before, I hadn't put the exit(0)
in the child process' code. I was getting seemingly tons of duplicate processes. I added the exit(0)
and now I'm only spawning one child. However, I want to know if this is proper practise or just a bandaid. Is this the correct thing to do. How should a child "stop" when its done?