Search code examples
cunixforkexecmanpage

In terms of call-return behaviour, how are the fork() and exec() system calls different from other system calls?


Fork returns twice-

  • once in parent

  • once in child

But, how is exec() different from other system calls in terms of call and return behaviour?


Solution

  • Actually, there are a few which don't obey the "returns once" paradigm.

    A call to fork() returns once or twice - the latter on success where it returns once in the parent and once in the child, the former on failure where it simply returns once in the parent.

    A call to exec() will return on failure but, if successful, the current process is simply overwritten with a new program.

    There are others, such as exit() or abort(), which are not expected to return at all.