Search code examples
cunixfork

Who executes first after fork(): parent or the child?


I know that it can be either of these. But I always see that the child executes first on my UNIX terminal. Also, why don't the parent and child execute in parallel. They seem to be executing serially. Is this because they share the same terminal?


Solution

  • In general, nothing can be said about the relative order of their execution.

    Now, let's consider your specific problem. If:

    1. both processes take a non-trivial amount of time to run, and
    2. you're saying that one runs to completion before the other makes any progress, and
    3. there are unused CPU cycles, and
    4. this happens every time you run the application.

    Most likely this indicates that there is some (perhaps unintended) synchronization going on between the two processes.