0

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?

Krishna
  • 484
  • 7
  • 22

1 Answers1

4

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    For future reference if anyone is wondering where this information can be found there is an entry in [`man exec`](https://linux.die.net/man/3/exec) --- `"The exec() functions only return if an error has occurred. The return value is -1, and errno is set to indicate the error."` – Puddler Mar 07 '17 at 06:08
  • 1
    And `setjmp()` can return more than once if the `jmp_buf` it saves is `longjmp`'d too. – Jonathan Leffler Mar 07 '17 at 06:10
  • 1
    Nitpick: `setjmp` / `longjmp` are C standard library functions, not system calls. (They don't require kernel assistance.) –  Mar 07 '17 at 06:41
  • @paxdiablo, Strictly speaking, `exit(3)` is not a system call, but a library function, as it has to deal with the `atexit(3)` installed callbacks, after which, it should call `_exit(2)`. The same thing applies to `abort(3)` which normally translates into a call to `kill(2)`. But are good examples anyway. – Luis Colorado Mar 09 '17 at 06:14