Comparisons and explanations · Intermediate

fork versus exec

Follow process creation and program replacement, including return values, inherited descriptors, failed exec and child reaping.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Creation and replacement are different operations
  2. Fork returns different values in parent and child
  3. Successful exec does not resume the old code
  4. Memory and descriptors have different inheritance
  5. Reap children and respect multithreaded restrictions
  6. Ask Logeia about this articleCheck your understanding

Creation and replacement are different operations

Fork creates a child process from the calling process. The exec family replaces the program image executed by the current process. Exec does not itself add another child. A shell launching an external command provides a familiar example of combining them: create a child and have that child replace its image. Real implementations may use another interface such as posix_spawn, so this is a conceptual launch sequence rather than a claim about every shell. Separate the everyday notion of a new command from the operating system’s process identity and program image. That distinction clarifies why creation and replacement have different return behavior, resource inheritance and failure handling.

Aspectforkexec
Main roleCreate a childReplace current program image
Successful returnDifferent values in parent and childDoes not normally resume old call
PIDNew child PIDNormally retains current PID

Fork returns different values in parent and child

After successful fork, both parent and child continue after the call. The parent receives the child’s PID, while the child receives zero, allowing their code paths to diverge. Failure returns minus one to the caller and creates no child. The two branches are not consecutive branches inside one process, and their execution order is not established by the return values. Synchronization is needed if an output order matters. Library buffering can also complicate examples because pending user-space buffer contents can be duplicated. Distinguish bytes already written to a file descriptor from data still buffered by a library before interpreting repeated output as evidence about the number or order of operating-system writes.

Successful exec does not resume the old code

On successful exec, the previous program image, including its code and stack, is replaced. Execution does not ordinarily resume at the next line of the old calling function. The process normally retains its PID. An exec failure, such as a missing executable or denied execution, instead returns an error to the old code. A launcher that ignores this path can accidentally let the child continue into parent-oriented application logic. Give failure an explicit reporting and termination path. Also distinguish failure to replace the image from a successfully launched program that later exits with a nonzero status. They are different events and may need different reporting to the parent or user.

Memory and descriptors have different inheritance

Ordinary private memory after fork preserves independent values, potentially using COW internally. Inherited file descriptors instead can refer to the same open file description, sharing file position and some state. Exec closes descriptors marked close-on-exec and performs other documented transformations. Passing an unintended file or socket into a new program can extend a resource lifetime or cross an authority boundary. Specify which resources the child needs and which should survive replacement. The fact that a descriptor has the same integer value in both processes does not establish independent state. Likewise, private-memory separation does not prove that descriptor-associated offsets or locks have been independently duplicated.

Reap children and respect multithreaded restrictions

A parent needs to collect its child’s termination status through an appropriate wait interface such as waitpid. Successful exec and successful completion of the replacement program are separate outcomes. Forking a multithreaded parent introduces another restriction: only the calling thread continues in the child, while copied state can include locks held by vanished threads. Operations safely available before exec are constrained, so arbitrary logging or memory-management activity is not automatically safe. Consider supported launch interfaces such as posix_spawn where appropriate. A small single-threaded demonstration explains return paths and identity; it is not a complete production launcher or a proof that the same sequence is safe in an already multithreaded service.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 5

Choose a learning path