为什么使用exit()被认为是不好的? [英] Why is using exit() considered bad?

查看:168
本文介绍了为什么使用exit()被认为是不好的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这个问题,并且有一个答案解释了为什么使用 exit()很糟糕,因为:

I'm reading this question and there is an answer that explains why using exit() is bad because:


  • 您最终将拥有多个退出点从程序

  • 它使代码更复杂(例如使用goto)

  • 它无法释放运行时分配的内存

我应该弄清楚我使用的是Qt,所以代码已经有点费解了,因为我利用了信号和插槽。就是说,对于问题1,我认为它与问题2有关,但是我的代码当前尝试避免使用 exit(),因为有人告诉我它将使我代码看起来像是一团糟,但是避免 exit 会使事情变得一团糟。我的函数不需要返回任何东西,也不需要返回东西。例如,当我让用户注册并且他们的用户名已经存在时,而不是在告诉用户注册失败之后不只是调用 exit()(在这种情况下,这是理想的行为) )我将 false 返回到一个函数,然后将 false 返回到另一个函数,然后将其返回 false 到我的main中,然后检查该函数返回的是true还是false,如果返回false则返回0。这对于避免 exit()使代码整洁。

I should clarify that I'm using Qt, so the code is already a bit "convoluted" since I'm taking advantage of signals and slots. That being said, for issue #1, I see it's related to #2, but my code currently attempts to avoid usage of exit() because I was told it would make my code look like a mess, but avoiding exit has made it a mess. I have functions that don't have to return anything, returning things. For example, when I have users register and their username already exists, instead of just calling exit() after telling the user registration has failed (which is the desired behavior in this situation) I return false to a function which then returns false to another function which then returns false to my main which then checks if that function returned true or false, and if it returns false then it returns 0. So much for avoiding exit() making the code clean.

对于第三个问题,不使用 exit(0)告诉操作系统该程序已运行完毕,并且操作系统将自行释放该内存?我运行了一个使用 exit(0)的测试用例,当我按下一个按钮并将该进程从进程列表中删除并释放了内存时,为什么这甚至是关心?看来这是一个完全错误的声明,至少在Windows上是如此。

For the third issue, doesn't using exit(0) tell the OS that the program is done running and the OS will free that memory by itself anyway? I ran a test case that uses exit(0) when I press a button and the process is removed from the process list and the memory is freed, so why is this even a concern? It seems it's an outright false statement, at least on Windows.

推荐答案

只需盲目调用 exit() 在程序中的某个位置被认为是不好的,原因很简单:

Just blindly calling exit() somewhere in your program is considered bad for a simple reason:

它无法正确关闭其他线程(它们只是被终止了),所以确实没有正确刷新所有缓冲区(刷新了stdio文件),并不能保证永久/共享资源(文件/共享内存/其他通信方式)的一致性和有效状态。

It does not properly shutdown other threads (they just get terminated), it does not properly flush all buffers (stdio files are flushed) and guarantee a consistent and valid state of permanent/shared resources (files/shared memory/other ways to communicate).

不过,如果您可以保证没有任何正在运行的线程可能会干扰(通过持有一个锁等杀死),并且所有需要该线程的缓冲区将由 exit(),这是实现更快关机的一种有效方法。

Still, if you can guarantee that no thread is running which might interfere (by being killed holding a lock or such), and all buffers which need it will be flushed by exit(), that's a valid way to achieve a faster shutdown.

许多现代软件都被编程为更快关机:

Much modern software is programmed for even faster shutdown:

它是耐崩溃的,几乎每次都使用例如 _Exit()(甚至不注册 atexit at_quick_exit 已注册)钩子)可以。在大多数情况下,这比有序关闭的速度要快得多(如果可能,应首先销毁Windows用户界面资源,因为它们是一个例外)。

It is crash-tolerant, in that at nearly every time, just shutting down using e.g. _Exit() (not even calling atexit or at_quick_exit registered hooks) is ok. That is vastly faster than an ordered shutdown in most cases (Windows user interface resources should be destroyed first if possible, because they are an exception).

更多信息:< a href = http://radlab.cs.berkeley.edu/people/fox/static/pubs/pdf/c22.pdf rel = noreferrer>仅限崩溃的软件(PDF!)


仅限崩溃的程序会安全崩溃并迅速恢复。
只有一种方法可以停止这种软件-使
崩溃-并且只有一种方法可以启动恢复-通过启动恢复。
仅崩溃的系统由仅崩溃的组件
构建,并且使用透明的组件级重试
对最终用户隐藏系统内组件的崩溃。在本文的
中,我们提倡Internet系统仅崩溃的设计,
表明它可以导致更可靠,可预测的
代码和更快,更有效的恢复。我们将介绍
关于如何构建此类仅崩溃的Internet服务的方法,将
成功的技术推向逻辑极限。

Crash-only programs crash safely and recover quickly. There is only one way to stop such software - by crashing it - and only one way to bring it up - by initiating recovery. Crash-only systems are built from crash-only components, and the use of transparent component-level retries hides intra-system component crashes from end users. In this paper we advocate a crash-only design for Internet systems, showing that it can lead to more reliable, predictable code and faster, more effective recovery. We present ideas on how to build such crash-only Internet services, taking successful techniques to their logical extreme.

这篇关于为什么使用exit()被认为是不好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆