简体   繁体   English

中止功能如何永不返回?

[英]How does abort function never return?

I was reading the man page for abort() system call and I came across this. 我在阅读abort()系统调用的手册页,就遇到了这个问题。

RETURN VALUE 返回值

The abort() function never returns. abort()函数从不返回。

I was wondering how this is possible? 我想知道这怎么可能?

Calling the abort function results in the program being terminated. 调用abort功能导致程序终止。

Therefore, abort does not return. 因此, abort不会返回。

A number of C library functions never return to their caller: 许多C库函数从不返回调用者:

  • exit() terminates the program, providing an exit status. exit()终止程序,提供退出状态。

  • abort() terminates the program with an error message. abort()使用错误消息终止程序。

  • longjmp() transfers control back to the point saved by the corresponding setjmp() longjmp()将控制权转移回由相应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(;;); ) 输入无限循环( for(;;);
  2. Perform a "nonlocal transfer of control" such that some other function appears to return instead ( longjmp , swapcontext ) 执行“非本地控制权转移”,以使其他一些功能似乎返回( longjmpswapcontext
  3. Invoke a "system call" that causes the operating system to terminate at least the current thread of execution ( pthread_exit , _exit , reboot ) 调用“系统调用”,使操作系统至少终止当前的执行线程( pthread_exit_exitreboot
  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 执行某种禁止或无效的机器操作(无效指令,对未映射的内存的访问等),再次导致OS终止进程
  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. 通常, abort是5-> 3、4和最后一个选择1的某种组合,因为它的合同是发生严重错误之后终止该过程:尝试第一件事并不是没有问题的(通常是kill(getpid(), SIGABRT) )不起作用。

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. 在较低的级别,我们可以讨论什么实际上是“执行线程”,操作系统如何设置它们并将其拆除,以及操作系统本身是如何由引导加载程序调用的程序,以及是否斜视按照正确的方式,“运行程序”只是动态地修改OS的代码以包含该程序的代码,然后跳转到该代码……但是也许您不希望我在Windows 2000中放弃整个操作系统课程。这个答案框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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