简体   繁体   中英

How does abort function never return?

I was reading the man page for abort() system call and I came across this.

RETURN VALUE

The abort() function never returns.

I was wondering how this is possible?

Calling the abort function results in the program being terminated.

Therefore, abort does not return.

A number of C library functions never return to their caller:

  • exit() terminates the program, providing an exit status.

  • abort() terminates the program with an error message.

  • longjmp() transfers control back to the point saved by the corresponding setjmp()

The assembly code either branches directly to an address different from the return address or makes a system call that terminates the program.

A function that "never returns" must unconditionally do one of these five things:

  1. Enter an infinite loop ( for(;;); )
  2. Perform a "nonlocal transfer of control" such that some other function appears to return instead ( longjmp , swapcontext )
  3. Invoke a "system call" that causes the operating system to terminate at least the current thread of execution ( pthread_exit , _exit , reboot )
  4. Execute some kind of forbidden or invalid machine operation (invalid instruction, access to unmapped memory, etc), again causing the OS to terminate the process
  5. Call some other function that does one of the above things.

abort is usually some combination of 5->3, 4, and as a last resort 1, because its contract is to terminate the process after things have already gone horribly wrong: it's not out of the question that the first thing it tries (usually kill(getpid(), SIGABRT) ) won't work.

At a lower level, we could talk about what a "thread of execution" actually is, and how the operating system sets them up and tears them down, and how the OS itself is a program invoked by the bootstrap loader, and if you squint at it the right way, "running a program" is just dynamically modifying the OS's code to include the code for the program and then jumping to it... but maybe you don't want me to reel off an entire operating systems course in this answer box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM