gpt4 book ai didi

c++ - 简单的 Linux 信号处理

转载 作者:IT老高 更新时间:2023-10-28 12:41:17 26 4
gpt4 key购买 nike

我有一个程序可以创建许多线程并运行直到嵌入式计算机关闭电源,或者用户使用 killctrlc 终止进程。

这是一些代码以及 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?
}

我想知道一些事情:

  1. 是否需要任何信号处理?
    我在这个线程中读到 "Linux C catching kill signal for graceful termination" ,显然操作系统将为我处理清理工作。因此,我可以只用一个无限循环替换信号处理程序,让操作系统优雅地退出线程、取消分配内存等吗?

  2. 关于干净终止,我是否需要关注其他任何信号?本帖"How does SIGINT relate to the other termination signals?" ,对于列出我可能关心的所有信号很有用,但实际需要处理的信号有多少?

  3. 我的示例中的 terminate 变量是否必须是 volatile 的?我见过很多例子,这个变量是 volatile 的,还有一些不是。

  4. 我了解到 signal() 现在已弃用,可以使用 sigaction()。是否有任何非常好的示例来展示如何从之前的 signal() 调用进行转换?我对必须创建/传递的新结构以及它们如何组合在一起遇到了问题。

  5. 是否需要第二次调用 signal()
    sigaction() 是否有类似的事情需要注意?

明确地说,我要完成的所有工作就是让我的主循环运行,直到 ctrlc 或电源断开或发生非常糟糕的事情。

最佳答案

[Q-3] Does the terminate variable in my example have to be volatile? 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 use sigaction(). Are there any really good examples to show how to convert from the previous signal() 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 );

引用:

  1. Beginning Linux Programming, 4th Edition : 在本书中,你的代码在“第 11 章:进程和信号”中用 sigaction() 很好地解释了。
  2. sigaction documentation ,包括一个例子(快速学习)。
  3. GNU C 库:Signal Handling
    *我从1开始, 目前我正在阅读 3 GNU 库

[Q-5] Is the second call to signal() necessary? Is there something similar that I need to be concerned with for sigaction()?

我不清楚为什么在程序终止之前将其设置为默认操作。我想下面这段话会给你答案:

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.

不,有限制!你无法捕捉到所有信号。有些信号是不可捕捉的,例如SIGKILLSIGSTOP 都是终止信号。引用一个:

— 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com