2

Which binary or piece of code is printing Segmentation fault on the console when an error occurs?

JB.
  • 40,344
  • 12
  • 79
  • 106
Nelstaar
  • 769
  • 9
  • 26
  • 3
    Pretty sure it's the shell. It gets notified that its child process exited with `SIGSEGV` and prints the message. – Colonel Thirty Two Sep 14 '15 at 12:24
  • I am pretty shure that the shell gets exactly no information what was happened during execution of a subprocess. The only interface is the return value... – Klaus Sep 14 '15 at 12:29
  • 1
    @Klaus: That's more or less right, but in the case of a segmentation fault, the return value will indicate that the process was terminated by a SIGSEGV signal - so the shell is able to (and does) print this message :) – psmears Sep 14 '15 at 12:36
  • Can you tell me which return value has the meaning of SIGSEGV? – Klaus Sep 14 '15 at 12:38
  • 1
    @Klaus, `128 + `. `SIGSEGV` is usually `11`, so `139`. – Frédéric Hamidi Sep 14 '15 at 12:39
  • So if i write a main() which simply returns this value should result in printing the message? I believe this will not work. – Klaus Sep 14 '15 at 12:46
  • 1
  • 1
    @Klaus, that won't work indeed, because returning a status code from `main()` is (roughly) an implicit call to `exit()`, and `exit()` [causes *normal* process termination](http://linux.die.net/man/3/exit). See [Are there any standard exit status codes in Linux?](http://stackoverflow.com/q/1101957/464709) for more information. – Frédéric Hamidi Sep 14 '15 at 12:51
  • 1
    @FrédéricHamidi: for ksh93 it apparently is `256 + `: http://unix.stackexchange.com/a/99134/1987 – ninjalj Sep 14 '15 at 19:13

1 Answers1

1

That's the invoking shell's job, when job control is enabled. Demonstration with bash:

$ set +m                    # disable job control
$ bash -c 'kill -SEGV $$'
$ set -m                    # enable job control
$ bash -c 'kill -SEGV $$'
Segmentation fault (core dumped)
JB.
  • 40,344
  • 12
  • 79
  • 106
  • In a more general form it's the parent job ? I've done a small binary that forks and the parent wait() returns 139 (segfault). – Nelstaar Sep 14 '15 at 13:19
  • 1
    You're correct in your representation of it. :-) But careful with the words, in this context "job" is a specific term that applies to the child process. The parent *process*, which is a shell, is the one responsible for tracking its child processes, that it calls jobs (hence the phrase "job control"). It is that parent shell that notifies when they terminate abnormally, via a signal. – JB. Sep 14 '15 at 13:23
  • It's parent's duty. :-) – Nelstaar Sep 14 '15 at 13:39