- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个程序可以创建许多线程并运行直到嵌入式计算机关闭电源,或者用户使用 kill
或 ctrlc 终止进程。
这是一些代码以及 main() 的外观。
static int terminate = 0; // does this need to be volatile?
static void sighandler(int signum) { terminate = 1; }
int main() {
signal(SIGINT, sighandler);
// ...
// create objects, spawn threads + allocate dynamic memory
// ...
while (!terminate) sleep(2);
// ...
// clean up memory, close threads, etc.
// ...
signal(SIGINT, SIG_DFL); // is this necessary?
}
我想知道一些事情:
是否需要任何信号处理?
我在这个线程中读到 "Linux C catching kill signal for graceful termination" ,显然操作系统将为我处理清理工作。因此,我可以只用一个无限循环替换信号处理程序,让操作系统优雅地退出线程、取消分配内存等吗?
关于干净终止,我是否需要关注其他任何信号?本帖"How does SIGINT relate to the other termination signals?" ,对于列出我可能关心的所有信号很有用,但实际需要处理的信号有多少?
我的示例中的 terminate 变量是否必须是 volatile 的?我见过很多例子,这个变量是 volatile 的,还有一些不是。
我了解到 signal()
现在已弃用,可以使用 sigaction()
。是否有任何非常好的示例来展示如何从之前的 signal()
调用进行转换?我对必须创建/传递的新结构以及它们如何组合在一起遇到了问题。
是否需要第二次调用 signal()
?sigaction()
是否有类似的事情需要注意?
明确地说,我要完成的所有工作就是让我的主循环运行,直到 ctrlc 或电源断开或发生非常糟糕的事情。
最佳答案
[Q-3] Does the
terminate
variable in my example have to bevolatile
? I've seen many examples where this variable is volatile, and others where it is not.
terminate
标志应该是 volatile sig_atomic_t
:
因为处理函数可以异步调用。也就是说,处理程序可能会在程序中的任何位置被调用,这是不可预测的。如果两个信号在很短的时间间隔内到达,一个处理程序可以在另一个处理程序内运行。声明 volatile sig_atomic_t
被认为是更好的做法,这种类型总是以原子方式访问,避免中断访问变量的不确定性。 volatile
告诉编译器不要优化并将其放入寄存器。 (阅读:Atomic Data Access and Signal Handling 了解详情)。
更多引用:24.4.7 Atomic Data Access and Signal Handling .此外,7.14.1.1-5 中的 C11 标准指出,只有 volatile sig_atomic_t
的对象可以从信号处理程序中访问(访问其他对象具有未定义的行为)。
[Q-4] I've read that
signal()
is now deprecated, and to usesigaction()
. Are there any really good examples to show how to convert from the previoussignal()
call? I'm having trouble with the new structure that I have to create/pass and how it all fits together.
下面的示例(以及评论中的链接)可能会有所帮助:
// 1. Prepare struct
struct sigaction sa;
sa.sa_handler = sighandler;
// 2. To restart functions if interrupted by handler (as handlers called asynchronously)
sa.sa_flags = SA_RESTART;
// 3. Set zero
sigemptyset(&sa.sa_mask);
/* 3b.
// uncomment if you wants to block
// some signals while one is executing.
sigaddset( &sa.sa_mask, SIGINT );
*/
// 4. Register signals
sigaction( SIGINT, &sa, NULL );
引用:
sigaction()
很好地解释了。 [Q-5] Is the second call to
signal()
necessary? Is there something similar that I need to be concerned with forsigaction()
?
我不清楚为什么在程序终止之前将其设置为默认操作。我想下面这段话会给你答案:
Handling Signals
The call to signal establishes signal handling for only one occurrence of a signal. Before the signal-handling function is called, the library resets the signal so that the default action is performed if the same signal occurs again. Resetting signal handling helps to prevent an infinite loop if, for example, an action performed in the signal handler raises the same signal again. If you want your handler to be used for a signal each time it occurs, you must call signal within the handler to reinstate it. You should be cautious in reinstating signal handling. For example, if you continually reinstate
SIGINT
handling, you may lose the ability to interrupt and terminate your program.
signal()
函数仅定义下一个接收到的信号的处理程序,在此之后恢复默认处理程序。因此,如果程序需要继续使用非默认处理程序处理信号,则信号处理程序必须调用 signal()
。
阅读讨论以供进一步引用:When to re-enable signal handlers .
[Q-1a] Is any signal handling necessary?
是的,Linux 会为您进行清理。例如,如果您不关闭文件或套接字,Linux 将在进程终止后进行清理。但是 Linux 可能不需要立即执行清理,并且可能需要一些时间(可能是为了保持系统性能较高或其他一些问题)。例如,如果您不关闭 tcp-socket 并且程序终止,内核不会立即关闭套接字以确保所有数据都已传输,如果可能,TCP 会保证交付。
[Q-1b] Therefore, can I just replace the signal handler with just an infinite loop and let the OS gracefully exit the threads, de-allocate the memory, etc?
不,操作系统仅在程序终止后执行清理。在进程执行时,分配给该进程的资源不会被操作系统占用。 (操作系统无法知道您的进程是否处于无限循环 - this is an unsolvable problem )。如果您希望在进程终止后操作系统为您执行清理操作,那么您不需要处理信号(即使您的进程被信号异常终止)。
[Q] All I'm trying to accomplish to to have my: main loop run until either ctrlc or power is disconnected or something really bad happens.
不,有限制!你无法捕捉到所有信号。有些信号是不可捕捉的,例如SIGKILL
和 SIGSTOP
都是终止信号。引用一个:
— Macro: int
SIGKILL
The
SIGKILL
signal is used to cause immediate program termination. It cannot be handled or ignored, and is therefore always fatal. It is also not possible to block this signal.
所以你不能制作 a program that cannot be interrupted (an uninterrupted program) !
<子>我不确定,但可能你可以在 Windows 系统中做这样的事情:通过编写 TSR(某种内核模式 Hook )。我记得在我论文的时候,有些病毒甚至无法从任务管理器中终止,但我也相信它们是通过管理员权限欺骗用户的。
希望这个回答对你有帮助。
关于c++ - 简单的 Linux 信号处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17942034/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
Linux 管道可以缓冲多少数据?这是可配置的吗? 如果管道的两端在同一个进程中,但线程不同,这会有什么不同吗? 请注意:这个“同一个进程,两个线程”的问题是理论上的边栏,真正的问题是关于缓冲的。 最
我找到了here [最后一页] 一种有趣的通过 Linux 启动 Linux 的方法。不幸的是,它只是被提及,我在网上找不到任何有用的链接。那么有人听说过一种避免引导加载程序而使用 Linux 的方法
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我试图了解 ld-linux.so 如何在 Linux 上解析对版本化符号的引用。我有以下文件: 测试.c: void f(); int main() { f(); } a.c 和 b.c:
与 RetroPie 的工作原理类似,我可以使用 Linux 应用程序作为我的桌面环境吗?我实际上并不需要像实际桌面和安装应用程序这样的东西。我只需要一种干净简单的方法来在 RaspberryPi 上
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
有什么方法可以覆盖现有的源代码,我应该用 PyQt、PyGTK、Java 等从头开始构建吗? 最佳答案 如果您指的是软件本身而不是它所连接的存储库,那么自定义应用程序的方法就是 fork 项目。据我所
我的情况是:我在一个磁盘上安装了两个 linux。我将第一个安装在/dev/sda1 中,然后在/dev/sda2 中安装第二个然后我运行第一个系统,我写了一个脚本来在第一个系统运行时更新它。
我在 i2c-0 总线上使用地址为 0x3f 的系统监视器设备。该设备在设备树中配置有 pmbus 驱动程序。 问题是,加载 linux 内核时,这个“Sysmon”设备没有供电。因此,当我在总线 0
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我正试图在 linux 模块中分配一大块内存,而 kalloc 做不到。 我知道唯一的方法是使用 alloc_bootmem(unsigned long size) 但我只能从 linux 内核而不是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我有 .sh 文件来运行应用程序。在该文件中,我想动态设置服务器名称,而不是每次都配置。 我尝试了以下方法,它在 CentOS 中运行良好。 nohup /voip/java/jdk1.8.0_71/
我是在 Linux 上开发嵌入式 C++ 程序的新手。我有我的 Debian 操作系统,我在其中开发和编译了我的 C++ 项目(一个简单的控制台进程)。 我想将我的应用程序放到另一个 Debian 操
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我使用4.19.78版本的稳定内核,我想找到带有企鹅二进制数据的C数组。系统启动时显示。我需要在哪里搜索该内容? 我在 include/linux/linux_logo.h 文件中只找到了一些 Log
我知道可以使用 gdb 的服务器模式远程调试代码,我知道可以调试针对另一种架构交叉编译的代码,但是是否可以更进一步,从远程调试 Linux 应用程序OS X 使用 gdbserver? 最佳答案 当然
是否有任何可能的方法来运行在另一个 Linux 上编译的二进制文件?我知道当然最简单的是在另一台机器上重建它,但假设我们唯一能得到的是一个二进制文件,那么这可能与否? (我知道这可能并不容易,但我只是
我是一名优秀的程序员,十分优秀!