3

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?

2 Answers2

5

Usually the child either has it's own code with an exit or calls one of the exec functions to replace its process's image with another program. So the exit is okay. But the parent and child could execute at least some of the same code, something like this:

int pid = fork();
if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    exit(-1);
} else if (pid == 0) {
    // child code
} else {
   // parent code
}
// shared code
ooga
  • 15,423
  • 2
  • 20
  • 21
  • Alright, so this is an extension of my question. When the child finishes it's code and moves on to the shared code, does it then exit that function and keep running the rest of the program? Let's say the code you wrote above was in `makeAFork()`, would the child, after running the shared code, return from `makeAFork()` and keep executing stuff? –  Feb 01 '14 at 16:04
  • 2
    The child keeps going wherever the code leads. Remember that the child is a separate (almost) identical copy of the parent. The return value of fork() is all that really distinguishes them (they also have different pid's of course, and there's a few other details that can differ). – ooga Feb 01 '14 at 16:13
1

Well if you want that parent process works only after child process finishes then you can use wait function.Here is the example:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h> //fork() is defined in this header file
#include<sys/wait.h>//wait() is defined in this header file
int main()
{
    int pid,a,b;
    printf("\nPlease enter two numbers\n");
    scanf("%d%d",&a,&b);
    pid=fork();
    if(pid<0)
    {
        printf("\nfork failed\n");
        exit(1);
    }
    if(pid==0)
    {
        //you are in chiled process
        //getting child process id
        printf("\n[child %d]: sum of %d and %d is %d\n",getpid(),a,b,a+b);
    }
    else
    {
        //waiting for child process to finish
        wait(NULL);
        //getting parent id
        printf("\n[parent %d]:difference of %d and %d is %d\n",pa_pid,a,b,a-b);
        exit(0);
    }
}
Prashant Anuragi
  • 390
  • 4
  • 14
  • Note that `fork()` is defined in ``, not in ``. Also note that on modern versions of POSIX-compliant systems (those created in the current millennium, not the last millennium — speaking roughly), you do not need to explicitly include ``. No harm is done, but it is not necessary. The other POSIX headers either include it or include other headers that define the relevant types that `` must define. It is a sign of 'old school' coding. There may be reasons to include it, such as corporate coding standards or legacy systems to be supported, but… – Jonathan Leffler Jul 19 '15 at 19:20
  • Your child process code uses an implicit `return 0;` at the end of `main()`; your parent process uses an explicit `exit(0);` to get out of `main()`. Were it my code, there'd be an explicit `return 0;` at the end of `main()`, and no direct `exit(0)` for the parent thread. The child code would be in its own function which would probably end with an explicit `exit(0)`; I might even decorate the child's function with the C11 [`_Noreturn`](http://stackoverflow.com/questions/28552467/) to show that it does not return. – Jonathan Leffler Jul 19 '15 at 19:27